ObjectList.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_OBJECTLIST_H
2 #define GAUDIKERNEL_OBJECTLIST_H
3 
4 
5 // Include files
6 #include "GaudiKernel/Kernel.h"
7 #include "GaudiKernel/ClassID.h"
8 #include "GaudiKernel/StreamBuffer.h"
9 #include "GaudiKernel/ObjectContainerBase.h"
10 
11 #include <list>
12 #include <iomanip>
13 
14 
15 // Definition of the CLID for this class defined in ClassID.h
16 // static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
17 
36 template <class TYPE>
37 class ObjectList : public ObjectContainerBase {
38 
39 public:
40  typedef TYPE contained_type;
41  typedef typename std::list<TYPE*>::value_type value_type;
42 
44  typedef typename std::list<TYPE*>::const_reference const_reference;
45 
46  typedef typename std::list<TYPE*>::iterator iterator;
47  typedef typename std::list<TYPE*>::const_iterator const_iterator;
48 
49  typedef typename std::list<TYPE*>::reverse_iterator reverse_iterator;
50  typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator;
51 
52 #ifdef _WIN32
53  typedef typename std::vector<TYPE*>::_Tptr pointer;
54  typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
55 #else
56  typedef typename std::vector<TYPE*>::pointer pointer;
57  typedef typename std::vector<TYPE*>::const_pointer const_pointer;
58 #endif
59 
60 public:
63  : m_list(0) { }
66  : m_list(value.m_list) { }
67 
69  virtual ~ObjectList() {
70  clear();
71  }
72 
74  virtual const CLID& clID() const {
76  }
77  static const CLID& classID() {
78  static CLID clid = TYPE::classID() + CLID_ObjectList;
79  return clid;
80  }
81 
84  return m_list.begin();
85  }
86 
89  return m_list.begin();
90  }
91 
94  return m_list.end();
95  }
96 
99  return m_list.end();
100  }
101 
105  return m_list.rbegin();
106  }
107 
110  return m_list.rbegin();
111  }
112 
115  return m_list.rend();
116  }
117 
120  return m_list.rend();
121  }
122 
125  typename ObjectList<TYPE>::size_type size () const {
126  return m_list.size();
127  }
129  virtual typename ObjectList<TYPE>::size_type numberOfObjects() const {
130  return m_list.size();
131  }
132 
135  return m_list.max_size();
136  }
137 
139  bool empty () const {
140  return m_list.empty();
141  }
142 
145  return m_list.front();
146  }
147 
150  return m_list.front();
151  }
152 
155  return m_list.back();
156  }
157 
160  return m_list.back();
161  }
162 
165  if( 0 != value->parent() ) {
166  const_cast<ObjectContainerBase*>(value->parent())->remove(value);
167  }
168  value->setParent(this);
169  m_list.push_back(value);
170  }
171 
173  virtual long add(ContainedObject* pObject) {
174  try {
175  typename ObjectList<TYPE>::value_type ptr =
176  dynamic_cast<typename ObjectList<TYPE>::value_type>(pObject);
177  if ( 0 != ptr ) {
178  push_back(ptr);
179  return m_list.size()-1;
180  }
181  }
182  catch(...) {
183  }
184  return -1;
185  }
186 
189  void pop_back () {
190  typename ObjectList<TYPE>::value_type position = m_list.back();
191  // Set the back pointer to 0 to avoid repetitional searching
192  // for the object in the container, and deleting the object
193  position->setParent (0);
194  delete position;
195  // Removing from the container itself
196  m_list.pop_back();
197  }
198 
201  virtual long remove(ContainedObject* value) {
202  // Find the object of value value
203  long idx = 0;
204  typename ObjectList<TYPE>::iterator iter;
205  for( iter = begin(); iter != end(); iter++, idx++ ) {
206  if( value == *iter ) {
207  break;
208  }
209  }
210  if( end() == iter ) {
211  // Object cannot be released from the container,
212  // as it is not contained in it
213  return -1;
214  }
215  else {
216  // Set the back pointer to 0 to avoid repetitional searching
217  // for the object in the container and deleting the object
218  (*iter)->setParent (0);
219  erase(iter);
220  return idx;
221  }
222  }
223 
227  value->setParent(this);
228  typename ObjectList<TYPE>::iterator i = m_list.insert(position, value);
229  return i;
230  }
231 
233  void erase( typename ObjectList<TYPE>::iterator position ) {
234  if( 0 != (*position)->parent() ) {
235  // Set the back pointer to 0 to avoid repetitional searching
236  // for the object in the container, and deleting the object
237  (*position)->setParent (0);
238  delete *position;
239  }
240  // Removing from the container itself
241  m_list.erase(position);
242  }
243 
245  void erase( typename ObjectList<TYPE>::iterator first,
246  typename ObjectList<TYPE>::iterator last ) {
247  for( typename ObjectList<TYPE>::iterator iter = first; iter != last; iter++ ) {
248  // Set the back pointer to 0 to avoid repetitional searching
249  // for the object in the container, and deleting the object
250  (*iter)->setParent (0);
251  delete *iter;
252  }
253  // Removing from the container itself
254  m_list.erase(first, last);
255  }
256 
258  void clear() {
259  erase(begin(), end());
260  }
261 
264  virtual long index( const ContainedObject* obj ) const {
265  long i = 0;
266  typename ObjectList<TYPE>::const_iterator iter;
267  for( iter = begin(); iter != end(); iter++ ) {
268  if( *iter == obj ) {
269  return i;
270  }
271  i++;
272  }
273  return -1;
274  }
275 
277  virtual ContainedObject* containedObject( long dist ) const {
278  long i = 0;
279  typename ObjectList<TYPE>::const_iterator iter;
280  for( iter = begin(); iter != end(); iter++ ) {
281  if( dist == i ) {
282  return *iter;
283  }
284  i++;
285  }
286  return 0;
287  }
288 
290  virtual std::ostream& fillStream( std::ostream& s ) const {
291  s << "class ObjectList : size = "
292  << std::setw(12)
293  << size() << "\n";
294  // Output the base class
295  //ObjectContainerBase::fillStream(s);
296  if ( 0 != size() ) {
297  s << "\nContents of the STL list :";
298  long count = 0;
299  typename ObjectList<TYPE>::const_iterator iter;
300  for( iter = m_list.begin(); iter != m_list.end(); iter++, count++ ) {
301  s << "\nIndex "
302  << std::setw(12)
303  << count
304  << " of object of type "<< **iter;
305  }
306  }
307  return s;
308  }
309 
310 private:
311 
313  std::list<TYPE*> m_list;
314 };
315 
316 
317 #endif // GAUDI_OBJECTLIST_H
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:56
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:119
virtual std::ostream & fillStream(std::ostream &s) const
Fill the output stream (ASCII)
Definition: ObjectList.h:290
TYPE contained_type
Definition: ObjectList.h:40
size_t size_type
size_type, to conform the STL container interface
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectList.h:189
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:225
ObjectList()
Constructors.
Definition: ObjectList.h:62
virtual ContainedObject * containedObject(long dist) const
Return const pointer to an object of a given distance.
Definition: ObjectList.h:277
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectList.h:88
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:139
ObjectList< TYPE >::size_type size() const
Return the size of the container Size means the number of objects stored in the container, independently on the amount of information stored in each object.
Definition: ObjectList.h:125
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:44
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:47
ObjectList is one of the basic Gaudi container classes capable of being registered in Data Stores...
std::list< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectList.h:49
std::list< TYPE * >::reference reference
Definition: ObjectList.h:43
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:41
virtual ObjectList< TYPE >::size_type numberOfObjects() const
The same as size(), return number of objects in the container.
Definition: ObjectList.h:129
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:109
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:104
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:98
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:83
virtual long index(const ContainedObject *obj) const
Return distance of a given object from the beginning of its container It corresponds to the "index" (...
Definition: ObjectList.h:264
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:114
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:159
virtual const CLID & clID() const
Retrieve pointer to class definition structure.
Definition: ObjectList.h:74
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectList.h:154
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:46
static const CLID & classID()
Definition: ObjectList.h:77
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
ObjectList< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectList.h:144
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:134
ObjectList(const ObjectList< TYPE > &value)
Copy Constructor.
Definition: ObjectList.h:65
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:50
virtual ~ObjectList()
Destructor.
Definition: ObjectList.h:69
virtual long add(ContainedObject *pObject)
Add an object to the container.
Definition: ObjectList.h:173
void erase(typename ObjectList< TYPE >::iterator first, typename ObjectList< TYPE >::iterator last)
Erase the range [first, last) from the container. The removed object will be deleted.
Definition: ObjectList.h:245
string s
Definition: gaudirun.py:246
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:258
void push_back(typename ObjectList< TYPE >::const_reference value)
push_back = append = insert a new element at the end of the container
Definition: ObjectList.h:164
ObjectContainerBase is the base class for Gaudi container classes.
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:57
list i
Definition: ana.py:128
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectList.h:233
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:313
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:149
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:93