All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ObjectList.h
Go to the documentation of this file.
1 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/GaudiKernel/ObjectList.h,v 1.10 2008/10/09 16:46:49 marcocle Exp $
2 #ifndef GAUDIKERNEL_OBJECTLIST_H
3 #define GAUDIKERNEL_OBJECTLIST_H
4 
5 
6 // Include files
7 #include "GaudiKernel/Kernel.h"
8 #include "GaudiKernel/ClassID.h"
11 
12 #include <list>
13 #include <iomanip>
14 
15 
16 // Definition of the CLID for this class defined in ClassID.h
17 // static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
18 
37 template <class TYPE>
38 class ObjectList : public ObjectContainerBase {
39 
40 public:
41  typedef TYPE contained_type;
42  typedef typename std::list<TYPE*>::value_type value_type;
43 
45  typedef typename std::list<TYPE*>::const_reference const_reference;
46 
47  typedef typename std::list<TYPE*>::iterator iterator;
48  typedef typename std::list<TYPE*>::const_iterator const_iterator;
49 
50  typedef typename std::list<TYPE*>::reverse_iterator reverse_iterator;
51  typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator;
52 
53 #ifdef _WIN32
54  typedef typename std::vector<TYPE*>::_Tptr pointer;
55  typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
56 #else
57  typedef typename std::vector<TYPE*>::pointer pointer;
58  typedef typename std::vector<TYPE*>::const_pointer const_pointer;
59 #endif
60 
61 public:
64  : m_list(0) { }
67  : m_list(value.m_list) { }
68 
70  virtual ~ObjectList() {
71  clear();
72  }
73 
75  virtual const CLID& clID() const {
77  }
78  static const CLID& classID() {
79  static CLID clid = TYPE::classID() + CLID_ObjectList;
80  return clid;
81  }
82 
85  return m_list.begin();
86  }
87 
90  return m_list.begin();
91  }
92 
95  return m_list.end();
96  }
97 
100  return m_list.end();
101  }
102 
106  return m_list.rbegin();
107  }
108 
111  return m_list.rbegin();
112  }
113 
116  return m_list.rend();
117  }
118 
121  return m_list.rend();
122  }
123 
126  typename ObjectList<TYPE>::size_type size () const {
127  return m_list.size();
128  }
130  virtual typename ObjectList<TYPE>::size_type numberOfObjects() const {
131  return m_list.size();
132  }
133 
136  return m_list.max_size();
137  }
138 
140  bool empty () const {
141  return m_list.empty();
142  }
143 
146  return m_list.front();
147  }
148 
151  return m_list.front();
152  }
153 
156  return m_list.back();
157  }
158 
161  return m_list.back();
162  }
163 
166  if( 0 != value->parent() ) {
167  const_cast<ObjectContainerBase*>(value->parent())->remove(value);
168  }
169  value->setParent(this);
170  m_list.push_back(value);
171  }
172 
174  virtual long add(ContainedObject* pObject) {
175  try {
176  typename ObjectList<TYPE>::value_type ptr =
177  dynamic_cast<typename ObjectList<TYPE>::value_type>(pObject);
178  if ( 0 != ptr ) {
179  push_back(ptr);
180  return m_list.size()-1;
181  }
182  }
183  catch(...) {
184  }
185  return -1;
186  }
187 
190  void pop_back () {
191  typename ObjectList<TYPE>::value_type position = m_list.back();
192  // Set the back pointer to 0 to avoid repetitional searching
193  // for the object in the container, and deleting the object
194  position->setParent (0);
195  delete position;
196  // Removing from the container itself
197  m_list.pop_back();
198  }
199 
202  virtual long remove(ContainedObject* value) {
203  // Find the object of value value
204  long idx = 0;
205  typename ObjectList<TYPE>::iterator iter;
206  for( iter = begin(); iter != end(); iter++, idx++ ) {
207  if( value == *iter ) {
208  break;
209  }
210  }
211  if( end() == iter ) {
212  // Object cannot be released from the container,
213  // as it is not contained in it
214  return -1;
215  }
216  else {
217  // Set the back pointer to 0 to avoid repetitional searching
218  // for the object in the container and deleting the object
219  (*iter)->setParent (0);
220  erase(iter);
221  return idx;
222  }
223  }
224 
228  value->setParent(this);
229  typename ObjectList<TYPE>::iterator i = m_list.insert(position, value);
230  return i;
231  }
232 
234  void erase( typename ObjectList<TYPE>::iterator position ) {
235  if( 0 != (*position)->parent() ) {
236  // Set the back pointer to 0 to avoid repetitional searching
237  // for the object in the container, and deleting the object
238  (*position)->setParent (0);
239  delete *position;
240  }
241  // Removing from the container itself
242  m_list.erase(position);
243  }
244 
246  void erase( typename ObjectList<TYPE>::iterator first,
247  typename ObjectList<TYPE>::iterator last ) {
248  for( typename ObjectList<TYPE>::iterator iter = first; iter != last; iter++ ) {
249  // Set the back pointer to 0 to avoid repetitional searching
250  // for the object in the container, and deleting the object
251  (*iter)->setParent (0);
252  delete *iter;
253  }
254  // Removing from the container itself
255  m_list.erase(first, last);
256  }
257 
259  void clear() {
260  erase(begin(), end());
261  }
262 
265  virtual long index( const ContainedObject* obj ) const {
266  long i = 0;
267  typename ObjectList<TYPE>::const_iterator iter;
268  for( iter = begin(); iter != end(); iter++ ) {
269  if( *iter == obj ) {
270  return i;
271  }
272  i++;
273  }
274  return -1;
275  }
276 
278  virtual ContainedObject* containedObject( long dist ) const {
279  long i = 0;
280  typename ObjectList<TYPE>::const_iterator iter;
281  for( iter = begin(); iter != end(); iter++ ) {
282  if( dist == i ) {
283  return *iter;
284  }
285  i++;
286  }
287  return 0;
288  }
289 
291  virtual std::ostream& fillStream( std::ostream& s ) const {
292  s << "class ObjectList : size = "
293  << std::setw(12)
294  << size() << "\n";
295  // Output the base class
296  //ObjectContainerBase::fillStream(s);
297  if ( 0 != size() ) {
298  s << "\nContents of the STL list :";
299  long count = 0;
300  typename ObjectList<TYPE>::const_iterator iter;
301  for( iter = m_list.begin(); iter != m_list.end(); iter++, count++ ) {
302  s << "\nIndex "
303  << std::setw(12)
304  << count
305  << " of object of type "<< **iter;
306  }
307  }
308  return s;
309  }
310 
311 private:
312 
314  std::list<TYPE*> m_list;
315 };
316 
317 
318 #endif // GAUDI_OBJECTLIST_H
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:57
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:120
virtual std::ostream & fillStream(std::ostream &s) const
Fill the output stream (ASCII)
Definition: ObjectList.h:291
TYPE contained_type
Definition: ObjectList.h:41
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:190
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:226
ObjectList()
Constructors.
Definition: ObjectList.h:63
virtual ContainedObject * containedObject(long dist) const
Return const pointer to an object of a given distance.
Definition: ObjectList.h:278
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectList.h:89
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:140
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:126
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:45
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:48
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:50
std::list< TYPE * >::reference reference
Definition: ObjectList.h:44
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:42
virtual ObjectList< TYPE >::size_type numberOfObjects() const
The same as size(), return number of objects in the container.
Definition: ObjectList.h:130
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:110
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:105
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:99
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:84
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:265
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:115
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:160
virtual const CLID & clID() const
Retrieve pointer to class definition structure.
Definition: ObjectList.h:75
unsigned int CLID
Class ID definition.
Definition: ClassID.h:9
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectList.h:155
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:47
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:314
static const CLID & classID()
Definition: ObjectList.h:78
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:145
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:135
ObjectList(const ObjectList< TYPE > &value)
Copy Constructor.
Definition: ObjectList.h:66
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:51
virtual ~ObjectList()
Destructor.
Definition: ObjectList.h:70
virtual long add(ContainedObject *pObject)
Add an object to the container.
Definition: ObjectList.h:174
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:246
string s
Definition: gaudirun.py:210
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:259
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:165
ObjectContainerBase is the base class for Gaudi container classes.
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:58
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:234
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:150
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:94