ATLAS Offline Software
EmulContext.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef EMUL_CONTEXT_H
6 #define EMUL_CONTEXT_H
7 
8 #include <memory>
9 #include <stdexcept>
10 #include <unordered_map>
11 #include <utility>
12 #include <typeinfo>
13 
14 namespace Trig {
15 
16 class EmulContext {
17  private:
18  class IHolder {
19  public:
20  virtual ~IHolder() = default;
21  virtual const std::type_info &type() const = 0;
22  };
23 
24  template <typename T,
25  typename Enable =
27  class HolderT
28  : public IHolder {
29  public:
30  HolderT(std::unique_ptr<T>&& value);
31 
32  const std::type_info &type() const;
33  const T* value() const;
34 
35  private:
36  std::unique_ptr<T> m_value;
37  };
38 
39  // =========== //
40 
41  public:
42  EmulContext() = default;
43  EmulContext(const EmulContext&) = delete;
44  EmulContext& operator=(const EmulContext&) = delete;
45  ~EmulContext() = default;
46 
47  template <typename T>
48  void store(const std::string &name, std::unique_ptr<T> &&object);
49 
50  template <typename T>
51  const T *get(const std::string &name) const;
52 
53  private:
54  std::unordered_map<std::string, std::unique_ptr<IHolder>> m_store;
55 };
56 
57 /* ============================================================== */
58 
59  template <typename T, typename Enable>
61  : m_value( std::move(value) )
62  {}
63 
64  template <typename T, typename Enable>
65  const std::type_info& EmulContext::HolderT<T, Enable>::type() const {
66  return typeid(T);
67  }
68 
69  template <typename T, typename Enable>
71  return m_value.get();
72  }
73 
74  // =========== //
75 
76  template <typename T>
77  inline void EmulContext::store(const std::string &name,
78  std::unique_ptr<T> &&object)
79  {
80  if (name.empty())
81  throw std::invalid_argument("Object can not have an empty name in order to be stored in memory");
82 
83  if (m_store.find(name) != m_store.end())
84  throw std::runtime_error("Object with name `" + name +
85  "` already stored in memory");
86 
87  using stored_object_t = std::unique_ptr<T>;
88  m_store.emplace( name,
89  std::make_unique< HolderT<T> >( std::forward<stored_object_t>( object ) )
90  );
91  }
92 
93 template <typename T>
94  inline const T* EmulContext::get(const std::string &name) const
95  {
96  auto itr = m_store.find(name);
97  if ( itr == m_store.end() )
98  throw std::invalid_argument("Object with name `" + name +
99  "` is not in memory. Cannot be retrieved: " + name);
100 
101  const IHolder *holder = (*itr).second.get();
102  if (typeid(T) != holder->type())
103  throw std::invalid_argument("Type mismatch for object '" + name + "'");
104 
105  return reinterpret_cast<const HolderT<T> *>(holder)->value();
106  }
107 
108 } // namespace
109 
110 #endif
Trig::EmulContext::IHolder::~IHolder
virtual ~IHolder()=default
Trig
The common trigger namespace for trigger analysis tools.
Definition: CaloTowerVecMon.h:44
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
Trig::EmulContext::IHolder::type
virtual const std::type_info & type() const =0
Trig::EmulContext::HolderT::HolderT
HolderT(std::unique_ptr< T > &&value)
Definition: EmulContext.h:60
Trig::EmulContext::HolderT::type
const std::type_info & type() const
Definition: EmulContext.h:65
Trig::EmulContext::HolderT::value
const T * value() const
Definition: EmulContext.h:70
athena.value
value
Definition: athena.py:122
Trig::EmulContext::~EmulContext
~EmulContext()=default
Trig::EmulContext::m_store
std::unordered_map< std::string, std::unique_ptr< IHolder > > m_store
Definition: EmulContext.h:54
Trig::EmulContext::HolderT
Definition: EmulContext.h:28
Trig::EmulContext::get
const T * get(const std::string &name) const
Definition: EmulContext.h:94
Trig::EmulContext::EmulContext
EmulContext(const EmulContext &)=delete
Trig::EmulContext
Definition: EmulContext.h:16
Trig::EmulContext::IHolder
Definition: EmulContext.h:18
Trig::EmulContext::operator=
EmulContext & operator=(const EmulContext &)=delete
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
Trig::EmulContext::HolderT::m_value
std::unique_ptr< T > m_value
Definition: EmulContext.h:36
Trig::EmulContext::store
void store(const std::string &name, std::unique_ptr< T > &&object)
Definition: EmulContext.h:77
Trig::EmulContext::EmulContext
EmulContext()=default