Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
RefTable.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_REFTABLE_H
2 #define GAUDIKERNEL_REFTABLE_H 1
3 
4 
5 // Include files
6 #include "GaudiKernel/Kernel.h"
10 #include "GaudiKernel/SmartRef.h"
11 #include "GaudiKernel/HashMap.h" // Cannot use maps through sharable images....
12 
13 // Externals
14 static const CLID CLID_RefTable1to1 = 300;
15 static const CLID CLID_RefTable1toN = 301;
16 
17 
18 //------------------------------------------------------------------------------
19 //
20 // Implementation and definition of template classes
21 // RefTableBase, RefTable1to1, RefTable1toN
22 //
23 // Author : Markus Frank
24 //
25 //------------------------------------------------------------------------------
45 template <class FROM, class MAPENTRY> class RefTableBase : public DataObject {
46 public:
47  // Type of the key
48  typedef FROM KeyType;
49  // Type of map entries
50  typedef MAPENTRY EntryType;
51  // My own type
63 private:
65  TableType m_table;
71 
72 protected:
74  bool insertMapElement ( const KeyType* from, EntryType& to ) {
75  return m_table.insert( from, to );
76  }
78  EntryType* i_reference(const KeyType* from) {
79  auto i = m_table.find( from );
80  return i != 0 ? &(i->second) : nullptr;
81  }
83  const EntryType* i_reference(const KeyType* from) const {
84  auto i = m_table.find( from );
85  return i != m_table.end() ? &(i->second) : nullptr;
86  }
87 
88 public:
90  RefTableBase(const CLID& clid, int len) : m_clid(clid), m_table(len) {
91  }
93  virtual ~RefTableBase() = default;
95  virtual void clear() {
96  m_table.clear();
97  }
99  iterator begin() {
100  return m_table.begin();
101  }
103  const_iterator begin() const {
104  return m_table.begin();
105  }
107  iterator end() {
108  return m_table.end();
109  }
111  const_iterator end() const {
112  return m_table.end();
113  }
115  long size() const {
116  return m_table.size();
117  }
119  void reserve(int len) {
120  m_table.reserve(len);
121  }
123  virtual StreamBuffer& serialize( StreamBuffer& s ) const {
124  DataObject::serialize(s) << m_table.size();
125  //for (TableType::const_iterator i = m_table.begin(), stop = m_table.end(); i != stop; i++ ) {
126  // SmartRef<KeyType> fromRef;
127  // fromRef = (KeyType*)(*i).first;
128  // s << fromRef(this);
129  // s << (*i).second(this);
130  //}
131  return s;
132  }
135  long siz;
136  DataObject::serialize(s) >> siz;
137  m_table.reserve(siz);
138  //for ( long i = 0; i < siz; i++ ) {
139  // SmartRef<KeyType> fromRef;
140  // EntryType entry;
141  // s >> fromRef(this);
142  // s >> entry(this);
143  // insertMapElement( fromRef, entry);
144  //}
145  return s;
146  }
147 };
148 
149 template <class FROM, class TO> class RefTable1to1
150 : public RefTableBase< FROM , SmartRef<TO> > {
151 public:
153  RefTable1to1 (const CLID& clid, int len=16)
154  : RefTableBase< FROM , SmartRef<TO> >(clid, len)
155  {
156  }
158  ~RefTable1to1() override = default;
159 
161  virtual const CLID& clID() const {
162  return m_clid;
163  }
165  bool insert ( const FROM* from, TO* to ) {
166  return insertMapElement(from, EntryType(to));
167  }
169  bool insert ( const FROM* from, const EntryType& to) {
170  // We MUST check the environment of the smart pointer!
171  if ( 0 != to.data() || StreamBuffer::INVALID != to.hintID() ) {
172  return insertMapElement(from, EntryType(to));
173  }
174  return false;
175  }
177  TO* reference(const FROM* from) {
178  EntryType* e = i_reference(from);
179  return (0 == e) ? 0 : (*e);
180  }
181 
183  const TO* reference(const FROM* from) const {
184  auto e = i_reference(from);
185  return e ? *e : nullptr;
186  }
187 
189  bool isReferenced(const FROM* from, const TO* to ) const {
190  auto e = i_reference(from);
191  return e && ( *e == to );
192  }
194  bool isReferenced(const FROM* from, const EntryType& to ) {
195  const EntryType* e = i_reference(from);
196  return assoc && (*e!=to) && (e->target()==to.target());
197  }
198 };
199 
200 template <class FROM, class TO> class RefTable1toN
201 : public RefTableBase< FROM , SmartRefVector<TO> > {
202 public:
204  RefTable1toN (const CLID& clid, int len=16)
205  : RefTableBase< FROM , SmartRefVector<TO> >(clid, len) {
206  }
208  ~RefTable1toN() override = default;
209 
211  virtual const CLID& clID() const {
212  return m_clid;
213  }
215  bool insert ( const FROM* from, TO* to) {
216  EntryType* entry = i_reference(from);
217  if ( 0 == entry ) {
218  bool result = insertMapElement(from, EntryType());
219  EntryType* newEntry = i_reference(from);
220  if ( !( 0 == newEntry) ) {
221  newEntry->push_back( SmartRef<TO>(to) );
222  return true;
223  }
224  return false;
225  }
226  entry->push_back( SmartRef<TO>(to) );
227  return true;
228  }
230  bool insert ( const FROM* from, const SmartRef<TO>& to) {
231  EntryType* entry = i_reference(from);
232  if ( 0 == entry ) {
233  bool result = insertMapElement(from, EntryType());
234  EntryType* newEntry = i_reference(from);
235  if ( !(0 == newEntry) ) {
236  newEntry->push_back( to );
237  return true;
238  }
239  return false;
240  }
241  entry->push_back( to );
242  return true;
243  }
245  bool insert ( const FROM* from, const EntryType& to) {
246  return insertMapElement(from, const_cast<EntryType&>(to));
247  }
249  EntryType& reference(const FROM* from) {
250  static EntryType empty;
251  EntryType* e = i_reference(from);
252  return e ? *e : empty;
253  }
255  const EntryType& reference(const FROM* from) const {
256  static const EntryType empty;
257  EntryType* e = i_reference(from);
258  return e ? *e : empty;
259  }
261  bool isReferenced(const FROM* from, const EntryType& to ) {
262  auto e = i_reference(from);
263  return e && ( *e == to );
264  }
266  bool isReferenced(const FROM* from, const TO* to ) {
267  return isReferenced(from, SmartRef<TO>(to));
268  }
270  bool isReferenced(const FROM* from, const SmartRef<TO>& to ) {
271  const EntryType* e = i_reference(from);
272  return assoc && std::find(e->begin(), e->end(), to) != e->end();
273  }
274 };
275 
276 
277 #endif // GAUDIKERNEL_REFTABLE_H
278 
CLID m_clid
Class id of the reference table.
Definition: RefTable.h:62
RefTableBase< FROM, EntryType > BaseType
Definition: RefTable.h:52
size_type size() const
Definition: Map.h:182
iterator begin()
Start of direct access iterator.
Definition: RefTable.h:99
bool isReferenced(const FROM *from, const TO *to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:266
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
virtual StreamBuffer & serialize(StreamBuffer &s)
Serialize the object for reading.
Definition: RefTable.h:134
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:278
const EntryType * i_reference(const KeyType *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:83
GaudiUtils::HashMap< const void *, EntryType > TableType
Define Reference map.
Definition: RefTable.h:56
TableType::const_iterator const_iterator
Definition of map iterator (CONST)
Definition: RefTable.h:60
const_iterator begin() const
Start of direct access iterator (CONST)
Definition: RefTable.h:103
T end(T...args)
TO * reference(const FROM *from)
Find Reference from it&#39;s source entry.
Definition: RefTable.h:177
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:194
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:161
const EntryType & reference(const FROM *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:255
Kernel objects: SmartRefVector.
EntryType * i_reference(const KeyType *from)
Find Reference from it&#39;s source entry.
Definition: RefTable.h:78
FROM KeyType
Definition: RefTable.h:48
bool insert(const FROM *from, const EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:245
const TO * reference(const FROM *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:183
T push_back(T...args)
void reserve(int len)
Size of References.
Definition: RefTable.h:119
long size() const
Size of References.
Definition: RefTable.h:115
iterator end()
Definition: Map.h:132
TableType::iterator iterator
Definition of map iterator.
Definition: RefTable.h:58
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:161
iterator end()
End of direct access iterator.
Definition: RefTable.h:107
RefTableBase(const CLID &clid, int len)
Constructors.
Definition: RefTable.h:90
iterator find(const key_type &key)
Definition: Map.h:149
RefTable1toN(const CLID &clid, int len=16)
Standard Constructor.
Definition: RefTable.h:204
const_iterator end() const
End of direct access iterator (CONST)
Definition: RefTable.h:111
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
map_type::const_iterator const_iterator
Definition: Map.h:100
virtual void clear()
Clear Reference map.
Definition: RefTable.h:95
iterator begin()
Definition: Map.h:131
T find(T...args)
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: RefTable.h:123
template <class FROM, class TO, class MAPENTRY> class RefTable
Definition: RefTable.h:45
TableType m_table
Reference map.
Definition: RefTable.h:65
map_type::iterator iterator
Definition: Map.h:99
T begin(T...args)
long hintID() const
Access hint id:
Definition: SmartRef.h:145
bool isReferenced(const FROM *from, const SmartRef< TO > &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:270
bool insert(const FROM *from, const SmartRef< TO > &to)
Insert new Entry into Reference container.
Definition: RefTable.h:230
RefTable1to1(const CLID &clid, int len=16)
Standard Constructor.
Definition: RefTable.h:153
bool insert(const FROM *from, const EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:169
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:165
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:211
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:168
string s
Definition: gaudirun.py:245
void clear()
Definition: Map.h:178
MAPENTRY EntryType
Definition: RefTable.h:50
bool insertMapElement(const KeyType *from, EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:74
SmartRef< KeyType > m_fromRef
This is a completely useless entry, but the compiler wants it to be instantiated before the serialize...
Definition: RefTable.h:70
EntryType & reference(const FROM *from)
Find Reference from it&#39;s source entry.
Definition: RefTable.h:249
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
bool isReferenced(const FROM *from, const TO *to) const
Check if two entries are associated to each other.
Definition: RefTable.h:189
virtual ~RefTableBase()=default
Destructor.
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:261
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:215