| The Gaudi Framework v32r1 (f65d50dc)

ObjectVector.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_OBJECTVECTOR_H
2 #define GAUDIKERNEL_OBJECTVECTOR_H
3
4 // Include files
5 #include "GaudiKernel/ClassID.h"
6 #include "GaudiKernel/Kernel.h"
7 #include "GaudiKernel/ObjectContainerBase.h"
8 #include "GaudiKernel/StreamBuffer.h"
9
10 #include <iomanip>
11 #include <vector>
12
13 // Definition of the CLID for this class defined in ClassID.h
14 // static const CLID CLID_ObjectList = (1<<17); // ObjectVector (bit 17 set)
15
35 template <class TYPE>
36 class ObjectVector : public ObjectContainerBase {
37
38 public:
39 typedef TYPE contained_type;
40 typedef typename std::vector<TYPE*>::value_type value_type;
41
42 typedef typename std::vector<TYPE*>::reference reference;
43 typedef typename std::vector<TYPE*>::const_reference const_reference;
44
45 typedef typename std::vector<TYPE*>::iterator iterator;
46 typedef typename std::vector<TYPE*>::const_iterator const_iterator;
47
48 typedef typename std::vector<TYPE*>::reverse_iterator reverse_iterator;
49 typedef typename std::vector<TYPE*>::const_reverse_iterator const_reverse_iterator;
50 #ifdef _WIN32
51 typedef typename std::vector<TYPE*>::_Tptr pointer;
52 typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
53 #else
54 typedef typename std::vector<TYPE*>::pointer pointer;
55 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
56 #endif
57
58 public:
60 ObjectVector() = default;
61 ObjectVector( const ObjectVector<TYPE>& ) = delete;
62 ObjectVector& operator=( const ObjectVector<TYPE>& ) = delete;
63 ObjectVector( ObjectVector&& rhs ) : ObjectContainerBase( std::move( rhs ) ), m_vector{std::move( rhs.m_vector )} {
64 std::for_each( begin(), end(), [this]( TYPE* obj ) { obj->setParent( this ); } );
65 }
66
68 ~ObjectVector() override {
69 for ( auto& i : m_vector ) {
70 // Set the back pointer to 0 to avoid repetitional searching
71 // for the object in the container, and deleting the object
72 i->setParent( nullptr );
73 delete i;
74 }
75 }
76
78 const CLID& clID() const override { return ObjectVector<TYPE>::classID(); }
80 static const CLID& classID() {
81 static const CLID clid = TYPE::classID() + CLID_ObjectVector;
82 return clid;
83 }
84
86 typename ObjectVector<TYPE>::iterator begin() { return m_vector.begin(); }
87
89 typename ObjectVector<TYPE>::const_iterator begin() const { return m_vector.begin(); }
90
92 typename ObjectVector<TYPE>::iterator end() { return m_vector.end(); }
93
95 typename ObjectVector<TYPE>::const_iterator end() const { return m_vector.end(); }
96
98 typename ObjectVector<TYPE>::reverse_iterator rbegin() { return m_vector.rbegin(); }
99
102 typename ObjectVector<TYPE>::const_reverse_iterator rbegin() const { return m_vector.rbegin(); }
103
105 typename ObjectVector<TYPE>::reverse_iterator rend() { return m_vector.rend(); }
106
108 typename ObjectVector<TYPE>::const_reverse_iterator rend() const { return m_vector.rend(); }
109
114 typename ObjectVector<TYPE>::size_type size() const { return m_vector.size(); }
115
117 typename ObjectVector<TYPE>::size_type numberOfObjects() const override { return m_vector.size(); }
118
120 typename ObjectVector<TYPE>::size_type max_size() const { return m_vector.max_size(); }
121
124 typename ObjectVector<TYPE>::size_type capacity() const { return m_vector.capacity(); }
125
133 void reserve( typename ObjectVector<TYPE>::size_type value ) { m_vector.reserve( value ); }
134
136 bool empty() const { return m_vector.empty(); }
137
139 typename ObjectVector<TYPE>::reference front() { return m_vector.front(); }
140
142 typename ObjectVector<TYPE>::const_reference front() const { return m_vector.front(); }
143
145 typename ObjectVector<TYPE>::reference back() { return m_vector.back(); }
146
148 typename ObjectVector<TYPE>::const_reference back() const { return m_vector.back(); }
149
151 void push_back( typename ObjectVector<TYPE>::const_reference value ) {
152 if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
153 value->setParent( this );
154 m_vector.push_back( value );
155 }
156
158 long add( ContainedObject* pObject ) override {
159 try {
160 auto ptr = dynamic_cast<typename ObjectVector<TYPE>::value_type>( pObject );
161 if ( ptr ) {
162 push_back( ptr );
163 return m_vector.size() - 1;
164 }
165 } catch ( ... ) {}
166 return -1;
167 }
168
171 void pop_back() {
172 auto position = m_vector.back();
173 // Set the back pointer to 0 to avoid repetitional searching
174 // for the object in the container, and deleting the object
175 position->setParent( nullptr );
176 delete position;
177 // Removing from the container itself
178 m_vector.pop_back();
179 }
180
183 long remove( ContainedObject* value ) override {
184 // Find the object of the value value
185 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* j ) { return j == value; } );
186 if ( i == end() ) {
187 // Object cannot be released from the conatiner,
188 // as it is not contained in it
189 return 0;
190 }
191 long idx = std::distance( begin(), i );
192 // Set the back pointer to 0 to avoid repetitional searching
193 // for the object in the container and deleting the object
194 ( *i )->setParent( nullptr );
195 erase( i );
196 return idx;
197 }
198
200 typename ObjectVector<TYPE>::iterator insert( typename ObjectVector<TYPE>::iterator position,
201 typename ObjectVector<TYPE>::const_reference value ) {
202 value->setParent( this );
203 return m_vector.insert( position, value );
204 }
205
207 void erase( typename ObjectVector<TYPE>::iterator position ) {
208 if ( ( *position )->parent() ) {
209 // Set the back pointer to 0 to avoid repetitional searching
210 // for the object in the container, and deleting the object
211 ( *position )->setParent( nullptr );
212 delete *position;
213 }
214 // Removing from the container itself
215 m_vector.erase( position );
216 }
217
219 void erase( typename ObjectVector<TYPE>::iterator first, typename ObjectVector<TYPE>::iterator last ) {
220 for ( auto i = first; i != last; i++ ) {
221 // Set the back pointer to 0 to avoid repetitional searching
222 // for the object in the container, and deleting the object
223 ( *i )->setParent( nullptr );
224 delete *i;
225 }
226 // Removing from the container itself
227 m_vector.erase( first, last );
228 }
229
231 void clear() { erase( begin(), end() ); }
232
234 typename ObjectVector<TYPE>::reference operator[]( typename ObjectVector<TYPE>::size_type n ) { return m_vector[n]; }
235
237 typename ObjectVector<TYPE>::const_reference operator[]( typename ObjectVector<TYPE>::size_type n ) const {
238 return m_vector[n];
239 }
240
244 long index( const ContainedObject* obj ) const override {
245 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
246 return i != end() ? std::distance( begin(), i ) : -1;
247 }
248
250 ContainedObject const* containedObject( long dist ) const override { return m_vector[dist]; }
251 ContainedObject* containedObject( long dist ) override { return m_vector[dist]; }
252
254 std::ostream& fillStream( std::ostream& s ) const override {
255 s << "class ObjectVector : size = " << std::setw( 12 ) << size() << "\n";
256 // Output the base class
257 // ObjectContainerBase::fillStream(s);
258 if ( !empty() ) {
259 s << "\nContents of the STL vector :";
260 long count = 0;
261 for ( const auto& i : m_vector ) { s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *i; }
262 }
263 return s;
264 }
265
266 private:
268 std::vector<TYPE*> m_vector;
269 };
270
271 #endif // GAUDIKERNEL_OBJECTVECTOR_H
ObjectVector::end
ObjectVector< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectVector.h:92
ClassID.h
Kernel.h
ObjectVector::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectVector.h:231
std::vector::empty
T empty(T... args)
std::distance
T distance(T... args)
ObjectVector::add
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectVector.h:158
ObjectVector::end
ObjectVector< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectVector.h:95
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:29
ObjectVector::back
ObjectVector< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectVector.h:145
ObjectVector::begin
ObjectVector< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectVector.h:86
std::vector::rend
T rend(T... args)
std::vector::front
T front(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:207
ObjectVector::rbegin
ObjectVector< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectVector.h:98
ObjectVector::const_iterator
std::vector< TYPE * >::const_iterator const_iterator
Definition: ObjectVector.h:46
ObjectVector::m_vector
std::vector< TYPE * > m_vector
The STL vector itself.
Definition: ObjectVector.h:268
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:151
ObjectVector
ObjectVector is one of the basic Gaudi container classes capable of being registered in Data Stores.
Definition: ContainedObject.h:12
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:124
std
STL namespace.
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:102
ObjectContainerBase.h
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:219
ObjectVector::const_reference
std::vector< TYPE * >::const_reference const_reference
Definition: ObjectVector.h:43
std::vector::end
T end(T... args)
ObjectVector::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectVector.h:55
StreamBuffer.h
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:183
ObjectVector::begin
ObjectVector< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectVector.h:89
std::setw
T setw(T... args)
ObjectVector::ObjectVector
ObjectVector(ObjectVector &&rhs)
Definition: ObjectVector.h:63
ObjectVector::size
ObjectVector< TYPE >::size_type size() const
Return the size of the container.
Definition: ObjectVector.h:114
ObjectVector::const_reverse_iterator
std::vector< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectVector.h:49
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
ObjectVector::front
ObjectVector< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectVector.h:139
ObjectVector::operator=
ObjectVector & operator=(const ObjectVector< TYPE > &)=delete
std::vector::push_back
T push_back(T... args)
ObjectVector::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectVector.h:136
ObjectVector::reverse_iterator
std::vector< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectVector.h:48
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:234
ObjectVector::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectVector.h:254
ObjectVector::ObjectVector
ObjectVector()=default
Constructors.
std::vector::erase
T erase(T... args)
std::vector::pop_back
T pop_back(T... args)
std::vector::capacity
T capacity(T... args)
ObjectVector::iterator
std::vector< TYPE * >::iterator iterator
Definition: ObjectVector.h:45
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:237
ObjectVector::front
ObjectVector< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectVector.h:142
ObjectVector::~ObjectVector
~ObjectVector() override
Destructor.
Definition: ObjectVector.h:68
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
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:108
ObjectVector::reserve
void reserve(typename ObjectVector< TYPE >::size_type value)
Reserve place for "value" objects in the container.
Definition: ObjectVector.h:133
std::move
T move(T... args)
ObjectVector::numberOfObjects
ObjectVector< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectVector.h:117
ObjectVector::reference
std::vector< TYPE * >::reference reference
Definition: ObjectVector.h:42
ObjectVector::insert
ObjectVector< TYPE >::iterator insert(typename ObjectVector< TYPE >::iterator position, typename ObjectVector< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectVector.h:200
std::vector::insert
T insert(T... args)
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:31
std::find_if
T find_if(T... args)
std::vector::size
T size(T... args)
ObjectVector::max_size
ObjectVector< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectVector.h:120
std::vector
STL class.
ObjectVector::value_type
std::vector< TYPE * >::value_type value_type
Definition: ObjectVector.h:40
std::vector::begin
T begin(T... args)
ObjectVector::containedObject
ContainedObject const * containedObject(long dist) const override
Return const pointer to an object of a given distance (index)
Definition: ObjectVector.h:250
ObjectVector::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectVector.h:54
std::vector::max_size
T max_size(T... args)
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:244
std::vector::back
T back(T... args)
gaudirun.s
string s
Definition: gaudirun.py:316
ObjectVector::contained_type
TYPE contained_type
Definition: ObjectVector.h:39
ObjectContainerBase
ObjectContainerBase is the base class for Gaudi container classes.
Definition: ObjectContainerBase.h:19
ObjectVector::rend
ObjectVector< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectVector.h:105
ObjectVector::back
ObjectVector< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectVector.h:148
ObjectVector::containedObject
ContainedObject * containedObject(long dist) override
Definition: ObjectVector.h:251
ObjectVector::clID
const CLID & clID() const override
Retrieve class ID.
Definition: ObjectVector.h:78
std::for_each
T for_each(T... args)
std::ostream
STL class.
ObjectVector::classID
static const CLID & classID()
Retrieve class ID.
Definition: ObjectVector.h:80
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:171
std::vector::reserve
T reserve(T... args)
std::vector::rbegin
T rbegin(T... args)