The Gaudi Framework  v40r0 (475e45c1)
ObjectList.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
13 #include <GaudiKernel/ClassID.h>
14 #include <GaudiKernel/Kernel.h>
17 #include <iomanip>
18 #include <list>
19 
20 // Definition of the CLID for this class defined in ClassID.h
21 // static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
22 
41 template <class TYPE>
42 class ObjectList : public ObjectContainerBase {
43 
44 public:
45  typedef TYPE contained_type;
46  typedef typename std::list<TYPE*>::value_type value_type;
47 
49  typedef typename std::list<TYPE*>::const_reference const_reference;
50 
51  typedef typename std::list<TYPE*>::iterator iterator;
52  typedef typename std::list<TYPE*>::const_iterator const_iterator;
53 
54  typedef typename std::list<TYPE*>::reverse_iterator reverse_iterator;
55  typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator;
56 
57 #ifdef _WIN32
58  typedef typename std::vector<TYPE*>::_Tptr pointer;
59  typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
60 #else
61  typedef typename std::vector<TYPE*>::pointer pointer;
62  typedef typename std::vector<TYPE*>::const_pointer const_pointer;
63 #endif
64 
65 public:
66  ObjectList() = default;
67  ObjectList( const ObjectList<TYPE>& ) = delete;
68  ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
69 
70  ~ObjectList() override { clear(); }
71 
73  const CLID& clID() const override { return ObjectList<TYPE>::classID(); }
74  static const CLID& classID() {
75  static CLID clid = TYPE::classID() + CLID_ObjectList;
76  return clid;
77  }
78 
80  typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
81 
83  typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
84 
86  typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
87 
89  typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
90 
93  typename ObjectList<TYPE>::reverse_iterator rbegin() { return m_list.rbegin(); }
94 
96  typename ObjectList<TYPE>::const_reverse_iterator rbegin() const { return m_list.rbegin(); }
97 
99  typename ObjectList<TYPE>::reverse_iterator rend() { return m_list.rend(); }
100 
102  typename ObjectList<TYPE>::const_reverse_iterator rend() const { return m_list.rend(); }
103 
108  // C++11: std::list::size is constant (pre C++11 it could be linear!)
109  return m_list.size();
110  }
112  typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
113 
115  typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
116 
118  bool empty() const { return m_list.empty(); }
119 
121  typename ObjectList<TYPE>::reference front() { return m_list.front(); }
122 
124  typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
125 
127  typename ObjectList<TYPE>::reference back() { return m_list.back(); }
128 
130  typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
131 
134  if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
135  value->setParent( this );
136  m_list.push_back( value );
137  }
138 
140  long add( ContainedObject* pObject ) override {
141  try {
142  auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
143  if ( ptr ) {
144  push_back( ptr );
145  return m_list.size() - 1;
146  }
147  } catch ( ... ) {}
148  return -1;
149  }
150 
153  void pop_back() {
154  auto position = m_list.back();
155  // Set the back pointer to 0 to avoid repetitional searching
156  // for the object in the container, and deleting the object
157  position->setParent( nullptr );
158  delete position;
159  // Removing from the container itself
160  m_list.pop_back();
161  }
162 
165  long remove( ContainedObject* value ) override {
166  // Find the object of value value
167  long idx = 0;
168  auto iter = std::find_if( begin(), end(), [&]( const ContainedObject* i ) { return i == value; } );
169  if ( iter == end() ) {
170  // Object cannot be released from the container,
171  // as it is not contained in it
172  return -1;
173  }
174 
175  // Set the back pointer to 0 to avoid repetitional searching
176  // for the object in the container and deleting the object
177  ( *iter )->setParent( nullptr );
178  erase( iter );
179  return idx;
180  }
181 
184  typename ObjectList<TYPE>::const_reference value ) {
185  value->setParent( this );
186  return m_list.insert( position, value );
187  }
188 
190  void erase( typename ObjectList<TYPE>::iterator position ) {
191  if ( ( *position )->parent() ) {
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( nullptr );
195  delete *position;
196  }
197  // Removing from the container itself
198  m_list.erase( position );
199  }
200 
202  void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last ) {
203  for ( auto iter = first; iter != last; ++iter ) {
204  // Set the back pointer to 0 to avoid repetitional searching
205  // for the object in the container, and deleting the object
206  ( *iter )->setParent( nullptr );
207  delete *iter;
208  }
209  // Removing from the container itself
210  m_list.erase( first, last );
211  }
212 
214  void clear() { erase( begin(), end() ); }
215 
218  long index( const ContainedObject* obj ) const override {
219  auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
220  return i != end() ? std::distance( begin(), i ) : -1;
221  }
222 
224  const ContainedObject* containedObject( long dist ) const override {
225  return dist < size() ? *std::next( begin(), dist ) : nullptr;
226  }
228  ContainedObject* containedObject( long dist ) override {
229  return dist < size() ? *std::next( begin(), dist ) : nullptr;
230  }
231 
233  std::ostream& fillStream( std::ostream& s ) const override {
234  s << "class ObjectList : size = " << std::setw( 12 ) << size() << "\n";
235  // Output the base class
236  // ObjectContainerBase::fillStream(s);
237  if ( !empty() ) {
238  s << "\nContents of the STL list :";
239  long count = 0;
240  for ( const auto& iter : m_list ) {
241  s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
242  }
243  }
244  return s;
245  }
246 
247 private:
248  std::list<TYPE*> m_list;
249 };
ObjectList::contained_type
TYPE contained_type
Definition: ObjectList.h:45
ObjectList::rend
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:102
gaudirun.s
string s
Definition: gaudirun.py:346
ObjectList::index
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:218
ObjectList::rbegin
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:93
ClassID.h
ObjectList::ObjectList
ObjectList()=default
ObjectList::~ObjectList
~ObjectList() override
Definition: ObjectList.h:70
ObjectList::push_back
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:133
ObjectList::front
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:124
ObjectList::erase
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:202
ObjectList::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:214
ObjectList::end
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:86
StreamBuffer.h
ObjectList::pop_back
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectList.h:153
ObjectList::ObjectList
ObjectList(const ObjectList< TYPE > &)=delete
ObjectList::max_size
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:115
ObjectList::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectList.h:233
ObjectList::remove
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
Definition: ObjectList.h:165
ObjectList::reverse_iterator
std::list< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectList.h:54
ObjectList::m_list
std::list< TYPE * > m_list
Definition: ObjectList.h:248
ObjectList::insert
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:183
ObjectList::erase
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectList.h:190
ObjectList::operator=
ObjectList & operator=(const ObjectList< TYPE > &)=delete
ObjectList::reference
std::list< TYPE * >::reference reference
Definition: ObjectList.h:48
ObjectList::const_iterator
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:52
ObjectList::clID
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition: ObjectList.h:73
ObjectList::front
ObjectList< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectList.h:121
ObjectList::numberOfObjects
ObjectList< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectList.h:112
ObjectList::rend
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:99
ObjectList::back
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:130
ObjectList::containedObject
const ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:224
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:16
ObjectList
Definition: ContainedObject.h:21
ObjectList::back
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectList.h:127
ObjectList::begin
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectList.h:83
ObjectList::containedObject
ContainedObject * containedObject(long dist) override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:228
ObjectList::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:61
ObjectList::add
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectList.h:140
ObjectList::rbegin
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:96
ObjectList::begin
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:80
ObjectList::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:62
ObjectContainerBase
Definition: ObjectContainerBase.h:26
ObjectList::iterator
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:51
Kernel.h
ObjectList::classID
static const CLID & classID()
Definition: ObjectList.h:74
ObjectList::const_reference
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:49
fixtures.reference
Generator[dict, None, None] reference(request, Optional[Path] reference_path)
Definition: fixtures.py:211
ObjectList::const_reverse_iterator
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:55
ObjectContainerBase.h
ObjectList::end
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:89
ObjectList::value_type
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:46
ObjectList::size
ObjectList< TYPE >::size_type size() const
Return the size of the container Size means the number of objects stored in the container,...
Definition: ObjectList.h:107
ContainedObject
Definition: ContainedObject.h:37
ObjectList::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:118