ATLAS Offline Software
MetaCont.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ATHENAKERNEL_METACONT_H
6 #define ATHENAKERNEL_METACONT_H
7 
8 #include <sstream>
9 #include <map>
10 #include <vector>
11 #include <typeinfo>
12 #include <mutex>
13 
14 #include "AthenaKernel/CLASS_DEF.h"
15 #include "AthenaKernel/BaseInfo.h"
16 #include "AthenaKernel/SourceID.h"
19 
20 namespace SG {
21  template<class T> class ReadMetaHandle;
22 }
23 
24 class MetaContBase {
25  public:
28  virtual ~MetaContBase(){};
29 
30  virtual bool insert(const SourceID& sid, void* obj) = 0;
31  virtual size_t erase(const SourceID& sid) = 0;
32 
33  virtual int entries() const { return 0; }
34  virtual bool valid(const SourceID& sid) const = 0;
35 
36  virtual std::vector<SourceID> sources() const = 0;
37 
38  virtual void list(std::ostringstream& stream) const = 0;
39 
40  virtual void* getAsVoid(const SourceID& sid) const = 0;
41 
42  private:
43 };
44 
46 
47 template <typename T>
48 class MetaCont: public MetaContBase {
49  public:
50  typedef T Payload_t;
51  friend SG::ReadMetaHandle<T>;
52 
53  // cppcheck-suppress syntaxError
54  MetaCont() {};
56 
57  // Virtual functions
58  virtual bool insert(const SourceID& sid, void* obj) override final;
59  virtual size_t erase(const SourceID& sid) override final;
60 
61 
62  virtual int entries() const override;
63  virtual bool valid(const SourceID& sid) const override final;
64 
65  virtual std::vector<SourceID> sources() const override final;
66 
67  virtual void list(std::ostringstream& stream) const override final;
68 
69  // Non-virtual functions
70  bool insert(const SourceID& sid, T* t);
71 
73  bool find(const SourceID& sid, T*& t) const;
75 
76  void* getAsVoid(const SourceID& sid) const override final { return get(sid); }
77 
78  private:
79 
80  mutable std::mutex m_mut;
81 
82  typedef std::map<SourceID,T*> MetaContSet;
84 };
85 
86 
87 namespace SG {
88 
89 
96  template <class T, class U>
98  {
100  static void init() {}
101  };
102 
103 } // namespace SG
104 
105 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
106 
107 template <typename T>
109  for (auto t : m_metaSet) {
110  delete t.second;
111  }
112  m_metaSet.clear();
113 }
114 
115 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
116 
117 template <typename T>
118 bool MetaCont<T>::insert(const SourceID& sid, void* obj) {
119  T* t = static_cast<T*>(obj);
120  return insert(sid, t);
121 }
122 
123 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
124 
125 template <typename T>
126 size_t MetaCont<T>::erase(const SourceID& sid) {
127  std::lock_guard<std::mutex> lock(m_mut);
128  return m_metaSet.erase(sid);
129 }
130 
131 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
132 
133 template <typename T>
134 bool MetaCont<T>::insert(const SourceID& sid, T* t) {
135  std::lock_guard<std::mutex> lock(m_mut);
136  return m_metaSet.insert(std::make_pair(sid,t)).second;
137 }
138 
139 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
140 
141 template <typename T>
142 bool MetaCont<T>::valid(const SourceID& sid) const {
143  std::lock_guard<std::mutex> lock(m_mut);
144  return m_metaSet.find(sid)!=m_metaSet.end();
145 }
146 
147 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
148 
149 template <typename T>
150 bool MetaCont<T>::find(const SourceID& sid, T*& t) const {
151  std::lock_guard<std::mutex> lock(m_mut);
152 
153  typename MetaContSet::const_iterator itr = m_metaSet.find(sid);
154  if (itr != m_metaSet.end()) {
155  t=itr->second;
156  return true;
157  }
158 
159  return false;
160 }
161 
162 
163 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
164 
165 template <typename T>
166 T* MetaCont<T>::get(const SourceID& sid) const {
167  std::lock_guard<std::mutex> lock(m_mut);
168 
169  typename MetaContSet::const_iterator itr = m_metaSet.find(sid);
170  if (itr != m_metaSet.end()) {
171  return itr->second;
172  }
173 
174  return nullptr;
175 }
176 
177 
178 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
179 
180 template <typename T>
181 void MetaCont<T>::list(std::ostringstream& stream) const {
182  std::lock_guard<std::mutex> lock(m_mut);
183  // To Do: perhaps extend this output?
184  stream << "MetaCont with size : [" << m_metaSet.size() << "]" << std::endl;
185  for(const auto& mapel : m_metaSet) {
186  stream << "... Key : " << mapel.first << std::endl;
187  }
188 }
189 
190 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
191 
192 template <typename T>
193 int MetaCont<T>::entries() const {
194  std::lock_guard<std::mutex> lock(m_mut);
195  return m_metaSet.size();
196 }
197 
198 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
199 
200 template <typename T>
201 std::vector<MetaContBase::SourceID>
203  std::lock_guard<std::mutex> lock(m_mut);
204 
205  std::vector<MetaContBase::SourceID> r;
206  for (auto ent : m_metaSet) {
207  r.push_back(ent.first);
208  }
209 
210  return r;
211 }
212 
213 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
214 
215 CLASS_DEF( MetaContBase , 34480469 , 1 )
216 
217 
218 // METACONT_DEF(TYPE, CLID);
220 //
221 #define METACONT_DEF(T, CLID) \
222  CLASS_DEF( MetaCont<T>, CLID, 1 ) \
223  SG_BASES( MetaCont<T>, MetaContBase )
224 
225 #endif
226 
MetaCont::sources
virtual std::vector< SourceID > sources() const override final
Definition: MetaCont.h:202
beamspotman.r
def r
Definition: beamspotman.py:676
MetaContBase::insert
virtual bool insert(const SourceID &sid, void *obj)=0
SG::MetaContDataBucket
Allow converting MetaCont<T> to T.
Definition: MetaContDataBucket.h:42
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
MetaContBase::MetaContBase
MetaContBase()
Definition: MetaCont.h:27
MetaContDataBucket.h
Allow converting MetaCont<T> to T.
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
MetaCont::get
T * get(const SourceID &sid) const
Definition: MetaCont.h:166
MetaContBase::list
virtual void list(std::ostringstream &stream) const =0
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:562
MetaCont::~MetaCont
~MetaCont()
Definition: MetaCont.h:108
MetaContBase::valid
virtual bool valid(const SourceID &sid) const =0
MetaCont::find
bool find(const SourceID &sid, T *&t) const
various Get methods
Definition: MetaCont.h:150
MetaCont::MetaCont
MetaCont()
Definition: MetaCont.h:54
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
MetaContBase
Definition: MetaCont.h:24
MetaCont::insert
virtual bool insert(const SourceID &sid, void *obj) override final
Definition: MetaCont.h:118
SG::DataBucketTrait< MetaCont< T >, U >::init
static void init()
Definition: MetaCont.h:100
MetaContBase::~MetaContBase
virtual ~MetaContBase()
Definition: MetaCont.h:28
MetaCont
Definition: MetaCont.h:48
BaseInfo.h
Provide an interface for finding inheritance information at run time.
MetaCont::MetaContSet
std::map< SourceID, T * > MetaContSet
Definition: MetaCont.h:82
DataBucketTraitFwd.h
MetaCont::list
virtual void list(std::ostringstream &stream) const override final
Definition: MetaCont.h:181
MetaContBase::sources
virtual std::vector< SourceID > sources() const =0
MetaCont::erase
virtual size_t erase(const SourceID &sid) override final
Definition: MetaCont.h:126
MetaCont::valid
virtual bool valid(const SourceID &sid) const override final
Definition: MetaCont.h:142
MetaCont::getAsVoid
void * getAsVoid(const SourceID &sid) const override final
Definition: MetaCont.h:76
MetaCont::m_metaSet
MetaContSet m_metaSet
Definition: MetaCont.h:83
SourceID.h
Type used to identify a metadata source.
MetaContBase::erase
virtual size_t erase(const SourceID &sid)=0
SG::ReadMetaHandle
Definition: MetaCont.h:21
MetaCont::entries
virtual int entries() const override
Definition: MetaCont.h:193
MetaContBase::SourceID
SG::SourceID SourceID
Definition: MetaCont.h:26
SG::SourceID
std::string SourceID
Definition: AthenaKernel/AthenaKernel/SourceID.h:23
CLASS_DEF
#define CLASS_DEF(NAME, CID, VERSION)
associate a clid and a version to a type eg
Definition: Control/AthenaKernel/AthenaKernel/CLASS_DEF.h:64
SG::DataBucketTrait< MetaCont< T >, U >::type
MetaContDataBucket< U > type
Definition: MetaCont.h:99
MetaCont::Payload_t
T Payload_t
Definition: MetaCont.h:50
MetaCont::m_mut
std::mutex m_mut
Definition: MetaCont.h:80
python.PyAthena.obj
obj
Definition: PyAthena.py:135
SG::DataBucketTrait
Metafunction to find the proper DataBucket class for T.
Definition: DataBucketTraitFwd.h:30
MetaContBase::getAsVoid
virtual void * getAsVoid(const SourceID &sid) const =0
CLASS_DEF.h
macros to associate a CLID to a type
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
MetaContBase::entries
virtual int entries() const
Definition: MetaCont.h:33