The Gaudi Framework  v29r0 (ff2e7097)
RefTable.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_REFTABLE_H
2 #define GAUDIKERNEL_REFTABLE_H 1
3 
4 // Include files
6 #include "GaudiKernel/HashMap.h" // Cannot use maps through sharable images....
7 #include "GaudiKernel/Kernel.h"
8 #include "GaudiKernel/SmartRef.h"
11 
12 // Externals
13 static const CLID CLID_RefTable1to1 = 300;
14 static const CLID CLID_RefTable1toN = 301;
15 
16 //------------------------------------------------------------------------------
17 //
18 // Implementation and definition of template classes
19 // RefTableBase, RefTable1to1, RefTable1toN
20 //
21 // Author : Markus Frank
22 //
23 //------------------------------------------------------------------------------
43 template <class FROM, class MAPENTRY>
44 class RefTableBase : public DataObject
45 {
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 
64 private:
66  TableType m_table;
72 
73 protected:
75  bool insertMapElement( const KeyType* from, EntryType& to ) { return m_table.insert( from, to ); }
77  EntryType* i_reference( const KeyType* from )
78  {
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  {
85  auto i = m_table.find( from );
86  return i != m_table.end() ? &( i->second ) : nullptr;
87  }
88 
89 public:
91  RefTableBase( const CLID& clid, int len ) : m_clid( clid ), m_table( len ) {}
93  virtual ~RefTableBase() = default;
95  virtual void clear() { m_table.clear(); }
97  iterator begin() { return m_table.begin(); }
99  const_iterator begin() const { return m_table.begin(); }
101  iterator end() { return m_table.end(); }
103  const_iterator end() const { return m_table.end(); }
105  long size() const { return m_table.size(); }
107  void reserve( int len ) { m_table.reserve( len ); }
110  {
111  DataObject::serialize( s ) << m_table.size();
112  // for (TableType::const_iterator i = m_table.begin(), stop = m_table.end(); i != stop; i++ ) {
113  // SmartRef<KeyType> fromRef;
114  // fromRef = (KeyType*)(*i).first;
115  // s << fromRef(this);
116  // s << (*i).second(this);
117  //}
118  return s;
119  }
122  {
123  long siz;
124  DataObject::serialize( s ) >> siz;
125  m_table.reserve( siz );
126  // for ( long i = 0; i < siz; i++ ) {
127  // SmartRef<KeyType> fromRef;
128  // EntryType entry;
129  // s >> fromRef(this);
130  // s >> entry(this);
131  // insertMapElement( fromRef, entry);
132  //}
133  return s;
134  }
135 };
136 
137 template <class FROM, class TO>
138 class RefTable1to1 : public RefTableBase<FROM, SmartRef<TO>>
139 {
140 public:
142  RefTable1to1( const CLID& clid, int len = 16 ) : RefTableBase<FROM, SmartRef<TO>>( clid, len ) {}
144  ~RefTable1to1() override = default;
145 
147  virtual const CLID& clID() const { return m_clid; }
149  bool insert( const FROM* from, TO* to ) { return insertMapElement( from, EntryType( to ) ); }
151  bool insert( const FROM* from, const EntryType& to )
152  {
153  // We MUST check the environment of the smart pointer!
154  if ( 0 != to.data() || StreamBuffer::INVALID != to.hintID() ) {
155  return insertMapElement( from, EntryType( to ) );
156  }
157  return false;
158  }
160  TO* reference( const FROM* from )
161  {
162  EntryType* e = i_reference( from );
163  return ( 0 == e ) ? 0 : ( *e );
164  }
165 
167  const TO* reference( const FROM* from ) const
168  {
169  auto e = i_reference( from );
170  return e ? *e : nullptr;
171  }
172 
174  bool isReferenced( const FROM* from, const TO* to ) const
175  {
176  auto e = i_reference( from );
177  return e && ( *e == to );
178  }
180  bool isReferenced( const FROM* from, const EntryType& to )
181  {
182  const EntryType* e = i_reference( from );
183  return assoc && ( *e != to ) && ( e->target() == to.target() );
184  }
185 };
186 
187 template <class FROM, class TO>
188 class RefTable1toN : public RefTableBase<FROM, SmartRefVector<TO>>
189 {
190 public:
192  RefTable1toN( const CLID& clid, int len = 16 ) : RefTableBase<FROM, SmartRefVector<TO>>( clid, len ) {}
194  ~RefTable1toN() override = default;
195 
197  virtual const CLID& clID() const { return m_clid; }
199  bool insert( const FROM* from, TO* to )
200  {
201  EntryType* entry = i_reference( from );
202  if ( 0 == entry ) {
203  bool result = insertMapElement( from, EntryType() );
204  EntryType* newEntry = i_reference( from );
205  if ( !( 0 == newEntry ) ) {
206  newEntry->push_back( SmartRef<TO>( to ) );
207  return true;
208  }
209  return false;
210  }
211  entry->push_back( SmartRef<TO>( to ) );
212  return true;
213  }
215  bool insert( const FROM* from, const SmartRef<TO>& to )
216  {
217  EntryType* entry = i_reference( from );
218  if ( 0 == entry ) {
219  bool result = insertMapElement( from, EntryType() );
220  EntryType* newEntry = i_reference( from );
221  if ( !( 0 == newEntry ) ) {
222  newEntry->push_back( to );
223  return true;
224  }
225  return false;
226  }
227  entry->push_back( to );
228  return true;
229  }
231  bool insert( const FROM* from, const EntryType& to )
232  {
233  return insertMapElement( from, const_cast<EntryType&>( to ) );
234  }
236  EntryType& reference( const FROM* from )
237  {
238  static EntryType empty;
239  EntryType* e = i_reference( from );
240  return e ? *e : empty;
241  }
243  const EntryType& reference( const FROM* from ) const
244  {
245  static const EntryType empty;
246  EntryType* e = i_reference( from );
247  return e ? *e : empty;
248  }
250  bool isReferenced( const FROM* from, const EntryType& to )
251  {
252  auto e = i_reference( from );
253  return e && ( *e == to );
254  }
256  bool isReferenced( const FROM* from, const TO* to ) { return isReferenced( from, SmartRef<TO>( to ) ); }
258  bool isReferenced( const FROM* from, const SmartRef<TO>& to )
259  {
260  const EntryType* e = i_reference( from );
261  return assoc && std::find( e->begin(), e->end(), to ) != e->end();
262  }
263 };
264 
265 #endif // GAUDIKERNEL_REFTABLE_H
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:199
iterator begin()
Start of direct access iterator.
Definition: RefTable.h:97
bool isReferenced(const FROM *from, const TO *to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:256
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:121
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:279
const EntryType * i_reference(const KeyType *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:83
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:99
T end(T...args)
TO * reference(const FROM *from)
Find Reference from it&#39;s source entry.
Definition: RefTable.h:160
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:180
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:147
const EntryType & reference(const FROM *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:243
Kernel objects: SmartRefVector.
EntryType * i_reference(const KeyType *from)
Find Reference from it&#39;s source entry.
Definition: RefTable.h:77
FROM KeyType
Definition: RefTable.h:48
bool insert(const FROM *from, const EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:231
const TO * reference(const FROM *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:167
T push_back(T...args)
std::pair< iterator, bool > insert(ValueType &&val)
Definition: Map.h:174
void reserve(int len)
Size of References.
Definition: RefTable.h:107
long size() const
Size of References.
Definition: RefTable.h:105
iterator end()
Definition: Map.h:134
TableType::iterator iterator
Definition of map iterator.
Definition: RefTable.h:58
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:167
iterator end()
End of direct access iterator.
Definition: RefTable.h:101
RefTableBase(const CLID &clid, int len)
Constructors.
Definition: RefTable.h:91
iterator find(const key_type &key)
Definition: Map.h:151
RefTable1toN(const CLID &clid, int len=16)
Standard Constructor.
Definition: RefTable.h:192
const_iterator end() const
End of direct access iterator (CONST)
Definition: RefTable.h:103
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
virtual void clear()
Clear Reference map.
Definition: RefTable.h:95
iterator begin()
Definition: Map.h:133
GaudiUtils::HashMap< const void *, EntryType > TableType
Define Reference map.
Definition: RefTable.h:56
T find(T...args)
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: RefTable.h:109
template <class FROM, class TO, class MAPENTRY> class RefTable
Definition: RefTable.h:44
TableType m_table
Reference map.
Definition: RefTable.h:66
T begin(T...args)
long hintID() const
Access hint id:
Definition: SmartRef.h:159
bool isReferenced(const FROM *from, const SmartRef< TO > &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:258
bool insert(const FROM *from, const SmartRef< TO > &to)
Insert new Entry into Reference container.
Definition: RefTable.h:215
RefTable1to1(const CLID &clid, int len=16)
Standard Constructor.
Definition: RefTable.h:142
bool insert(const FROM *from, const EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:151
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:149
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:197
string s
Definition: gaudirun.py:253
void clear()
Definition: Map.h:195
MAPENTRY EntryType
Definition: RefTable.h:50
bool insertMapElement(const KeyType *from, EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:75
SmartRef< KeyType > m_fromRef
This is a completely useless entry, but the compiler wants it to be instantiated before the serialize...
Definition: RefTable.h:71
EntryType & reference(const FROM *from)
Find Reference from it&#39;s source entry.
Definition: RefTable.h:236
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:29
bool isReferenced(const FROM *from, const TO *to) const
Check if two entries are associated to each other.
Definition: RefTable.h:174
virtual ~RefTableBase()=default
Destructor.
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:250
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:199