| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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
5 // Include files
6 #include "GaudiKernel/Kernel.h"
7 #include "GaudiKernel/ClassID.h"
8 #include "GaudiKernel/StreamBuffer.h"
9 #include "GaudiKernel/ObjectContainerBase.h"
10
11 #include <vector>
12 #include <iomanip>
13
14
15 // Definition of the CLID for this class defined in ClassID.h
16 //static const CLID CLID_ObjectList = (1<<17); // ObjectVector (bit 17 set)
17
37 template <class TYPE>
38 class ObjectVector : public ObjectContainerBase {
39
40 public:
41 typedef TYPE contained_type;
42 typedef typename std::vector<TYPE*>::value_type value_type;
43
44 typedef typename std::vector<TYPE*>::reference reference;
45 typedef typename std::vector<TYPE*>::const_reference const_reference;
46
47 typedef typename std::vector<TYPE*>::iterator iterator;
48 typedef typename std::vector<TYPE*>::const_iterator const_iterator;
49
50 typedef typename std::vector<TYPE*>::reverse_iterator reverse_iterator;
51 typedef typename std::vector<TYPE*>::const_reverse_iterator const_reverse_iterator;
52 #ifdef _WIN32
53 typedef typename std::vector<TYPE*>::_Tptr pointer;
54 typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
55 #else
56 typedef typename std::vector<TYPE*>::pointer pointer;
57 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
58 #endif
59
60 public:
62 ObjectVector() = default;
63 ObjectVector( const ObjectVector<TYPE>& ) = delete;
64 ObjectVector& operator=( const ObjectVector<TYPE>& ) = delete;
65 ObjectVector(ObjectVector&& rhs)
66 : ObjectContainerBase(std::move(rhs))
67 , m_vector{std::move(rhs.m_vector)}
68 {
69 std::for_each(begin(), end(), [this](TYPE* obj) {obj->setParent(this);});
70 }
71
73 ~ObjectVector() override {
74 for( auto& i : m_vector ) {
75 // Set the back pointer to 0 to avoid repetitional searching
76 // for the object in the container, and deleting the object
77 i->setParent (nullptr);
78 delete i;
79 }
80 }
81
83 const CLID& clID() const override {
84 return ObjectVector<TYPE>::classID();
85 }
87 static const CLID& classID() {
88 static const CLID clid = TYPE::classID() + CLID_ObjectVector;
89 return clid;
90 }
91
92
94 typename ObjectVector<TYPE>::iterator begin () {
95 return m_vector.begin();
96 }
97
99 typename ObjectVector<TYPE>::const_iterator begin () const {
100 return m_vector.begin();
101 }
102
104 typename ObjectVector<TYPE>::iterator end () {
105 return m_vector.end();
106 }
107
109 typename ObjectVector<TYPE>::const_iterator end () const {
110 return m_vector.end();
111 }
112
114 typename ObjectVector<TYPE>::reverse_iterator rbegin () {
115 return m_vector.rbegin();
116 }
117
120 typename ObjectVector<TYPE>::const_reverse_iterator rbegin () const {
121 return m_vector.rbegin();
122 }
123
125 typename ObjectVector<TYPE>::reverse_iterator rend () {
126 return m_vector.rend();
127 }
128
130 typename ObjectVector<TYPE>::const_reverse_iterator rend () const {
131 return m_vector.rend();
132 }
133
138 typename ObjectVector<TYPE>::size_type size () const {
139 return m_vector.size();
140 }
141
143 typename ObjectVector<TYPE>::size_type numberOfObjects() const override {
144 return m_vector.size();
145 }
146
148 typename ObjectVector<TYPE>::size_type max_size () const {
149 return m_vector.max_size();
150 }
151
154 typename ObjectVector<TYPE>::size_type capacity () const {
155 return m_vector.capacity();
156 }
157
165 void reserve( typename ObjectVector<TYPE>::size_type value ) {
166 m_vector.reserve( value );
167 }
168
170 bool empty () const {
171 return m_vector.empty();
172 }
173
175 typename ObjectVector<TYPE>::reference front () {
176 return m_vector.front();
177 }
178
180 typename ObjectVector<TYPE>::const_reference front () const {
181 return m_vector.front();
182 }
183
185 typename ObjectVector<TYPE>::reference back () {
186 return m_vector.back();
187 }
188
190 typename ObjectVector<TYPE>::const_reference back () const {
191 return m_vector.back();
192 }
193
195 void push_back( typename ObjectVector<TYPE>::const_reference value ) {
196 if( value->parent() ) {
197 const_cast<ObjectContainerBase*>(value->parent())->remove(value);
198 }
199 value->setParent(this);
200 m_vector.push_back(value);
201 }
202
204 long add(ContainedObject* pObject) override {
205 try {
206 auto ptr = dynamic_cast<typename ObjectVector<TYPE>::value_type>(pObject);
207 if ( ptr ) {
208 push_back(ptr);
209 return m_vector.size()-1;
210 }
211 }
212 catch(...) {
213 }
214 return -1;
215 }
216
219 void pop_back () {
220 auto position = m_vector.back();
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 // Removing from the container itself
226 m_vector.pop_back();
227 }
228
231 long remove(ContainedObject* value) override {
232 // Find the object of the value value
233 auto i = std::find_if( begin(), end(), [&](const ContainedObject* j) { return j==value; } );
234 if( i == end() ) {
235 // Object cannot be released from the conatiner,
236 // as it is not contained in it
237 return StatusCode::FAILURE;
238 }
239 // Set the back pointer to 0 to avoid repetitional searching
240 // for the object in the container and deleting the object
241 (*i)->setParent (nullptr);
242 erase(i);
243 return StatusCode::SUCCESS;
244 }
245
247 typename ObjectVector<TYPE>::iterator insert( typename ObjectVector<TYPE>::iterator position,
248 typename ObjectVector<TYPE>::const_reference value ) {
249 value->setParent(this);
250 return m_vector.insert(position, value);
251 }
252
254 void erase( typename ObjectVector<TYPE>::iterator position ) {
255 if( (*position)->parent() ) {
256 // Set the back pointer to 0 to avoid repetitional searching
257 // for the object in the container, and deleting the object
258 (*position)->setParent (nullptr);
259 delete *position;
260 }
261 // Removing from the container itself
262 m_vector.erase(position);
263 }
264
266 void erase( typename ObjectVector<TYPE>::iterator first,
267 typename ObjectVector<TYPE>::iterator last ) {
268 for(auto i = first; i != last; i++ ) {
269 // Set the back pointer to 0 to avoid repetitional searching
270 // for the object in the container, and deleting the object
271 (*i)->setParent(nullptr);
272 delete *i;
273 }
274 // Removing from the container itself
275 m_vector.erase(first, last);
276 }
277
279 void clear() {
280 erase(begin(), end());
281 }
282
284 typename ObjectVector<TYPE>::reference
285 operator[] (typename ObjectVector<TYPE>::size_type n) {
286 return m_vector[n];
287 }
288
290 typename ObjectVector<TYPE>::const_reference
291 operator[] (typename ObjectVector<TYPE>::size_type n) const {
292 return m_vector[n];
293 }
294
298 long index( const ContainedObject* obj ) const override {
299 auto i = std::find_if( begin(), end(), [&](const ContainedObject *o)
300 { return o == obj; } );
301 return i!=end() ? std::distance( begin(), i ) : -1 ;
302 }
303
305 ContainedObject* containedObject( long dist ) const override {
306 return m_vector[dist];
307 }
308
310 std::ostream& fillStream( std::ostream& s ) const override {
311 s << "class ObjectVector : size = "
312 << std::setw(12)
313 << size() << "\n";
314 // Output the base class
315 //ObjectContainerBase::fillStream(s);
316 if ( !empty() ) {
317 s << "\nContents of the STL vector :";
318 long count = 0;
319 for(const auto& i : m_vector) {
320 s << "\nIndex "
321 << std::setw(12)
322 << count++
323 << " of object of type " << *i;
324 }
325 }
326 return s;
327 }
328
329 private:
331 std::vector<TYPE*> m_vector;
332 };
333
334 #endif // GAUDIKERNEL_OBJECTVECTOR_H
ObjectVector::end
ObjectVector< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectVector.h:104
StatusCode::SUCCESS
Definition: StatusCode.h:30
ClassID.h
Kernel.h
ObjectVector::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectVector.h:279
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:204
StatusCode::FAILURE
Definition: StatusCode.h:29
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:31
ObjectVector::clID
const CLID & clID() const override
Retrieve class ID.
Definition: ObjectVector.h:83
ObjectVector::back
ObjectVector< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectVector.h:185
ObjectVector::begin
ObjectVector< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectVector.h:94
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:254
ObjectVector::rbegin
ObjectVector< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectVector.h:114
ObjectVector::begin
ObjectVector< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectVector.h:99
ObjectVector::const_iterator
std::vector< TYPE * >::const_iterator const_iterator
Definition: ObjectVector.h:48
ObjectVector::m_vector
std::vector< TYPE * > m_vector
The STL vector itself.
Definition: ObjectVector.h:331
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:195
ObjectVector
ObjectVector is one of the basic Gaudi container classes capable of being registered in Data Stores...
Definition: ContainedObject.h:11
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:305
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:266
ObjectVector::const_reference
std::vector< TYPE * >::const_reference const_reference
Definition: ObjectVector.h:45
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:130
std::vector::end
T end(T...args)
ObjectVector::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectVector.h:57
StreamBuffer.h
std::setw
T setw(T...args)
ObjectVector::ObjectVector
ObjectVector(ObjectVector &&rhs)
Definition: ObjectVector.h:65
ObjectVector::const_reverse_iterator
std::vector< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectVector.h:51
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:217
ObjectVector::front
ObjectVector< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectVector.h:175
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:180
ObjectVector::reverse_iterator
std::vector< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectVector.h:50
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:285
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:298
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:154
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:47
ObjectVector::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectVector.h:170
ObjectVector::~ObjectVector
~ObjectVector() override
Destructor.
Definition: ObjectVector.h:73
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:165
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:120
ObjectVector::reference
std::vector< TYPE * >::reference reference
Definition: ObjectVector.h:44
ObjectVector::insert
ObjectVector< TYPE >::iterator insert(typename ObjectVector< TYPE >::iterator position, typename ObjectVector< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectVector.h:247
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:29
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:148
std::vector
STL class.
ObjectVector::back
ObjectVector< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectVector.h:190
ObjectVector::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectVector.h:310
ObjectVector::value_type
std::vector< TYPE * >::value_type value_type
Definition: ObjectVector.h:42
ObjectVector::numberOfObjects
ObjectVector< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectVector.h:143
std::vector::begin
T begin(T...args)
ObjectVector::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectVector.h:56
std::vector::max_size
T max_size(T...args)
std::vector::back
T back(T...args)
gaudirun.s
string s
Definition: gaudirun.py:245
ObjectVector::contained_type
TYPE contained_type
Definition: ObjectVector.h:41
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:125
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:138
ObjectVector::end
ObjectVector< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectVector.h:109
std::ostream
STL class.
ObjectVector::classID
static const CLID & classID()
Retrieve class ID.
Definition: ObjectVector.h:87
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:219
std::vector::reserve
T reserve(T...args)
std::vector::rbegin
T rbegin(T...args)