The Gaudi Framework  master (37c0b60a)
ObjectList.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 #ifndef GAUDIKERNEL_OBJECTLIST_H
12 #define GAUDIKERNEL_OBJECTLIST_H
13 
14 // Include files
15 #include <GaudiKernel/ClassID.h>
16 #include <GaudiKernel/Kernel.h>
19 
20 #include <iomanip>
21 #include <list>
22 
23 // Definition of the CLID for this class defined in ClassID.h
24 // static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
25 
44 template <class TYPE>
45 class ObjectList : public ObjectContainerBase {
46 
47 public:
48  typedef TYPE contained_type;
50 
53 
56 
59 
60 #ifdef _WIN32
61  typedef typename std::vector<TYPE*>::_Tptr pointer;
63 #else
66 #endif
67 
68 public:
70  ObjectList() = default;
71 
72  ObjectList( const ObjectList<TYPE>& ) = delete;
73  ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
74 
76  ~ObjectList() override { clear(); }
77 
79  const CLID& clID() const override { return ObjectList<TYPE>::classID(); }
80  static const CLID& classID() {
81  static CLID clid = TYPE::classID() + CLID_ObjectList;
82  return clid;
83  }
84 
86  typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
87 
89  typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
90 
92  typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
93 
95  typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
96 
100 
103 
106 
109 
114  // C++11: std::list::size is constant (pre C++11 it could be linear!)
115  return m_list.size();
116  }
118  typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
119 
121  typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
122 
124  bool empty() const { return m_list.empty(); }
125 
128 
130  typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
131 
133  typename ObjectList<TYPE>::reference back() { return m_list.back(); }
134 
136  typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
137 
140  if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
141  value->setParent( this );
142  m_list.push_back( value );
143  }
144 
146  long add( ContainedObject* pObject ) override {
147  try {
148  auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
149  if ( ptr ) {
150  push_back( ptr );
151  return m_list.size() - 1;
152  }
153  } catch ( ... ) {}
154  return -1;
155  }
156 
159  void pop_back() {
160  auto position = m_list.back();
161  // Set the back pointer to 0 to avoid repetitional searching
162  // for the object in the container, and deleting the object
163  position->setParent( nullptr );
164  delete position;
165  // Removing from the container itself
166  m_list.pop_back();
167  }
168 
171  long remove( ContainedObject* value ) override {
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  value->setParent( this );
192  return m_list.insert( position, value );
193  }
194 
196  void erase( typename ObjectList<TYPE>::iterator position ) {
197  if ( ( *position )->parent() ) {
198  // Set the back pointer to 0 to avoid repetitional searching
199  // for the object in the container, and deleting the object
200  ( *position )->setParent( nullptr );
201  delete *position;
202  }
203  // Removing from the container itself
204  m_list.erase( position );
205  }
206 
208  void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last ) {
209  for ( auto iter = first; iter != last; ++iter ) {
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  delete *iter;
214  }
215  // Removing from the container itself
216  m_list.erase( first, last );
217  }
218 
220  void clear() { erase( begin(), end() ); }
221 
224  long index( const ContainedObject* obj ) const override {
225  auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
226  return i != end() ? std::distance( begin(), i ) : -1;
227  }
228 
230  const ContainedObject* containedObject( long dist ) const override {
231  return dist < size() ? *std::next( begin(), dist ) : nullptr;
232  }
234  ContainedObject* containedObject( long dist ) override {
235  return dist < size() ? *std::next( begin(), dist ) : nullptr;
236  }
237 
239  std::ostream& fillStream( std::ostream& s ) const override {
240  s << "class ObjectList : size = " << std::setw( 12 ) << size() << "\n";
241  // Output the base class
242  // ObjectContainerBase::fillStream(s);
243  if ( !empty() ) {
244  s << "\nContents of the STL list :";
245  long count = 0;
246  for ( const auto& iter : m_list ) {
247  s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
248  }
249  }
250  return s;
251  }
252 
253 private:
256 };
257 
258 #endif // GAUDI_OBJECTLIST_H
ObjectList::contained_type
TYPE contained_type
Definition: ObjectList.h:48
std::list
STL class.
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:108
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:224
std::vector
STL class.
std::find_if
T find_if(T... args)
std::list::size
T size(T... args)
ObjectList::rbegin
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:99
ClassID.h
ObjectList::ObjectList
ObjectList()=default
Constructors.
ObjectList::~ObjectList
~ObjectList() override
Destructor.
Definition: ObjectList.h:76
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:139
ObjectList::front
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:130
std::distance
T distance(T... args)
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:208
ObjectList::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:220
std::list::back
T back(T... args)
ObjectList::end
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:92
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:159
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:121
std::list::front
T front(T... args)
ObjectList::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectList.h:239
ObjectList::remove
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
Definition: ObjectList.h:171
std::list::push_back
T push_back(T... args)
ObjectList::reverse_iterator
std::list< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectList.h:57
ObjectList::m_list
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:255
ObjectList::insert
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:189
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:196
ObjectList::operator=
ObjectList & operator=(const ObjectList< TYPE > &)=delete
std::ostream
STL class.
ObjectList::reference
std::list< TYPE * >::reference reference
Definition: ObjectList.h:51
ObjectList::const_iterator
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:55
ObjectList::clID
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition: ObjectList.h:79
ObjectList::front
ObjectList< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectList.h:127
ObjectList::numberOfObjects
ObjectList< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectList.h:118
ObjectList::rend
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:105
std::list::erase
T erase(T... args)
ObjectList::back
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:136
ObjectList::containedObject
const ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:230
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
ObjectList
Definition: ContainedObject.h:24
std::list::pop_back
T pop_back(T... args)
std::list::max_size
T max_size(T... args)
ObjectList::back
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectList.h:133
std::list::rend
T rend(T... args)
ObjectList::begin
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectList.h:89
ObjectList::containedObject
ContainedObject * containedObject(long dist) override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:234
ObjectList::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:64
ObjectList::add
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectList.h:146
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:102
ObjectList::begin
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:86
ObjectList::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:65
ObjectContainerBase
Definition: ObjectContainerBase.h:29
std::list::begin
T begin(T... args)
ObjectList::iterator
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:54
std::list::insert
T insert(T... args)
Kernel.h
ObjectList::classID
static const CLID & classID()
Definition: ObjectList.h:80
ObjectList::const_reference
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:52
ObjectList::const_reverse_iterator
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:58
ObjectContainerBase.h
std::list::empty
T empty(T... args)
std::list::end
T end(T... args)
ObjectList::end
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:95
ObjectList::value_type
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:49
std::setw
T setw(T... args)
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:113
ContainedObject
Definition: ContainedObject.h:41
std::list::rbegin
T rbegin(T... args)
ObjectList::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:124
std::next
T next(T... args)