EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WhiteBoard.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file WhiteBoard.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 
13 #include <memory>
14 #include <stdexcept>
15 #include <string>
16 #include <type_traits>
17 #include <typeinfo>
18 #include <unordered_map>
19 #include <vector>
20 
21 namespace ActsExamples {
22 
29 class WhiteBoard {
30  public:
31  WhiteBoard(std::unique_ptr<const Acts::Logger> logger =
33 
34  // A WhiteBoard holds unique elements and can not be copied
35  WhiteBoard(const WhiteBoard& other) = delete;
36  WhiteBoard& operator=(const WhiteBoard&) = delete;
37 
43  template <typename T>
44  void add(const std::string& name, T&& object);
45 
51  template <typename T>
52  const T& get(const std::string& name) const;
53 
54  private:
55  // type-erased value holder for move-constructible types
56  struct IHolder {
57  virtual ~IHolder() = default;
58  virtual const std::type_info& type() const = 0;
59  };
60  template <typename T,
61  typename =
63  struct HolderT : public IHolder {
65 
66  HolderT(T&& v) : value(std::move(v)) {}
67  const std::type_info& type() const { return typeid(T); }
68  };
69 
70  std::unique_ptr<const Acts::Logger> m_logger;
71  std::unordered_map<std::string, std::unique_ptr<IHolder>> m_store;
72 
73  const Acts::Logger& logger() const { return *m_logger; }
74 };
75 
76 } // namespace ActsExamples
77 
79  std::unique_ptr<const Acts::Logger> logger)
80  : m_logger(std::move(logger)) {}
81 
82 template <typename T>
83 inline void ActsExamples::WhiteBoard::add(const std::string& name, T&& object) {
84  if (name.empty()) {
85  throw std::invalid_argument("Object can not have an empty name");
86  }
87  if (0 < m_store.count(name)) {
88  throw std::invalid_argument("Object '" + name + "' already exists");
89  }
90  m_store.emplace(name, std::make_unique<HolderT<T>>(std::forward<T>(object)));
91  ACTS_VERBOSE("Added object '" << name << "'");
92 }
93 
94 template <typename T>
95 inline const T& ActsExamples::WhiteBoard::get(const std::string& name) const {
96  auto it = m_store.find(name);
97  if (it == m_store.end()) {
98  throw std::out_of_range("Object '" + name + "' does not exists");
99  }
100  const IHolder* holder = it->second.get();
101  if (typeid(T) != holder->type()) {
102  throw std::out_of_range("Type missmatch for object '" + name + "'");
103  }
104  ACTS_VERBOSE("Retrieved object '" << name << "'");
105  return reinterpret_cast<const HolderT<T>*>(holder)->value;
106 }