The Gaudi Framework  v30r1 (5d4f4ae2)
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 ) {}
143 
145  virtual const CLID& clID() const { return m_clid; }
147  bool insert( const FROM* from, TO* to ) { return insertMapElement( from, EntryType( to ) ); }
149  bool insert( const FROM* from, const EntryType& to )
150  {
151  // We MUST check the environment of the smart pointer!
152  if ( 0 != to.data() || StreamBuffer::INVALID != to.hintID() ) {
153  return insertMapElement( from, EntryType( to ) );
154  }
155  return false;
156  }
158  TO* reference( const FROM* from )
159  {
160  EntryType* e = i_reference( from );
161  return ( 0 == e ) ? 0 : ( *e );
162  }
163 
165  const TO* reference( const FROM* from ) const
166  {
167  auto e = i_reference( from );
168  return e ? *e : nullptr;
169  }
170 
172  bool isReferenced( const FROM* from, const TO* to ) const
173  {
174  auto e = i_reference( from );
175  return e && ( *e == to );
176  }
178  bool isReferenced( const FROM* from, const EntryType& to )
179  {
180  const EntryType* e = i_reference( from );
181  return assoc && ( *e != to ) && ( e->target() == to.target() );
182  }
183 };
184 
185 template <class FROM, class TO>
186 class RefTable1toN : public RefTableBase<FROM, SmartRefVector<TO>>
187 {
188 public:
190  RefTable1toN( const CLID& clid, int len = 16 ) : RefTableBase<FROM, SmartRefVector<TO>>( clid, len ) {}
191 
193  virtual const CLID& clID() const { return m_clid; }
195  bool insert( const FROM* from, TO* to )
196  {
197  EntryType* entry = i_reference( from );
198  if ( 0 == entry ) {
199  bool result = insertMapElement( from, EntryType() );
200  EntryType* newEntry = i_reference( from );
201  if ( !( 0 == newEntry ) ) {
202  newEntry->push_back( SmartRef<TO>( to ) );
203  return true;
204  }
205  return false;
206  }
207  entry->push_back( SmartRef<TO>( to ) );
208  return true;
209  }
211  bool insert( const FROM* from, const SmartRef<TO>& to )
212  {
213  EntryType* entry = i_reference( from );
214  if ( 0 == entry ) {
215  bool result = insertMapElement( from, EntryType() );
216  EntryType* newEntry = i_reference( from );
217  if ( !( 0 == newEntry ) ) {
218  newEntry->push_back( to );
219  return true;
220  }
221  return false;
222  }
223  entry->push_back( to );
224  return true;
225  }
227  bool insert( const FROM* from, const EntryType& to )
228  {
229  return insertMapElement( from, const_cast<EntryType&>( to ) );
230  }
232  EntryType& reference( const FROM* from )
233  {
234  static EntryType empty;
235  EntryType* e = i_reference( from );
236  return e ? *e : empty;
237  }
239  const EntryType& reference( const FROM* from ) const
240  {
241  static const EntryType empty;
242  EntryType* e = i_reference( from );
243  return e ? *e : empty;
244  }
246  bool isReferenced( const FROM* from, const EntryType& to )
247  {
248  auto e = i_reference( from );
249  return e && ( *e == to );
250  }
252  bool isReferenced( const FROM* from, const TO* to ) { return isReferenced( from, SmartRef<TO>( to ) ); }
254  bool isReferenced( const FROM* from, const SmartRef<TO>& to )
255  {
256  const EntryType* e = i_reference( from );
257  return assoc && std::find( e->begin(), e->end(), to ) != e->end();
258  }
259 };
260 
261 #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:252
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:158
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:178
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:145
const EntryType & reference(const FROM *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:239
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:227
const TO * reference(const FROM *from) const
Find Reference from it&#39;s source entry (CONST)
Definition: RefTable.h:165
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:190
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:254
bool insert(const FROM *from, const SmartRef< TO > &to)
Insert new Entry into Reference container.
Definition: RefTable.h:211
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:149
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:147
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:193
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:232
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:172
virtual ~RefTableBase()=default
Destructor.
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:246
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:195