All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RefTable.h
Go to the documentation of this file.
1 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/GaudiKernel/RefTable.h,v 1.6 2006/02/06 13:22:20 hmd Exp $
2 #ifndef GAUDIKERNEL_REFTABLE_H
3 #define GAUDIKERNEL_REFTABLE_H 1
4 
5 
6 // Include files
7 #include "GaudiKernel/Kernel.h"
11 #include "GaudiKernel/SmartRef.h"
12 #include "GaudiKernel/HashMap.h" // Cannot use maps through sharable images....
13 
14 // Externals
15 static const CLID CLID_RefTable1to1 = 300;
16 static const CLID CLID_RefTable1toN = 301;
17 
18 
19 //------------------------------------------------------------------------------
20 //
21 // Implementation and definition of template classes
22 // RefTableBase, RefTable1to1, RefTable1toN
23 //
24 // Author : Markus Frank
25 //
26 //------------------------------------------------------------------------------
46 template <class FROM, class MAPENTRY> class RefTableBase : public DataObject {
47 public:
48  // Type of the key
49  typedef FROM KeyType;
50  // Type of map entries
51  typedef MAPENTRY EntryType;
52  // My own type
64 private:
72 
73 protected:
75  bool insertMapElement ( const KeyType* from, EntryType& to ) {
76  return m_table.insert( from, to );
77  }
79  EntryType* i_reference(const KeyType* from) {
80  iterator i = m_table.find( from );
81  if ( i != 0 ) {
82  return &((*i).second);
83  }
84  return 0;
85  }
87  const EntryType* i_reference(const KeyType* from) const {
88  const_iterator i = m_table.find( from );
89  if ( i != m_table.end() ) {
90  return &((*i).second);
91  }
92  return 0;
93  }
94 
95 public:
97  RefTableBase(const CLID& clid, int len) : m_clid(clid), m_table(len) {
98  }
100  virtual ~RefTableBase() {
101  clear();
102  }
104  virtual void clear() {
105  m_table.clear();
106  }
109  return m_table.begin();
110  }
113  return m_table.begin();
114  }
117  return m_table.end();
118  }
120  const_iterator end() const {
121  return m_table.end();
122  }
124  long size() const {
125  return m_table.size();
126  }
128  void reserve(int len) {
129  m_table.reserve(len);
130  }
132  virtual StreamBuffer& serialize( StreamBuffer& s ) const {
133  DataObject::serialize(s) << m_table.size();
134  //for (TableType::const_iterator i = m_table.begin(), stop = m_table.end(); i != stop; i++ ) {
135  // SmartRef<KeyType> fromRef;
136  // fromRef = (KeyType*)(*i).first;
137  // s << fromRef(this);
138  // s << (*i).second(this);
139  //}
140  return s;
141  }
144  long siz;
145  DataObject::serialize(s) >> siz;
146  m_table.reserve(siz);
147  //for ( long i = 0; i < siz; i++ ) {
148  // SmartRef<KeyType> fromRef;
149  // EntryType entry;
150  // s >> fromRef(this);
151  // s >> entry(this);
152  // insertMapElement( fromRef, entry);
153  //}
154  return s;
155  }
156 };
157 
158 template <class FROM, class TO> class RefTable1to1
159 : public RefTableBase< FROM , SmartRef<TO> > {
160 public:
162  RefTable1to1 (const CLID& clid, int len=16)
163  : RefTableBase< FROM , SmartRef<TO> >(clid, len)
164  {
165  }
167  virtual ~RefTable1to1() {
168  }
170  virtual const CLID& clID() const {
171  return m_clid;
172  }
174  bool insert ( const FROM* from, TO* to ) {
175  return insertMapElement(from, EntryType(to));
176  }
178  bool insert ( const FROM* from, const EntryType& to) {
179  // We MUST check the environment of the smart pointer!
180  if ( 0 != to.data() || StreamBuffer::INVALID != to.hintID() ) {
181  return insertMapElement(from, EntryType(to));
182  }
183  return false;
184  }
186  TO* reference(const FROM* from) {
187  EntryType* e = i_reference(from);
188  return (0 == e) ? 0 : (*e);
189  }
190 
192  const TO* reference(const FROM* from) const {
193  const EntryType* e = i_reference(from);
194  return (0 == e) ? 0 : (*e);
195  }
196 
198  bool isReferenced(const FROM* from, const TO* to ) {
199  const EntryType* e = i_reference(from);
200  return (e == 0) ? false : ((*e) == to);
201  }
203  bool isReferenced(const FROM* from, const EntryType& to ) {
204  const EntryType* e = i_reference(from);
205  return (assoc!=0) ? ((*e)!=to) ? (e->target()==to.target()) : false : false;
206  }
207 };
208 
209 template <class FROM, class TO> class RefTable1toN
210 : public RefTableBase< FROM , SmartRefVector<TO> > {
211 public:
213  RefTable1toN (const CLID& clid, int len=16)
214  : RefTableBase< FROM , SmartRefVector<TO> >(clid, len) {
215  }
217  virtual ~RefTable1toN() {
218  }
220  virtual const CLID& clID() const {
221  return m_clid;
222  }
224  bool insert ( const FROM* from, TO* to) {
225  EntryType* entry = i_reference(from);
226  if ( 0 == entry ) {
227  bool result = insertMapElement(from, EntryType());
228  EntryType* newEntry = i_reference(from);
229  if ( !( 0 == newEntry) ) {
230  newEntry->push_back( SmartRef<TO>(to) );
231  return true;
232  }
233  return false;
234  }
235  entry->push_back( SmartRef<TO>(to) );
236  return true;
237  }
239  bool insert ( const FROM* from, const SmartRef<TO>& to) {
240  EntryType* entry = i_reference(from);
241  if ( 0 == entry ) {
242  bool result = insertMapElement(from, EntryType());
243  EntryType* newEntry = i_reference(from);
244  if ( !(0 == newEntry) ) {
245  newEntry->push_back( to );
246  return true;
247  }
248  return false;
249  }
250  entry->push_back( to );
251  return true;
252  }
254  bool insert ( const FROM* from, const EntryType& to) {
255  return insertMapElement(from, const_cast<EntryType&>(to));
256  }
258  EntryType& reference(const FROM* from) {
259  static EntryType empty;
260  EntryType* e = i_reference(from);
261  return (0 == e) ? empty : *e;
262  }
264  const EntryType& reference(const FROM* from) const {
265  static EntryType empty;
266  EntryType* e = i_reference(from);
267  return (0 == e) ? empty : (*e);
268  }
270  bool isReferenced(const FROM* from, const EntryType& to ) {
271  const EntryType* e = i_reference(from);
272  return (0 == e) ? false : (*e == to);
273  }
275  bool isReferenced(const FROM* from, const TO* to ) {
276  return isReferenced(from, SmartRef<TO>(to));
277  }
279  bool isReferenced(const FROM* from, const SmartRef<TO>& to ) {
280  const EntryType* e = i_reference(from);
281  if ( 0 != assoc ) {
282  SmartRefVector<TO>::const_iterator i = std::find(e->begin(), e->end(), to);
283  return (i == e->end()) ? false : true;
284  }
285  return false;
286  }
287 };
288 
289 
290 #endif // GAUDIKERNEL_REFTABLE_H
291 
CLID m_clid
Class id of the reference table.
Definition: RefTable.h:63
RefTableBase< FROM, EntryType > BaseType
Definition: RefTable.h:53
virtual ~RefTableBase()
Destructor.
Definition: RefTable.h:100
size_type size() const
Definition: Map.h:180
iterator begin()
Start of direct access iterator.
Definition: RefTable.h:108
bool isReferenced(const FROM *from, const TO *to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:275
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:40
virtual StreamBuffer & serialize(StreamBuffer &s)
Serialize the object for reading.
Definition: RefTable.h:143
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:288
const EntryType * i_reference(const KeyType *from) const
Find Reference from it's source entry (CONST)
Definition: RefTable.h:87
GaudiUtils::HashMap< const void *, EntryType > TableType
Define Reference map.
Definition: RefTable.h:57
TableType::const_iterator const_iterator
Definition of map iterator (CONST)
Definition: RefTable.h:61
const_iterator begin() const
Start of direct access iterator (CONST)
Definition: RefTable.h:112
TO * reference(const FROM *from)
Find Reference from it's source entry.
Definition: RefTable.h:186
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:203
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:170
const EntryType & reference(const FROM *from) const
Find Reference from it's source entry (CONST)
Definition: RefTable.h:264
Kernel objects: SmartRefVector.
EntryType * i_reference(const KeyType *from)
Find Reference from it's source entry.
Definition: RefTable.h:79
FROM KeyType
Definition: RefTable.h:49
std::pair< iterator, bool > insert(const value_type &val)
Definition: Map.h:165
bool insert(const FROM *from, const EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:254
const TO * reference(const FROM *from) const
Find Reference from it's source entry (CONST)
Definition: RefTable.h:192
void reserve(int len)
Size of References.
Definition: RefTable.h:128
long size() const
Size of References.
Definition: RefTable.h:124
iterator end()
Definition: Map.h:131
TableType::iterator iterator
Definition of map iterator.
Definition: RefTable.h:59
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:163
iterator end()
End of direct access iterator.
Definition: RefTable.h:116
RefTableBase(const CLID &clid, int len)
Constructors.
Definition: RefTable.h:97
virtual ~RefTable1to1()
Standard Destructor.
Definition: RefTable.h:167
iterator find(const key_type &key)
Definition: Map.h:148
RefTable1toN(const CLID &clid, int len=16)
Standard Constructor.
Definition: RefTable.h:213
const_iterator end() const
End of direct access iterator (CONST)
Definition: RefTable.h:120
unsigned int CLID
Class ID definition.
Definition: ClassID.h:9
map_type::const_iterator const_iterator
Definition: Map.h:99
virtual void clear()
Clear Reference map.
Definition: RefTable.h:104
iterator begin()
Definition: Map.h:130
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: RefTable.h:132
template class RefTable
Definition: RefTable.h:46
TableType m_table
Reference map.
Definition: RefTable.h:66
virtual ~RefTable1toN()
Standard Destructor.
Definition: RefTable.h:217
long hintID() const
Access hint id:
Definition: SmartRef.h:147
bool isReferenced(const FROM *from, const SmartRef< TO > &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:279
bool insert(const FROM *from, const SmartRef< TO > &to)
Insert new Entry into Reference container.
Definition: RefTable.h:239
RefTable1to1(const CLID &clid, int len=16)
Standard Constructor.
Definition: RefTable.h:162
bool insert(const FROM *from, const EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:178
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:174
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: RefTable.h:220
string s
Definition: gaudirun.py:210
void clear()
Definition: Map.h:176
MAPENTRY EntryType
Definition: RefTable.h:51
bool insertMapElement(const KeyType *from, EntryType &to)
Insert new Entry into Reference container.
Definition: RefTable.h:75
bool isReferenced(const FROM *from, const TO *to)
Check if two entries are associated to each other.
Definition: RefTable.h:198
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's source entry.
Definition: RefTable.h:258
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
list i
Definition: ana.py:128
bool isReferenced(const FROM *from, const EntryType &to)
Check if two entries are Referenced to each other.
Definition: RefTable.h:270
bool insert(const FROM *from, TO *to)
Insert new Entry into Reference container.
Definition: RefTable.h:224