| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
39 public:
40 typedef TYPE contained_type;
41 typedef typename std::vector<TYPE*>::value_type value_type;
42
43 typedef typename std::vector<TYPE*>::reference reference;
44 typedef typename std::vector<TYPE*>::const_reference const_reference;
45
46 typedef typename std::vector<TYPE*>::iterator iterator;
47 typedef typename std::vector<TYPE*>::const_iterator const_iterator;
48
49 typedef typename std::vector<TYPE*>::reverse_iterator reverse_iterator;
50 typedef typename std::vector<TYPE*>::const_reverse_iterator const_reverse_iterator;
51 #ifdef _WIN32
52 typedef typename std::vector<TYPE*>::_Tptr pointer;
53 typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
54 #else
55 typedef typename std::vector<TYPE*>::pointer pointer;
56 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
57 #endif
58
59 public:
61 ObjectVector() = default;
62 ObjectVector( const ObjectVector<TYPE>& ) = delete;
63 ObjectVector& operator=( const ObjectVector<TYPE>& ) = delete;
64 ObjectVector( ObjectVector&& rhs ) : ObjectContainerBase( std::move( rhs ) ), m_vector{std::move( rhs.m_vector )}
65 {
66 std::for_each( begin(), end(), [this]( TYPE* obj ) { obj->setParent( this ); } );
67 }
68
70 ~ObjectVector() override
71 {
72 for ( auto& i : m_vector ) {
73 // Set the back pointer to 0 to avoid repetitional searching
74 // for the object in the container, and deleting the object
75 i->setParent( nullptr );
76 delete i;
77 }
78 }
79
81 const CLID& clID() const override { return ObjectVector<TYPE>::classID(); }
83 static const CLID& classID()
84 {
85 static const CLID clid = TYPE::classID() + CLID_ObjectVector;
86 return clid;
87 }
88
90 typename ObjectVector<TYPE>::iterator begin() { return m_vector.begin(); }
91
93 typename ObjectVector<TYPE>::const_iterator begin() const { return m_vector.begin(); }
94
96 typename ObjectVector<TYPE>::iterator end() { return m_vector.end(); }
97
99 typename ObjectVector<TYPE>::const_iterator end() const { return m_vector.end(); }
100
102 typename ObjectVector<TYPE>::reverse_iterator rbegin() { return m_vector.rbegin(); }
103
106 typename ObjectVector<TYPE>::const_reverse_iterator rbegin() const { return m_vector.rbegin(); }
107
109 typename ObjectVector<TYPE>::reverse_iterator rend() { return m_vector.rend(); }
110
112 typename ObjectVector<TYPE>::const_reverse_iterator rend() const { return m_vector.rend(); }
113
118 typename ObjectVector<TYPE>::size_type size() const { return m_vector.size(); }
119
121 typename ObjectVector<TYPE>::size_type numberOfObjects() const override { return m_vector.size(); }
122
124 typename ObjectVector<TYPE>::size_type max_size() const { return m_vector.max_size(); }
125
128 typename ObjectVector<TYPE>::size_type capacity() const { return m_vector.capacity(); }
129
137 void reserve( typename ObjectVector<TYPE>::size_type value ) { m_vector.reserve( value ); }
138
140 bool empty() const { return m_vector.empty(); }
141
143 typename ObjectVector<TYPE>::reference front() { return m_vector.front(); }
144
146 typename ObjectVector<TYPE>::const_reference front() const { return m_vector.front(); }
147
149 typename ObjectVector<TYPE>::reference back() { return m_vector.back(); }
150
152 typename ObjectVector<TYPE>::const_reference back() const { return m_vector.back(); }
153
155 void push_back( typename ObjectVector<TYPE>::const_reference value )
156 {
157 if ( value->parent() ) {
158 const_cast<ObjectContainerBase*>( value->parent() )->remove( value );
159 }
160 value->setParent( this );
161 m_vector.push_back( value );
162 }
163
165 long add( ContainedObject* pObject ) override
166 {
167 try {
168 auto ptr = dynamic_cast<typename ObjectVector<TYPE>::value_type>( pObject );
169 if ( ptr ) {
170 push_back( ptr );
171 return m_vector.size() - 1;
172 }
173 } catch ( ... ) {
174 }
175 return -1;
176 }
177
180 void pop_back()
181 {
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 {
195 // Find the object of the value value
196 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* j ) { return j == value; } );
197 if ( i == end() ) {
198 // Object cannot be released from the conatiner,
199 // as it is not contained in it
200 return StatusCode::FAILURE;
201 }
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 StatusCode::SUCCESS;
207 }
208
210 typename ObjectVector<TYPE>::iterator insert( typename ObjectVector<TYPE>::iterator position,
211 typename ObjectVector<TYPE>::const_reference value )
212 {
213 value->setParent( this );
214 return m_vector.insert( position, value );
215 }
216
218 void erase( typename ObjectVector<TYPE>::iterator position )
219 {
220 if ( ( *position )->parent() ) {
221 // Set the back pointer to 0 to avoid repetitional searching
222 // for the object in the container, and deleting the object
223 ( *position )->setParent( nullptr );
224 delete *position;
225 }
226 // Removing from the container itself
227 m_vector.erase( position );
228 }
229
231 void erase( typename ObjectVector<TYPE>::iterator first, typename ObjectVector<TYPE>::iterator last )
232 {
233 for ( auto i = first; i != last; i++ ) {
234 // Set the back pointer to 0 to avoid repetitional searching
235 // for the object in the container, and deleting the object
236 ( *i )->setParent( nullptr );
237 delete *i;
238 }
239 // Removing from the container itself
240 m_vector.erase( first, last );
241 }
242
244 void clear() { erase( begin(), end() ); }
245
247 typename ObjectVector<TYPE>::reference operator[]( typename ObjectVector<TYPE>::size_type n ) { return m_vector[n]; }
248
250 typename ObjectVector<TYPE>::const_reference operator[]( typename ObjectVector<TYPE>::size_type n ) const
251 {
252 return m_vector[n];
253 }
254
258 long index( const ContainedObject* obj ) const override
259 {
260 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
261 return i != end() ? std::distance( begin(), i ) : -1;
262 }
263
265 ContainedObject* containedObject( long dist ) const override { return m_vector[dist]; }
266
268 std::ostream& fillStream( std::ostream& s ) const override
269 {
270 s << "class ObjectVector : size = " << std::setw( 12 ) << size() << "\n";
271 // Output the base class
272 // ObjectContainerBase::fillStream(s);
273 if ( !empty() ) {
274 s << "\nContents of the STL vector :";
275 long count = 0;
276 for ( const auto& i : m_vector ) {
277 s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *i;
278 }
279 }
280 return s;
281 }
282
283 private:
285 std::vector<TYPE*> m_vector;
286 };
287
288 #endif // GAUDIKERNEL_OBJECTVECTOR_H
ObjectVector::end
ObjectVector< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectVector.h:96
StatusCode::SUCCESS
Definition: StatusCode.h:31
ClassID.h
Kernel.h
ObjectVector::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectVector.h:244
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:165
StatusCode::FAILURE
Definition: StatusCode.h:31
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:30
ObjectVector::clID
const CLID & clID() const override
Retrieve class ID.
Definition: ObjectVector.h:81
ObjectVector::back
ObjectVector< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectVector.h:149
ObjectVector::begin
ObjectVector< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectVector.h:90
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:218
ObjectVector::rbegin
ObjectVector< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectVector.h:102
ObjectVector::operator[]
ObjectVector< TYPE >::const_reference operator[](typename ObjectVector< TYPE >::size_type n) const
Return the const_reference to the n&#39;th object in the container.
Definition: ObjectVector.h:250
ObjectVector::begin
ObjectVector< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectVector.h:93
ObjectVector::const_iterator
std::vector< TYPE * >::const_iterator const_iterator
Definition: ObjectVector.h:47
ObjectVector::m_vector
std::vector< TYPE * > m_vector
The STL vector itself.
Definition: ObjectVector.h:285
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:155
ObjectVector
ObjectVector is one of the basic Gaudi container classes capable of being registered in Data Stores...
Definition: ContainedObject.h:12
std
STL namespace.
ObjectContainerBase.h
ObjectVector::containedObject
ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance (index)
Definition: ObjectVector.h:265
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:231
ObjectVector::const_reference
std::vector< TYPE * >::const_reference const_reference
Definition: ObjectVector.h:44
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:112
std::vector::end
T end(T...args)
ObjectVector::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectVector.h:56
StreamBuffer.h
std::setw
T setw(T...args)
ObjectVector::ObjectVector
ObjectVector(ObjectVector &&rhs)
Definition: ObjectVector.h:64
ObjectVector::const_reverse_iterator
std::vector< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectVector.h:50
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:228
ObjectVector::front
ObjectVector< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectVector.h:143
ObjectVector::operator=
ObjectVector & operator=(const ObjectVector< TYPE > &)=delete
std::vector::push_back
T push_back(T...args)
ObjectVector::front
ObjectVector< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectVector.h:146
ObjectVector::reverse_iterator
std::vector< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectVector.h:49
ObjectVector::operator[]
ObjectVector< TYPE >::reference operator[](typename ObjectVector< TYPE >::size_type n)
Return the reference to the n&#39;th object in the container.
Definition: ObjectVector.h:247
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:258
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:128
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:46
ObjectVector::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectVector.h:140
ObjectVector::~ObjectVector
~ObjectVector() override
Destructor.
Definition: ObjectVector.h:70
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
ObjectVector::reserve
void reserve(typename ObjectVector< TYPE >::size_type value)
Reserve place for "value" objects in the container.
Definition: ObjectVector.h:137
std::move
T move(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:106
ObjectVector::reference
std::vector< TYPE * >::reference reference
Definition: ObjectVector.h:43
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
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:124
std::vector
STL class.
ObjectVector::back
ObjectVector< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectVector.h:152
ObjectVector::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectVector.h:268
ObjectVector::value_type
std::vector< TYPE * >::value_type value_type
Definition: ObjectVector.h:41
ObjectVector::numberOfObjects
ObjectVector< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectVector.h:121
std::vector::begin
T begin(T...args)
ObjectVector::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectVector.h:55
std::vector::max_size
T max_size(T...args)
std::vector::back
T back(T...args)
gaudirun.s
string s
Definition: gaudirun.py:253
ObjectVector::contained_type
TYPE contained_type
Definition: ObjectVector.h:40
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:109
std::for_each
T for_each(T...args)
ObjectVector::size
ObjectVector< TYPE >::size_type size() const
Return the size of the container.
Definition: ObjectVector.h:118
ObjectVector::end
ObjectVector< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectVector.h:99
std::ostream
STL class.
ObjectVector::classID
static const CLID & classID()
Retrieve class ID.
Definition: ObjectVector.h:83
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:180
std::vector::reserve
T reserve(T...args)
std::vector::rbegin
T rbegin(T...args)