ATLAS Offline Software
Loading...
Searching...
No Matches
DataLinkVector.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef EVENTCONTAINERS_DATALINKVECTOR_H
6#define EVENTCONTAINERS_DATALINKVECTOR_H
16
17#include "GaudiKernel/DataObject.h"
18#include <vector>
19
20
31
32template < class DC >
33class DataLinkVector :public DataObject
34{
35public:
36
37 typedef DC value_type;
38 typedef DC* pointer;
39 typedef const DC* const_pointer;
40 typedef DC& reference;
41 typedef const DC& const_reference;
42
48 {
49 public:
51 DataHolder();
52
54 bool hasData() const;
55
58
60 operator const_pointer () const { return this->getDataPtr(); }
61
63 const_pointer cptr() const;
64
66 void reset();
67
69 void setDataPtr(const DC* obj);
70
72 pointer getDataPtr() const;
73
74 private:
75
77 };
78
79
82
87 class Entry
88 {
89 public:
91 Entry();
92
94 Entry& operator = (const Entry& e);
95
98
101
103 bool ownObject() const;
104
106 void setPreviousEntry(Entry* entry);
107
109 void setDataLink (const DataLinkT& link);
110
112 void setOwnObject(bool owns);
113
114 private:
117 // ownObject is true if this object should be deleted by DLV.
118 // May be false if object is managed by a data pool
120 };
121 typedef std::vector<Entry> EntryVector;
122 typedef typename EntryVector::iterator EntryVectorIt;
123
128 {
129 public:
131 iterator();
132
134 bool operator != ( const iterator it ) const;
135
137 bool operator == ( const iterator it ) const;
138
140 iterator& operator = ( const iterator it );
141
144
146 iterator operator ++ ( int );
147
150
153
155 bool hasData();
156
157 protected:
158 friend class DataLinkVector<DC>;
159
160 iterator( const MyType* dlv,
161 EntryVectorIt it );
162
163
166 void checkChain();
167
170
171 const MyType* m_dlv;
173 };
174
175 friend class iterator;
176
177
180
183
185 void init( unsigned int nbr );
186
188 iterator begin() const;
189
191 iterator end() const;
192
194 iterator find( IdentifierHash id ) const;
195
197 bool setObjectOwnership(IdentifierHash idhash, bool flag);
198
201 DataLinkT& operator [] ( int id );
202
206 virtual void cleanup();
207
209 int size() const;
210
211protected:
212
214 void chainEntry( Entry* e ) const;
215
217 mutable Entry* m_last;
218};
219
221
222template <class DC>
227
228template <class DC>
229bool
231{
232 return (m_obj != 0);
233}
234
235template <class DC>
238{
239 return this->getDataPtr();
240}
241
242template <class DC>
245{
246 return this->getDataPtr();
247}
248
249template <class DC>
250void
255
256template <class DC>
257void
259{
260 m_obj = const_cast<pointer>(obj);
261}
262
263template <class DC>
266{
267 return (m_obj);
268}
269
270template <class DC>
272 :
273 m_previousEntry(NULL),
274 m_ownObject(false)
275{
277}
278
279template <class DC>
282 m_dataLink = e.m_dataLink ;
283 m_previousEntry = e.m_previousEntry;
284 m_ownObject = e.m_ownObject;
285 return *this;
286}
287
288template <class DC>
294
295template <class DC>
301
302template <class DC>
303bool
308
309template <class DC>
310void
315
316template <class DC>
317void
322
323template <class DC>
324void
329
330
331
332template <class DC>
335
336
337template <class DC>
338bool
340{ return it.m_it != m_it; }
341
342template <class DC>
343bool
345{ return it.m_it == m_it; }
346
347template <class DC>
350{ m_dlv = it.m_dlv; m_it = it.m_it; return *this; }
351
352template <class DC>
356
357template <class DC>
361
362template <class DC>
366
367template <class DC>
371
372template <class DC>
373bool
375{
376 // check if DL is valid, and cash the state
377 checkChain(); // this entry has been touched.
378 Entry& e = *m_it;
379 bool DL_valid = e.dataLink().hasData();
380 return DL_valid;
381}
382
383template <class DC>
387
388// make sur entry is chained
389// If not, add entry to the chain otherwise do nothing because
390
391template <class DC>
392void
394{ if( m_it->previousEntry() == NULL ) m_dlv->chainEntry( &(*m_it) ) ; }
395
396// protected method for derived class to access m_vect
397
398template <class DC>
401{
402 return m_dlv->m_vect ;
403}
404
405// Default constructor
406template <class DC>
409
410// Default constructor
411template <class DC>
416
417// initialize vector with nbr DataLinks
418template <class DC>
419void
420DataLinkVector<DC>::init( unsigned int nbr )
421{
422 // fill in vector of Entry
423 m_vect.resize(nbr);
424}
425
426// return iterator on m_previousEntry entry
427template <class DC>
430{ return iterator( this, m_vect.begin() ); }
431
432// return iterator on after last entry
433template <class DC>
436{ return iterator( this, m_vect.end()); }
437
438// return iterator on the found entry or end() if out of range
439template <class DC>
442{ return ( (id >= m_vect.size() )
443 ?end():iterator(this,m_vect.begin()+id));}
444
445// set object ownership
446template <class DC>
447bool
449{
450 if ( idhash >= m_vect.size() )
451 return false;
452 Entry& e = *(m_vect.begin()+idhash);
453 e.setOwnObject(flag);
454 return true;
455}
456
457// return reference on DataLink
458// BEWARE: Doesn't check for boundary limits !
459template <class DC>
462{
463 Entry* e = &(m_vect[id]);
464 if( !e->previousEntry() )
465 chainEntry( e );
466 return e->dataLink();
467}
468
469// cleans up the DataLinkVector
470// goes trough all chained entries and call clear() method of DataLink
471// and clear chain of course.
472template <class DC>
473void
475{
476 Entry *e;
477 // assign last to e and check if it is not NULL
478 if( (e = m_last) )
479 {
480 // for chained entries
481 do {
482 Entry * tmp = e->previousEntry();
483 e->setPreviousEntry(NULL); // clear chain pointer
484 if (e->ownObject()) // delete object
485 delete (e->dataLink()).getDataPtr();
486 e->dataLink().reset(); // reset keeps it in identified state.
487 e->setOwnObject(false); // doesn't own object
488 e = tmp; // go to next entry
489 } while( e != m_last ); // until we end where we started
490 m_last = NULL; // clear chain hook
491 }
492}
493
494template <class DC>
495int
497 return m_vect.size();
498}
499
500template <class DC>
501void
503{
504 if( m_last == NULL ) {
505 e->setPreviousEntry(e);
506 m_last = e;
507 }
508 else {
509 e->setPreviousEntry(m_last->previousEntry());
510 m_last->setPreviousEntry(e);
511 }
512}
513
514
515#endif
CONT::iterator iterator
class DataHolder is a simple class which holds a pointer to an object of type DC.
pointer getDataPtr() const
access to pointer
void reset()
reset pointer to 0
const_pointer operator->() const
access to pointer
void setDataPtr(const DC *obj)
set pointer
const_pointer cptr() const
access to pointer
bool hasData() const
is pointer set
class Entry is a link in a chained list, used for efficient cleanup of vectors.
bool ownObject() const
ownObject is true if this object should be deleted by DLV.
void setDataLink(const DataLinkT &link)
set data link
DataLinkT & dataLink()
access to the DataLink
void setOwnObject(bool owns)
set object ownership
Entry * previousEntry()
access to next entry
Entry & operator=(const Entry &e)
assignement operator
void setPreviousEntry(Entry *entry)
set previous entry
class iterator - iterator over elements in DataLinkVector which manages the Entry objects
DataLinkT * operator->()
access to object by pointer
bool hasData()
test if DataHolder has data
DataLinkT & operator*()
access to object by ref
bool operator!=(const iterator it) const
comparison operator
EntryVector & entryVector()
protected method for derived class to access m_vect
iterator & operator++()
increment operator
bool operator==(const iterator it) const
comparison operator
iterator & operator=(const iterator it)
assignment operator
void checkChain()
make sure entry is chained If not, add entry to the chain otherwise do nothing because
void init(unsigned int nbr)
initialize vector with nbr DataLinks
void chainEntry(Entry *e) const
add pointed entry in chain of modified entries
DataLinkT & operator[](int id)
return reference on DataLink BEWARE: Doesn't check for boundary limits !
virtual void cleanup()
cleans up the DataLinkVector goes trough all chained entries and call clear() method of DataLink and ...
friend class iterator
DataLinkVector()
Default constructor.
std::vector< Entry > EntryVector
const DC & const_reference
iterator end() const
return iterator on after last entry
DataLinkVector< DC > MyType
iterator find(IdentifierHash id) const
return iterator on the found entry or end() if out of range
~DataLinkVector()
Default destructor.
bool setObjectOwnership(IdentifierHash idhash, bool flag)
set object ownership
int size() const
access to size of vector
EntryVector::iterator EntryVectorIt
iterator begin() const
return iterator on first entry
const DC * const_pointer
DataHolder DataLinkT
EntryVector m_vect
This is a "hash" representation of an Identifier.