Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
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 
5 // Include files
6 #include "GaudiKernel/Kernel.h"
7 #include "GaudiKernel/ClassID.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;
42 
45 
48 
51 
52 #ifdef _WIN32
53  typedef typename std::vector<TYPE*>::_Tptr pointer;
55 #else
58 #endif
59 
60 public:
62  ObjectList() = default;
63 
64  ObjectList( const ObjectList<TYPE>& ) = delete;
65  ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
66 
68  ~ObjectList() override {
69  clear();
70  }
71 
73  const CLID& clID() const override {
75  }
76  static const CLID& classID() {
77  static CLID clid = TYPE::classID() + CLID_ObjectList;
78  return clid;
79  }
80 
83  return m_list.begin();
84  }
85 
88  return m_list.begin();
89  }
90 
93  return m_list.end();
94  }
95 
98  return m_list.end();
99  }
100 
104  return m_list.rbegin();
105  }
106 
109  return m_list.rbegin();
110  }
111 
114  return m_list.rend();
115  }
116 
119  return m_list.rend();
120  }
121 
124  typename ObjectList<TYPE>::size_type size () const {
125  // C++11: std::list::size is constant (pre C++11 it could be linear!)
126  return m_list.size();
127  }
129  typename ObjectList<TYPE>::size_type numberOfObjects() const override {
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( value->parent() ) {
166  const_cast<ObjectContainerBase*>(value->parent())->remove(value);
167  }
168  value->setParent(this);
169  m_list.push_back(value);
170  }
171 
173  long add(ContainedObject* pObject) override {
174  try {
175  auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>(pObject);
176  if ( ptr ) {
177  push_back(ptr);
178  return m_list.size()-1;
179  }
180  }
181  catch(...) {
182  }
183  return -1;
184  }
185 
188  void pop_back () {
189  auto position = m_list.back();
190  // Set the back pointer to 0 to avoid repetitional searching
191  // for the object in the container, and deleting the object
192  position->setParent (nullptr);
193  delete position;
194  // Removing from the container itself
195  m_list.pop_back();
196  }
197 
200  long remove(ContainedObject* value) override {
201  // Find the object of value value
202  long idx = 0;
203  auto iter = std::find_if( begin(), end(), [&](const ContainedObject* i) { return i == value; } );
204  if( iter == end() ) {
205  // Object cannot be released from the container,
206  // as it is not contained in it
207  return -1;
208  }
209 
210  // Set the back pointer to 0 to avoid repetitional searching
211  // for the object in the container and deleting the object
212  (*iter)->setParent(nullptr);
213  erase(iter);
214  return idx;
215  }
216 
219  typename ObjectList<TYPE>::const_reference value ) {
220  value->setParent(this);
221  return m_list.insert(position, value);
222  }
223 
225  void erase( typename ObjectList<TYPE>::iterator position ) {
226  if( (*position)->parent() ) {
227  // Set the back pointer to 0 to avoid repetitional searching
228  // for the object in the container, and deleting the object
229  (*position)->setParent(nullptr);
230  delete *position;
231  }
232  // Removing from the container itself
233  m_list.erase(position);
234  }
235 
237  void erase( typename ObjectList<TYPE>::iterator first,
238  typename ObjectList<TYPE>::iterator last ) {
239  for( auto iter = first; iter != last; ++iter ) {
240  // Set the back pointer to 0 to avoid repetitional searching
241  // for the object in the container, and deleting the object
242  (*iter)->setParent(nullptr);
243  delete *iter;
244  }
245  // Removing from the container itself
246  m_list.erase(first, last);
247  }
248 
250  void clear() {
251  erase(begin(), end());
252  }
253 
256  long index( const ContainedObject* obj ) const override {
257  auto i = std::find_if(begin(),end(),
258  [&](const ContainedObject* o)
259  { return o == obj; } );
260  return i!=end() ? std::distance( begin(), i ) : -1 ;
261  }
262 
264  ContainedObject* containedObject( long dist ) const override {
265  return dist < size() ? *std::next( begin(), dist ) : nullptr;
266  }
267 
269  std::ostream& fillStream( std::ostream& s ) const override {
270  s << "class ObjectList : size = "
271  << std::setw(12)
272  << size() << "\n";
273  // Output the base class
274  //ObjectContainerBase::fillStream(s);
275  if ( !empty() ) {
276  s << "\nContents of the STL list :";
277  long count = 0;
278  for( const auto& iter : m_list ) {
279  s << "\nIndex "
280  << std::setw(12)
281  << count++
282  << " of object of type "<< *iter;
283  }
284  }
285  return s;
286  }
287 
288 private:
291 };
292 
293 
294 #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:118
T empty(T...args)
T distance(T...args)
TYPE contained_type
Definition: ObjectList.h:40
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:129
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectList.h:188
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:218
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:87
T end(T...args)
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:124
ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:264
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:44
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:47
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:49
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition: ObjectList.h:73
T push_back(T...args)
std::list< TYPE * >::reference reference
Definition: ObjectList.h:43
~ObjectList() override
Destructor.
Definition: ObjectList.h:68
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:41
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:108
T next(T...args)
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:103
T erase(T...args)
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:97
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:82
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:113
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:159
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:154
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:46
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:290
T insert(T...args)
static const CLID & classID()
Definition: ObjectList.h:76
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:144
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectList.h:269
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:134
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:50
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:237
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:256
T back(T...args)
string s
Definition: gaudirun.py:245
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:250
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
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectList.h:173
ObjectContainerBase is the base class for Gaudi container classes.
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:57
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectList.h:225
STL class.
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:92
T rbegin(T...args)