| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
8 #include "GaudiKernel/DataObject.h"
9 #include "GaudiKernel/StreamBuffer.h"
10 #include "GaudiKernel/SmartRefVector.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
53 typedef RefTableBase<FROM, EntryType> BaseType;
57 typedef GaudiUtils::HashMap< const void* , EntryType > TableType;
59 typedef TableType::iterator iterator;
61 typedef TableType::const_iterator const_iterator;
63 CLID m_clid;
64 private:
66 TableType m_table;
71 SmartRef<KeyType> m_fromRef;
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 }
108 iterator begin() {
109 return m_table.begin();
110 }
112 const_iterator begin() const {
113 return m_table.begin();
114 }
116 iterator end() {
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 }
143 virtual StreamBuffer& serialize( StreamBuffer& s ) {
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