Loading [MathJax]/jax/output/HTML-CSS/config.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 public:
38  typedef TYPE contained_type;
40 
43 
46 
49 
50 #ifdef _WIN32
51  typedef typename std::vector<TYPE*>::_Tptr pointer;
53 #else
56 #endif
57 
58 public:
60  ObjectList() = default;
61 
62  ObjectList( const ObjectList<TYPE>& ) = delete;
63  ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
64 
66  ~ObjectList() override { clear(); }
67 
69  const CLID& clID() const override { return ObjectList<TYPE>::classID(); }
70  static const CLID& classID() {
71  static CLID clid = TYPE::classID() + CLID_ObjectList;
72  return clid;
73  }
74 
76  typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
77 
79  typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
80 
82  typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
83 
85  typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
86 
90 
93 
96 
99 
104  // C++11: std::list::size is constant (pre C++11 it could be linear!)
105  return m_list.size();
106  }
108  typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
109 
111  typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
112 
114  bool empty() const { return m_list.empty(); }
115 
118 
120  typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
121 
123  typename ObjectList<TYPE>::reference back() { return m_list.back(); }
124 
126  typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
127 
130  if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
131  value->setParent( this );
132  m_list.push_back( value );
133  }
134 
136  long add( ContainedObject* pObject ) override {
137  try {
138  auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
139  if ( ptr ) {
140  push_back( ptr );
141  return m_list.size() - 1;
142  }
143  } catch ( ... ) {}
144  return -1;
145  }
146 
149  void pop_back() {
150  auto position = m_list.back();
151  // Set the back pointer to 0 to avoid repetitional searching
152  // for the object in the container, and deleting the object
153  position->setParent( nullptr );
154  delete position;
155  // Removing from the container itself
156  m_list.pop_back();
157  }
158 
161  long remove( ContainedObject* value ) override {
162  // Find the object of value value
163  long idx = 0;
164  auto iter = std::find_if( begin(), end(), [&]( const ContainedObject* i ) { return i == value; } );
165  if ( iter == end() ) {
166  // Object cannot be released from the container,
167  // as it is not contained in it
168  return -1;
169  }
170 
171  // Set the back pointer to 0 to avoid repetitional searching
172  // for the object in the container and deleting the object
173  ( *iter )->setParent( nullptr );
174  erase( iter );
175  return idx;
176  }
177 
180  typename ObjectList<TYPE>::const_reference value ) {
181  value->setParent( this );
182  return m_list.insert( position, value );
183  }
184 
186  void erase( typename ObjectList<TYPE>::iterator position ) {
187  if ( ( *position )->parent() ) {
188  // Set the back pointer to 0 to avoid repetitional searching
189  // for the object in the container, and deleting the object
190  ( *position )->setParent( nullptr );
191  delete *position;
192  }
193  // Removing from the container itself
194  m_list.erase( position );
195  }
196 
198  void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last ) {
199  for ( auto iter = first; iter != last; ++iter ) {
200  // Set the back pointer to 0 to avoid repetitional searching
201  // for the object in the container, and deleting the object
202  ( *iter )->setParent( nullptr );
203  delete *iter;
204  }
205  // Removing from the container itself
206  m_list.erase( first, last );
207  }
208 
210  void clear() { erase( begin(), end() ); }
211 
214  long index( const ContainedObject* obj ) const override {
215  auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
216  return i != end() ? std::distance( begin(), i ) : -1;
217  }
218 
220  ContainedObject* containedObject( long dist ) const override {
221  return dist < size() ? *std::next( begin(), dist ) : nullptr;
222  }
223 
225  std::ostream& fillStream( std::ostream& s ) const override {
226  s << "class ObjectList : size = " << std::setw( 12 ) << size() << "\n";
227  // Output the base class
228  // ObjectContainerBase::fillStream(s);
229  if ( !empty() ) {
230  s << "\nContents of the STL list :";
231  long count = 0;
232  for ( const auto& iter : m_list ) {
233  s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
234  }
235  }
236  return s;
237  }
238 
239 private:
242 };
243 
244 #endif // GAUDI_OBJECTLIST_H
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:54
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:98
T empty(T...args)
T distance(T...args)
TYPE contained_type
Definition: ObjectList.h:38
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:108
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectList.h:149
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:179
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:79
T end(T...args)
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:114
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:103
ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:220
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:42
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:45
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:47
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition: ObjectList.h:69
T push_back(T...args)
std::list< TYPE * >::reference reference
Definition: ObjectList.h:41
~ObjectList() override
Destructor.
Definition: ObjectList.h:66
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:39
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:92
T next(T...args)
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:89
T erase(T...args)
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:85
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:76
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:95
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:126
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:123
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:44
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:241
T insert(T...args)
static const CLID & classID()
Definition: ObjectList.h:70
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:117
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectList.h:225
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:111
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:48
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:198
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:214
T back(T...args)
string s
Definition: gaudirun.py:312
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:210
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:129
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectList.h:136
ObjectContainerBase is the base class for Gaudi container classes.
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:55
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectList.h:186
STL class.
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:120
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:82
T rbegin(T...args)