ATLAS Offline Software
Loading...
Searching...
No Matches
EmulContext.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
14namespace Trig {
15
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 =
26 std::enable_if_t<std::is_nothrow_move_constructible<T>::value>>
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;
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
93template <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
const std::type_info & type() const
Definition EmulContext.h:65
HolderT(std::unique_ptr< T > &&value)
Definition EmulContext.h:60
std::unique_ptr< T > m_value
Definition EmulContext.h:36
const T * value() const
Definition EmulContext.h:70
virtual ~IHolder()=default
virtual const std::type_info & type() const =0
EmulContext(const EmulContext &)=delete
const T * get(const std::string &name) const
Definition EmulContext.h:94
~EmulContext()=default
std::unordered_map< std::string, std::unique_ptr< IHolder > > m_store
Definition EmulContext.h:54
void store(const std::string &name, std::unique_ptr< T > &&object)
Definition EmulContext.h:77
EmulContext & operator=(const EmulContext &)=delete
EmulContext()=default
The common trigger namespace for trigger analysis tools.
STL namespace.