The Gaudi Framework  v29r0 (ff2e7097)
ObjectList.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_OBJECTLIST_H
2 #define GAUDIKERNEL_OBJECTLIST_H
3 
4 // Include files
5 #include "GaudiKernel/ClassID.h"
6 #include "GaudiKernel/Kernel.h"
9 
10 #include <iomanip>
11 #include <list>
12 
13 // Definition of the CLID for this class defined in ClassID.h
14 // static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
15 
34 template <class TYPE>
35 class ObjectList : public ObjectContainerBase
36 {
37 
38 public:
39  typedef TYPE contained_type;
41 
44 
47 
50 
51 #ifdef _WIN32
52  typedef typename std::vector<TYPE*>::_Tptr pointer;
54 #else
57 #endif
58 
59 public:
61  ObjectList() = default;
62 
63  ObjectList( const ObjectList<TYPE>& ) = delete;
64  ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
65 
67  ~ObjectList() override { clear(); }
68 
70  const CLID& clID() const override { return ObjectList<TYPE>::classID(); }
71  static const CLID& classID()
72  {
73  static CLID clid = TYPE::classID() + CLID_ObjectList;
74  return clid;
75  }
76 
78  typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
79 
81  typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
82 
84  typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
85 
87  typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
88 
92 
95 
98 
101 
106  {
107  // C++11: std::list::size is constant (pre C++11 it could be linear!)
108  return m_list.size();
109  }
111  typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
112 
114  typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
115 
117  bool empty() const { return m_list.empty(); }
118 
121 
123  typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
124 
126  typename ObjectList<TYPE>::reference back() { return m_list.back(); }
127 
129  typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
130 
133  {
134  if ( value->parent() ) {
135  const_cast<ObjectContainerBase*>( value->parent() )->remove( value );
136  }
137  value->setParent( this );
138  m_list.push_back( value );
139  }
140 
142  long add( ContainedObject* pObject ) override
143  {
144  try {
145  auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
146  if ( ptr ) {
147  push_back( ptr );
148  return m_list.size() - 1;
149  }
150  } catch ( ... ) {
151  }
152  return -1;
153  }
154 
157  void pop_back()
158  {
159  auto position = m_list.back();
160  // Set the back pointer to 0 to avoid repetitional searching
161  // for the object in the container, and deleting the object
162  position->setParent( nullptr );
163  delete position;
164  // Removing from the container itself
165  m_list.pop_back();
166  }
167 
170  long remove( ContainedObject* value ) override
171  {
172  // Find the object of value value
173  long idx = 0;
174  auto iter = std::find_if( begin(), end(), [&]( const ContainedObject* i ) { return i == value; } );
175  if ( iter == end() ) {
176  // Object cannot be released from the container,
177  // as it is not contained in it
178  return -1;
179  }
180 
181  // Set the back pointer to 0 to avoid repetitional searching
182  // for the object in the container and deleting the object
183  ( *iter )->setParent( nullptr );
184  erase( iter );
185  return idx;
186  }
187 
190  typename ObjectList<TYPE>::const_reference value )
191  {
192  value->setParent( this );
193  return m_list.insert( position, value );
194  }
195 
197  void erase( typename ObjectList<TYPE>::iterator position )
198  {
199  if ( ( *position )->parent() ) {
200  // Set the back pointer to 0 to avoid repetitional searching
201  // for the object in the container, and deleting the object
202  ( *position )->setParent( nullptr );
203  delete *position;
204  }
205  // Removing from the container itself
206  m_list.erase( position );
207  }
208 
210  void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last )
211  {
212  for ( auto iter = first; iter != last; ++iter ) {
213  // Set the back pointer to 0 to avoid repetitional searching
214  // for the object in the container, and deleting the object
215  ( *iter )->setParent( nullptr );
216  delete *iter;
217  }
218  // Removing from the container itself
219  m_list.erase( first, last );
220  }
221 
223  void clear() { erase( begin(), end() ); }
224 
227  long index( const ContainedObject* obj ) const override
228  {
229  auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
230  return i != end() ? std::distance( begin(), i ) : -1;
231  }
232 
234  ContainedObject* containedObject( long dist ) const override
235  {
236  return dist < size() ? *std::next( begin(), dist ) : nullptr;
237  }
238 
240  std::ostream& fillStream( std::ostream& s ) const override
241  {
242  s << "class ObjectList : size = " << std::setw( 12 ) << size() << "\n";
243  // Output the base class
244  // ObjectContainerBase::fillStream(s);
245  if ( !empty() ) {
246  s << "\nContents of the STL list :";
247  long count = 0;
248  for ( const auto& iter : m_list ) {
249  s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
250  }
251  }
252  return s;
253  }
254 
255 private:
258 };
259 
260 #endif // GAUDI_OBJECTLIST_H
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:55
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:100
T empty(T...args)
T distance(T...args)
TYPE contained_type
Definition: ObjectList.h:39
size_t size_type
size_type, to conform the STL container interface
ObjectList< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectList.h:111
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectList.h:157
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:189
T rend(T...args)
T front(T...args)
ObjectList & operator=(const ObjectList< TYPE > &)=delete
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectList.h:81
T end(T...args)
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:117
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:105
ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:234
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:43
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:46
T setw(T...args)
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:48
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition: ObjectList.h:70
T push_back(T...args)
std::list< TYPE * >::reference reference
Definition: ObjectList.h:42
~ObjectList() override
Destructor.
Definition: ObjectList.h:67
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:40
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:94
T next(T...args)
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:91
T erase(T...args)
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:87
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:78
T pop_back(T...args)
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:97
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:129
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
STL class.
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectList.h:126
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:45
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:257
T insert(T...args)
static const CLID & classID()
Definition: ObjectList.h:71
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
T find_if(T...args)
ObjectList()=default
Constructors.
T size(T...args)
STL class.
ObjectList< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectList.h:120
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectList.h:240
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:114
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:49
T begin(T...args)
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:210
T max_size(T...args)
long index(const ContainedObject *obj) const override
Return distance of a given object from the beginning of its container It corresponds to the "index" (...
Definition: ObjectList.h:227
T back(T...args)
string s
Definition: gaudirun.py:253
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:223
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:132
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectList.h:142
ObjectContainerBase is the base class for Gaudi container classes.
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:56
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectList.h:197
STL class.
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:123
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:84
T rbegin(T...args)