The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
ObjectVector.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 <vector>
19
20// Definition of the CLID for this class defined in ClassID.h
21// static const CLID CLID_ObjectList = (1<<17); // ObjectVector (bit 17 set)
22
42template <class TYPE>
44
45public:
46 typedef TYPE contained_type;
47 typedef typename std::vector<TYPE*>::value_type value_type;
48
49 typedef typename std::vector<TYPE*>::reference reference;
50 typedef typename std::vector<TYPE*>::const_reference const_reference;
51
52 typedef typename std::vector<TYPE*>::iterator iterator;
53 typedef typename std::vector<TYPE*>::const_iterator const_iterator;
54
55 typedef typename std::vector<TYPE*>::reverse_iterator reverse_iterator;
56 typedef typename std::vector<TYPE*>::const_reverse_iterator const_reverse_iterator;
57
58 typedef typename std::vector<TYPE*>::pointer pointer;
59 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
60
61public:
62 ObjectVector() = default;
63 ObjectVector( const ObjectVector<TYPE>& ) = delete;
65 ObjectVector( ObjectVector&& rhs ) : ObjectContainerBase( std::move( rhs ) ), m_vector{ std::move( rhs.m_vector ) } {
66 std::for_each( begin(), end(), [this]( TYPE* obj ) { obj->setParent( this ); } );
67 }
68
69 ~ObjectVector() override {
70 for ( auto& i : m_vector ) {
71 // Set the back pointer to 0 to avoid repetitional searching
72 // for the object in the container, and deleting the object
73 i->setParent( nullptr );
74 delete i;
75 }
76 }
77
79 const CLID& clID() const override { return ObjectVector<TYPE>::classID(); }
81 static const CLID& classID() {
82 static const CLID clid = TYPE::classID() + CLID_ObjectVector;
83 return clid;
84 }
85
87 typename ObjectVector<TYPE>::iterator begin() { return m_vector.begin(); }
88
90 typename ObjectVector<TYPE>::const_iterator begin() const { return m_vector.begin(); }
91
93 typename ObjectVector<TYPE>::iterator end() { return m_vector.end(); }
94
96 typename ObjectVector<TYPE>::const_iterator end() const { return m_vector.end(); }
97
100
103 typename ObjectVector<TYPE>::const_reverse_iterator rbegin() const { return m_vector.rbegin(); }
104
107
109 typename ObjectVector<TYPE>::const_reverse_iterator rend() const { return m_vector.rend(); }
110
115 typename ObjectVector<TYPE>::size_type size() const { return m_vector.size(); }
116
118 typename ObjectVector<TYPE>::size_type numberOfObjects() const override { return m_vector.size(); }
119
121 typename ObjectVector<TYPE>::size_type max_size() const { return m_vector.max_size(); }
122
125 typename ObjectVector<TYPE>::size_type capacity() const { return m_vector.capacity(); }
126
134 void reserve( typename ObjectVector<TYPE>::size_type value ) { m_vector.reserve( value ); }
135
137 bool empty() const { return m_vector.empty(); }
138
140 typename ObjectVector<TYPE>::reference front() { return m_vector.front(); }
141
143 typename ObjectVector<TYPE>::const_reference front() const { return m_vector.front(); }
144
146 typename ObjectVector<TYPE>::reference back() { return m_vector.back(); }
147
149 typename ObjectVector<TYPE>::const_reference back() const { return m_vector.back(); }
150
153 if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
154 value->setParent( this );
155 m_vector.push_back( value );
156 }
157
159 long add( ContainedObject* pObject ) override {
160 try {
161 auto ptr = dynamic_cast<typename ObjectVector<TYPE>::value_type>( pObject );
162 if ( ptr ) {
163 push_back( ptr );
164 return m_vector.size() - 1;
165 }
166 } catch ( ... ) {}
167 return -1;
168 }
169
172 void pop_back() {
173 auto position = m_vector.back();
174 // Set the back pointer to 0 to avoid repetitional searching
175 // for the object in the container, and deleting the object
176 position->setParent( nullptr );
177 delete position;
178 // Removing from the container itself
179 m_vector.pop_back();
180 }
181
184 long remove( ContainedObject* value ) override {
185 // Find the object of the value value
186 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* j ) { return j == value; } );
187 if ( i == end() ) {
188 // Object cannot be released from the conatiner,
189 // as it is not contained in it
190 return 0;
191 }
192 long idx = std::distance( begin(), i );
193 // Set the back pointer to 0 to avoid repetitional searching
194 // for the object in the container and deleting the object
195 ( *i )->setParent( nullptr );
196 erase( i );
197 return idx;
198 }
199
202 typename ObjectVector<TYPE>::const_reference value ) {
203 value->setParent( this );
204 return m_vector.insert( position, value );
205 }
206
208 void erase( typename ObjectVector<TYPE>::iterator position ) {
209 if ( ( *position )->parent() ) {
210 // Set the back pointer to 0 to avoid repetitional searching
211 // for the object in the container, and deleting the object
212 ( *position )->setParent( nullptr );
213 delete *position;
214 }
215 // Removing from the container itself
216 m_vector.erase( position );
217 }
218
221 for ( auto i = first; i != last; i++ ) {
222 // Set the back pointer to 0 to avoid repetitional searching
223 // for the object in the container, and deleting the object
224 ( *i )->setParent( nullptr );
225 delete *i;
226 }
227 // Removing from the container itself
228 m_vector.erase( first, last );
229 }
230
232 void clear() { erase( begin(), end() ); }
233
236
241
245 long index( const ContainedObject* obj ) const override {
246 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
247 return i != end() ? std::distance( begin(), i ) : -1;
248 }
249
251 ContainedObject const* containedObject( long dist ) const override { return m_vector[dist]; }
252 ContainedObject* containedObject( long dist ) override { return m_vector[dist]; }
253
255 std::ostream& fillStream( std::ostream& s ) const override {
256 s << "class ObjectVector : size = " << std::setw( 12 ) << size() << "\n";
257 // Output the base class
258 // ObjectContainerBase::fillStream(s);
259 if ( !empty() ) {
260 s << "\nContents of the STL vector :";
261 long count = 0;
262 for ( const auto& i : m_vector ) { s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *i; }
263 }
264 return s;
265 }
266
267private:
268 std::vector<TYPE*> m_vector;
269};
unsigned int CLID
Class ID definition.
Definition ClassID.h:16
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
ObjectContainerBase()=default
Constructor.
size_t size_type
size_type, to conform the STL container interface
ObjectVector< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
ObjectVector< TYPE >::reference operator[](typename ObjectVector< TYPE >::size_type n)
Return the reference to the n'th object in the container.
void push_back(typename ObjectVector< TYPE >::const_reference value)
push_back = append = insert a new element at the end of the container
ObjectVector< TYPE >::iterator insert(typename ObjectVector< TYPE >::iterator position, typename ObjectVector< TYPE >::const_reference value)
Insert "value" before "position".
ObjectVector< TYPE >::reference front()
Return reference to the first element.
ObjectVector< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
~ObjectVector() override
std::vector< ContainedObject * >::reverse_iterator reverse_iterator
std::vector< ContainedObject * >::iterator iterator
long remove(ContainedObject *value) override
Release object from the container (the poiter will be removed from the container, but the object itse...
std::vector< ContainedObject * >::const_reverse_iterator const_reverse_iterator
ObjectVector & operator=(const ObjectVector< TYPE > &)=delete
std::vector< ContainedObject * >::reference reference
std::vector< ContainedObject * >::const_reference const_reference
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
ObjectVector< TYPE >::const_reference front() const
Return const_reference to the first element.
long add(ContainedObject *pObject) override
Add an object to the container.
ObjectVector< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
ObjectVector< TYPE >::size_type size() const
Return the size of the container.
std::vector< ContainedObject * >::const_pointer const_pointer
ObjectVector< TYPE >::reference back()
Return reference to the last element.
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.
void erase(typename ObjectVector< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
const CLID & clID() const override
Retrieve class ID.
void clear()
Clear the entire content of the container and delete all contained objects.
ObjectVector(ObjectVector &&rhs)
ObjectVector< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
std::vector< ContainedObject * >::pointer pointer
ContainedObject * containedObject(long dist) override
ObjectVector< TYPE >::const_reference back() const
Return const_reference to the last element.
bool empty() const
Return true if the size of the container is 0.
std::vector< ContainedObject * >::value_type value_type
ObjectVector< TYPE >::size_type max_size() const
Return the largest possible size of the container.
void reserve(typename ObjectVector< TYPE >::size_type value)
Reserve place for "value" objects in the container.
ObjectVector(const ObjectVector< TYPE > &)=delete
ObjectVector< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
ObjectVector< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
long index(const ContainedObject *obj) const override
Return distance of a given object from the beginning of its container It correcponds to the "index" (...
ObjectVector< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
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...
ObjectVector< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
std::vector< ContainedObject * > m_vector
ContainedObject const * containedObject(long dist) const override
Return const pointer to an object of a given distance (index)
ObjectVector()=default
ObjectVector< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
static const CLID & classID()
Retrieve class ID.
std::vector< ContainedObject * >::const_iterator const_iterator
ObjectVector< TYPE >::const_reference operator[](typename ObjectVector< TYPE >::size_type n) const
Return the const_reference to the n'th object in the container.
STL namespace.