Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  MetaCont() {};
55 
56  // Virtual functions
57  virtual bool insert(const SourceID& sid, void* obj) override final;
58  virtual size_t erase(const SourceID& sid) override final;
59 
60 
61  virtual int entries() const override;
62  virtual bool valid(const SourceID& sid) const override final;
63 
64  virtual std::vector<SourceID> sources() const override final;
65 
66  virtual void list(std::ostringstream& stream) const override final;
67 
68  // Non-virtual functions
69  bool insert(const SourceID& sid, T* t);
70 
72  bool find(const SourceID& sid, T*& t) const;
74 
75  void* getAsVoid(const SourceID& sid) const override final { return get(sid); }
76 
77  private:
78 
79  mutable std::mutex m_mut;
80 
81  typedef std::map<SourceID,T*> MetaContSet;
83 };
84 
85 
86 namespace SG {
87 
88 
95  template <class T, class U>
97  {
99  static void init() {}
100  };
101 
102 } // namespace SG
103 
104 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
105 
106 template <typename T>
108  for (auto t : m_metaSet) {
109  delete t.second;
110  }
111  m_metaSet.clear();
112 }
113 
114 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
115 
116 template <typename T>
117 bool MetaCont<T>::insert(const SourceID& sid, void* obj) {
118  T* t = static_cast<T*>(obj);
119  return insert(sid, t);
120 }
121 
122 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
123 
124 template <typename T>
125 size_t MetaCont<T>::erase(const SourceID& sid) {
126  std::lock_guard<std::mutex> lock(m_mut);
127  return m_metaSet.erase(sid);
128 }
129 
130 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
131 
132 template <typename T>
133 bool MetaCont<T>::insert(const SourceID& sid, T* t) {
134  std::lock_guard<std::mutex> lock(m_mut);
135  return m_metaSet.insert(std::make_pair(sid,t)).second;
136 }
137 
138 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
139 
140 template <typename T>
141 bool MetaCont<T>::valid(const SourceID& sid) const {
142  std::lock_guard<std::mutex> lock(m_mut);
143  return m_metaSet.find(sid)!=m_metaSet.end();
144 }
145 
146 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
147 
148 template <typename T>
149 bool MetaCont<T>::find(const SourceID& sid, T*& t) const {
150  std::lock_guard<std::mutex> lock(m_mut);
151 
152  typename MetaContSet::const_iterator itr = m_metaSet.find(sid);
153  if (itr != m_metaSet.end()) {
154  t=itr->second;
155  return true;
156  }
157 
158  return false;
159 }
160 
161 
162 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
163 
164 template <typename T>
165 T* MetaCont<T>::get(const SourceID& sid) const {
166  std::lock_guard<std::mutex> lock(m_mut);
167 
168  typename MetaContSet::const_iterator itr = m_metaSet.find(sid);
169  if (itr != m_metaSet.end()) {
170  return itr->second;
171  }
172 
173  return nullptr;
174 }
175 
176 
177 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
178 
179 template <typename T>
180 void MetaCont<T>::list(std::ostringstream& stream) const {
181  std::lock_guard<std::mutex> lock(m_mut);
182  // To Do: perhaps extend this output?
183  stream << "MetaCont with size : [" << m_metaSet.size() << "]" << std::endl;
184  for(const auto& mapel : m_metaSet) {
185  stream << "... Key : " << mapel.first << std::endl;
186  }
187 }
188 
189 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
190 
191 template <typename T>
192 int MetaCont<T>::entries() const {
193  std::lock_guard<std::mutex> lock(m_mut);
194  return m_metaSet.size();
195 }
196 
197 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
198 
199 template <typename T>
200 std::vector<MetaContBase::SourceID>
202  std::lock_guard<std::mutex> lock(m_mut);
203 
204  std::vector<MetaContBase::SourceID> r;
205  for (auto ent : m_metaSet) {
206  r.push_back(ent.first);
207  }
208 
209  return r;
210 }
211 
212 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
213 
214 CLASS_DEF( MetaContBase , 34480469 , 1 )
215 
216 
217 // METACONT_DEF(TYPE, CLID);
219 //
220 #define METACONT_DEF(T, CLID) \
221  CLASS_DEF( MetaCont<T>, CLID, 1 ) \
222  SG_BASES( MetaCont<T>, MetaContBase )
223 
224 #endif
225 
MetaCont::sources
virtual std::vector< SourceID > sources() const override final
Definition: MetaCont.h:201
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:43
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:165
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:560
MetaCont::~MetaCont
~MetaCont()
Definition: MetaCont.h:107
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:149
MetaCont::MetaCont
MetaCont()
Definition: MetaCont.h:53
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:117
SG::DataBucketTrait< MetaCont< T >, U >::init
static void init()
Definition: MetaCont.h:99
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:81
DataBucketTraitFwd.h
MetaCont::list
virtual void list(std::ostringstream &stream) const override final
Definition: MetaCont.h:180
MetaContBase::sources
virtual std::vector< SourceID > sources() const =0
MetaCont::erase
virtual size_t erase(const SourceID &sid) override final
Definition: MetaCont.h:125
MetaCont::valid
virtual bool valid(const SourceID &sid) const override final
Definition: MetaCont.h:141
MetaCont::getAsVoid
void * getAsVoid(const SourceID &sid) const override final
Definition: MetaCont.h:75
MetaCont::m_metaSet
MetaContSet m_metaSet
Definition: MetaCont.h:82
SourceID.h
Type used to identify a metadata source.
columnar::final
CM final
Definition: ColumnAccessor.h:106
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:192
MetaContBase::SourceID
SG::SourceID SourceID
Definition: MetaCont.h:26
SG::SourceID
std::string SourceID
Definition: AthenaKernel/AthenaKernel/SourceID.h:25
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:67
SG::DataBucketTrait< MetaCont< T >, U >::type
MetaContDataBucket< U > type
Definition: MetaCont.h:98
MetaCont::Payload_t
T Payload_t
Definition: MetaCont.h:50
MetaCont::m_mut
std::mutex m_mut
Definition: MetaCont.h:79
python.PyAthena.obj
obj
Definition: PyAthena.py:132
SG::DataBucketTrait
Metafunction to find the proper DataBucket class for T.
Definition: DataBucketTraitFwd.h:28
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