The Gaudi Framework  master (37c0b60a)
ObjectVector.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_OBJECTVECTOR_H
12 #define GAUDIKERNEL_OBJECTVECTOR_H
13 
14 // Include files
15 #include <GaudiKernel/ClassID.h>
16 #include <GaudiKernel/Kernel.h>
19 
20 #include <iomanip>
21 #include <vector>
22 
23 // Definition of the CLID for this class defined in ClassID.h
24 // static const CLID CLID_ObjectList = (1<<17); // ObjectVector (bit 17 set)
25 
45 template <class TYPE>
46 class ObjectVector : public ObjectContainerBase {
47 
48 public:
49  typedef TYPE contained_type;
51 
54 
57 
60 #ifdef _WIN32
61  typedef typename std::vector<TYPE*>::_Tptr pointer;
63 #else
66 #endif
67 
68 public:
70  ObjectVector() = default;
71  ObjectVector( const ObjectVector<TYPE>& ) = delete;
73  ObjectVector( ObjectVector&& rhs ) : ObjectContainerBase( std::move( rhs ) ), m_vector{ std::move( rhs.m_vector ) } {
74  std::for_each( begin(), end(), [this]( TYPE* obj ) { obj->setParent( this ); } );
75  }
76 
78  ~ObjectVector() override {
79  for ( auto& i : m_vector ) {
80  // Set the back pointer to 0 to avoid repetitional searching
81  // for the object in the container, and deleting the object
82  i->setParent( nullptr );
83  delete i;
84  }
85  }
86 
88  const CLID& clID() const override { return ObjectVector<TYPE>::classID(); }
90  static const CLID& classID() {
91  static const CLID clid = TYPE::classID() + CLID_ObjectVector;
92  return clid;
93  }
94 
97 
99  typename ObjectVector<TYPE>::const_iterator begin() const { return m_vector.begin(); }
100 
102  typename ObjectVector<TYPE>::iterator end() { return m_vector.end(); }
103 
105  typename ObjectVector<TYPE>::const_iterator end() const { return m_vector.end(); }
106 
109 
113 
116 
119 
124  typename ObjectVector<TYPE>::size_type size() const { return m_vector.size(); }
125 
127  typename ObjectVector<TYPE>::size_type numberOfObjects() const override { return m_vector.size(); }
128 
131 
135 
143  void reserve( typename ObjectVector<TYPE>::size_type value ) { m_vector.reserve( value ); }
144 
146  bool empty() const { return m_vector.empty(); }
147 
150 
153 
156 
158  typename ObjectVector<TYPE>::const_reference back() const { return m_vector.back(); }
159 
162  if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
163  value->setParent( this );
164  m_vector.push_back( value );
165  }
166 
168  long add( ContainedObject* pObject ) override {
169  try {
170  auto ptr = dynamic_cast<typename ObjectVector<TYPE>::value_type>( pObject );
171  if ( ptr ) {
172  push_back( ptr );
173  return m_vector.size() - 1;
174  }
175  } catch ( ... ) {}
176  return -1;
177  }
178 
181  void pop_back() {
182  auto position = m_vector.back();
183  // Set the back pointer to 0 to avoid repetitional searching
184  // for the object in the container, and deleting the object
185  position->setParent( nullptr );
186  delete position;
187  // Removing from the container itself
188  m_vector.pop_back();
189  }
190 
193  long remove( ContainedObject* value ) override {
194  // Find the object of the value value
195  auto i = std::find_if( begin(), end(), [&]( const ContainedObject* j ) { return j == value; } );
196  if ( i == end() ) {
197  // Object cannot be released from the conatiner,
198  // as it is not contained in it
199  return 0;
200  }
201  long idx = std::distance( begin(), i );
202  // Set the back pointer to 0 to avoid repetitional searching
203  // for the object in the container and deleting the object
204  ( *i )->setParent( nullptr );
205  erase( i );
206  return idx;
207  }
208 
211  typename ObjectVector<TYPE>::const_reference value ) {
212  value->setParent( this );
213  return m_vector.insert( position, value );
214  }
215 
217  void erase( typename ObjectVector<TYPE>::iterator position ) {
218  if ( ( *position )->parent() ) {
219  // Set the back pointer to 0 to avoid repetitional searching
220  // for the object in the container, and deleting the object
221  ( *position )->setParent( nullptr );
222  delete *position;
223  }
224  // Removing from the container itself
225  m_vector.erase( position );
226  }
227 
229  void erase( typename ObjectVector<TYPE>::iterator first, typename ObjectVector<TYPE>::iterator last ) {
230  for ( auto i = first; i != last; i++ ) {
231  // Set the back pointer to 0 to avoid repetitional searching
232  // for the object in the container, and deleting the object
233  ( *i )->setParent( nullptr );
234  delete *i;
235  }
236  // Removing from the container itself
237  m_vector.erase( first, last );
238  }
239 
241  void clear() { erase( begin(), end() ); }
242 
245 
248  return m_vector[n];
249  }
250 
254  long index( const ContainedObject* obj ) const override {
255  auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
256  return i != end() ? std::distance( begin(), i ) : -1;
257  }
258 
260  ContainedObject const* containedObject( long dist ) const override { return m_vector[dist]; }
261  ContainedObject* containedObject( long dist ) override { return m_vector[dist]; }
262 
264  std::ostream& fillStream( std::ostream& s ) const override {
265  s << "class ObjectVector : size = " << std::setw( 12 ) << size() << "\n";
266  // Output the base class
267  // ObjectContainerBase::fillStream(s);
268  if ( !empty() ) {
269  s << "\nContents of the STL vector :";
270  long count = 0;
271  for ( const auto& i : m_vector ) { s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *i; }
272  }
273  return s;
274  }
275 
276 private:
279 };
280 
281 #endif // GAUDIKERNEL_OBJECTVECTOR_H
ObjectVector::max_size
ObjectVector< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectVector.h:130
ObjectVector::const_iterator
std::vector< TYPE * >::const_iterator const_iterator
Definition: ObjectVector.h:56
ObjectVector::rend
ObjectVector< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectVector.h:118
std::for_each
T for_each(T... args)
ObjectVector::operator=
ObjectVector & operator=(const ObjectVector< TYPE > &)=delete
ObjectVector::clID
const CLID & clID() const override
Retrieve class ID.
Definition: ObjectVector.h:88
ObjectVector::front
ObjectVector< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectVector.h:149
ObjectVector
Definition: ContainedObject.h:22
ObjectVector::iterator
std::vector< TYPE * >::iterator iterator
Definition: ObjectVector.h:55
ObjectVector::capacity
ObjectVector< TYPE >::size_type capacity() const
Return number of elements for which memory has been allocated It is always greater than or equal to s...
Definition: ObjectVector.h:134
ObjectVector::begin
ObjectVector< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectVector.h:99
std::move
T move(T... args)
std::vector::reserve
T reserve(T... args)
ObjectVector::containedObject
ContainedObject * containedObject(long dist) override
Definition: ObjectVector.h:261
gaudirun.s
string s
Definition: gaudirun.py:346
ObjectVector::value_type
std::vector< TYPE * >::value_type value_type
Definition: ObjectVector.h:50
std::vector
STL class.
std::find_if
T find_if(T... args)
std::vector::size
T size(T... args)
ClassID.h
ObjectVector::pop_back
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectVector.h:181
ObjectVector::size
ObjectVector< TYPE >::size_type size() const
Return the size of the container.
Definition: ObjectVector.h:124
ObjectVector::front
ObjectVector< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectVector.h:152
ObjectVector::ObjectVector
ObjectVector(const ObjectVector< TYPE > &)=delete
ObjectVector::ObjectVector
ObjectVector(ObjectVector &&rhs)
Definition: ObjectVector.h:73
std::distance
T distance(T... args)
ObjectVector::push_back
void push_back(typename ObjectVector< TYPE >::const_reference value)
push_back = append = insert a new element at the end of the container
Definition: ObjectVector.h:161
ObjectVector::reserve
void reserve(typename ObjectVector< TYPE >::size_type value)
Reserve place for "value" objects in the container.
Definition: ObjectVector.h:143
std::vector::back
T back(T... args)
StreamBuffer.h
ObjectVector::reference
std::vector< TYPE * >::reference reference
Definition: ObjectVector.h:52
ObjectVector::begin
ObjectVector< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectVector.h:96
ObjectVector::index
long index(const ContainedObject *obj) const override
Return distance of a given object from the beginning of its container It correcponds to the "index" (...
Definition: ObjectVector.h:254
std::vector::front
T front(T... args)
ObjectVector::end
ObjectVector< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectVector.h:105
ObjectVector::contained_type
TYPE contained_type
Definition: ObjectVector.h:49
std::vector::push_back
T push_back(T... args)
ObjectVector::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectVector.h:65
std::vector::capacity
T capacity(T... args)
ObjectVector::operator[]
ObjectVector< TYPE >::const_reference operator[](typename ObjectVector< TYPE >::size_type n) const
Return the const_reference to the n'th object in the container.
Definition: ObjectVector.h:247
ObjectVector::insert
ObjectVector< TYPE >::iterator insert(typename ObjectVector< TYPE >::iterator position, typename ObjectVector< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectVector.h:210
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::ostream
STL class.
ObjectVector::erase
void erase(typename ObjectVector< TYPE >::iterator first, typename ObjectVector< TYPE >::iterator last)
Erase the range [first, last) from the container. The removed object will be deleted.
Definition: ObjectVector.h:229
ObjectVector::rbegin
ObjectVector< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectVector.h:108
ObjectVector::const_reference
std::vector< TYPE * >::const_reference const_reference
Definition: ObjectVector.h:53
ObjectVector::back
ObjectVector< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectVector.h:155
std::vector::erase
T erase(T... args)
ObjectVector::erase
void erase(typename ObjectVector< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectVector.h:217
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
std::vector::pop_back
T pop_back(T... args)
std::vector::max_size
T max_size(T... args)
cpluginsvc.n
n
Definition: cpluginsvc.py:234
ObjectVector::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectVector.h:64
std::vector::rend
T rend(T... args)
ObjectVector::rbegin
ObjectVector< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectVector.h:112
ObjectVector::const_reverse_iterator
std::vector< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectVector.h:59
ObjectVector::numberOfObjects
ObjectVector< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectVector.h:127
ObjectVector::back
ObjectVector< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectVector.h:158
ObjectVector::~ObjectVector
~ObjectVector() override
Destructor.
Definition: ObjectVector.h:78
ObjectContainerBase
Definition: ObjectContainerBase.h:29
ObjectVector::end
ObjectVector< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectVector.h:102
ObjectVector::remove
long remove(ContainedObject *value) override
Release object from the container (the poiter will be removed from the container, but the object itse...
Definition: ObjectVector.h:193
std::vector::begin
T begin(T... args)
std
STL namespace.
std::vector::insert
T insert(T... args)
Kernel.h
ObjectVector::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectVector.h:146
ObjectContainerBase.h
std::vector::empty
T empty(T... args)
std::vector::end
T end(T... args)
std::setw
T setw(T... args)
ObjectVector::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectVector.h:264
ObjectVector::reverse_iterator
std::vector< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectVector.h:58
ObjectVector::operator[]
ObjectVector< TYPE >::reference operator[](typename ObjectVector< TYPE >::size_type n)
Return the reference to the n'th object in the container.
Definition: ObjectVector.h:244
ObjectVector::containedObject
ContainedObject const * containedObject(long dist) const override
Return const pointer to an object of a given distance (index)
Definition: ObjectVector.h:260
ObjectVector::m_vector
std::vector< TYPE * > m_vector
The STL vector itself.
Definition: ObjectVector.h:278
ObjectVector::rend
ObjectVector< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectVector.h:115
ObjectVector::add
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectVector.h:168
ObjectVector::ObjectVector
ObjectVector()=default
Constructors.
ContainedObject
Definition: ContainedObject.h:41
std::vector::rbegin
T rbegin(T... args)
ObjectVector::classID
static const CLID & classID()
Retrieve class ID.
Definition: ObjectVector.h:90
ObjectVector::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectVector.h:241