| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ObjectVector.h
Go to the documentation of this file.
1 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/GaudiKernel/ObjectVector.h,v 1.11 2008/10/09 16:46:49 marcocle Exp $
2 #ifndef GAUDIKERNEL_OBJECTVECTOR_H
3 #define GAUDIKERNEL_OBJECTVECTOR_H
4
5
6 // Include files
7 #include "GaudiKernel/Kernel.h"
8 #include "GaudiKernel/ClassID.h"
9 #include "GaudiKernel/StreamBuffer.h"
10 #include "GaudiKernel/ObjectContainerBase.h"
11
12 #include <vector>
13 #include <iomanip>
14
15
16 // Definition of the CLID for this class defined in ClassID.h
17 //static const CLID CLID_ObjectList = (1<<17); // ObjectVector (bit 17 set)
18
38 template <class TYPE>
39 class ObjectVector : public ObjectContainerBase {
40
41 public:
42 typedef TYPE contained_type;
43 typedef typename std::vector<TYPE*>::value_type value_type;
44
45 typedef typename std::vector<TYPE*>::reference reference;
46 typedef typename std::vector<TYPE*>::const_reference const_reference;
47
48 typedef typename std::vector<TYPE*>::iterator iterator;
49 typedef typename std::vector<TYPE*>::const_iterator const_iterator;
50
51 typedef typename std::vector<TYPE*>::reverse_iterator reverse_iterator;
52 typedef typename std::vector<TYPE*>::const_reverse_iterator const_reverse_iterator;
53 #ifdef _WIN32
54 typedef typename std::vector<TYPE*>::_Tptr pointer;
55 typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
56 #else
57 typedef typename std::vector<TYPE*>::pointer pointer;
58 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
59 #endif
60
61 public:
63 ObjectVector()
64 : m_vector(0) { }
65 ObjectVector( const char* /* name */ )
66 : m_vector(0) { }
68 ObjectVector( const ObjectVector<TYPE>& value )
69 : ObjectContainerBase(), m_vector(value.m_vector) { }
70
72 virtual ~ObjectVector() {
73 for( typename ObjectVector<TYPE>::iterator i = begin(); i != end(); i++ ) {
74 // Set the back pointer to 0 to avoid repetitional searching
75 // for the object in the container, and deleting the object
76 (*i)->setParent (0);
77 delete *i;
78 }
79 }
80
82 virtual const CLID& clID() const {
83 return ObjectVector<TYPE>::classID();
84 }
86 static const CLID& classID() {
87 static CLID clid = TYPE::classID() + CLID_ObjectVector;
88 return clid;
89 }
90
92 const ObjectVector<TYPE>& operator = (const ObjectVector<TYPE> &right) {
93 m_vector = right.m_vector;
94 return *this;
95 }
96
98 typename ObjectVector<TYPE>::iterator begin () {
99 return m_vector.begin();
100 }
101
103 typename ObjectVector<TYPE>::const_iterator begin () const {
104 return m_vector.begin();
105 }
106
108 typename ObjectVector<TYPE>::iterator end () {
109 return m_vector.end();
110 }
111
113 typename ObjectVector<TYPE>::const_iterator end () const {
114 return m_vector.end();
115 }
116
118 typename ObjectVector<TYPE>::reverse_iterator rbegin () {
119 return m_vector.rbegin();
120 }
121
124 typename ObjectVector<TYPE>::const_reverse_iterator rbegin () const {
125 return m_vector.rbegin();
126 }
127
129 typename ObjectVector<TYPE>::reverse_iterator rend () {
130 return m_vector.rend();
131 }
132
134 typename ObjectVector<TYPE>::const_reverse_iterator rend () const {
135 return m_vector.rend();
136 }
137
142 typename ObjectVector<TYPE>::size_type size () const {
143 return m_vector.size();
144 }
145
147 virtual typename ObjectVector<TYPE>::size_type numberOfObjects() const {
148 return m_vector.size();
149 }
150
152 typename ObjectVector<TYPE>::size_type max_size () const {
153 return m_vector.max_size();
154 }
155
158 typename ObjectVector<TYPE>::size_type capacity () const {
159 return m_vector.capacity();
160 }
161
169 void reserve( typename ObjectVector<TYPE>::size_type value ) {
170 m_vector.reserve( value );
171 }
172
174 bool empty () const {
175 return m_vector.empty();
176 }
177
179 typename ObjectVector<TYPE>::reference front () {
180 return m_vector.front();
181 }
182
184 typename ObjectVector<TYPE>::const_reference front () const {
185 return m_vector.front();
186 }
187
189 typename ObjectVector<TYPE>::reference back () {
190 return m_vector.back();
191 }
192
194 typename ObjectVector<TYPE>::const_reference back () const {
195 return m_vector.back();
196 }
197
199 void push_back( typename ObjectVector<TYPE>::const_reference value ) {
200 if( 0 != value->parent() ) {
201 const_cast<ObjectContainerBase*>(value->parent())->remove(value);
202 }
203 value->setParent(this);
204 m_vector.push_back(value);
205 }
206
208 virtual long add(ContainedObject* pObject) {
209 try {
210 typename ObjectVector<TYPE>::value_type ptr =
211 dynamic_cast<typename ObjectVector<TYPE>::value_type>(pObject);
212 if ( 0 != ptr ) {
213 push_back(ptr);
214 return m_vector.size()-1;
215 }
216 }
217 catch(...) {
218 }
219 return -1;
220 }
221
224 void pop_back () {
225 typename ObjectVector<TYPE>::value_type position = m_vector.back();
226 // Set the back pointer to 0 to avoid repetitional searching
227 // for the object in the container, and deleting the object
228 position->setParent (0);
229 delete position;
230 // Removing from the container itself
231 m_vector.pop_back();
232 }
233
236 virtual long remove(ContainedObject* value) {
237 // Find the object of the value value
238 typename ObjectVector<TYPE>::iterator i;
239 for( i = begin(); i != end(); i++ ) {
240 if( value == *i ) {
241 break;
242 }
243 }
244 if( end() == i ) {
245 // Object cannot be released from the conatiner,
246 // as it is not contained in it
247 return StatusCode::FAILURE;
248 }
249 else {
250 // Set the back pointer to 0 to avoid repetitional searching
251 // for the object in the container and deleting the object
252 (*i)->setParent (0);
253 erase(i);
254 return StatusCode::SUCCESS;
255 }
256 }
257
259 typename ObjectVector<TYPE>::iterator insert( typename ObjectVector<TYPE>::iterator position,
260 typename ObjectVector<TYPE>::const_reference value ) {
261 value->setParent(this);
262 typename ObjectVector<TYPE>::iterator i = m_vector.insert(position, value);
263 return i;
264 }
265
267 void erase( typename ObjectVector<TYPE>::iterator position ) {
268 if( 0 != (*position)->parent() ) {
269 // Set the back pointer to 0 to avoid repetitional searching
270 // for the object in the container, and deleting the object
271 (*position)->setParent (0);
272 delete *position;
273 }
274 // Removing from the container itself
275 m_vector.erase(position);
276 }
277
279 void erase( typename ObjectVector<TYPE>::iterator first,
280 typename ObjectVector<TYPE>::iterator last ) {
281 for(typename ObjectVector<TYPE>::iterator i = first; i != last; i++ ) {
282 // Set the back pointer to 0 to avoid repetitional searching
283 // for the object in the container, and deleting the object
284 (*i)->setParent(0);
285 delete *i;
286 }
287 // Removing from the container itself
288 m_vector.erase(first, last);
289 }
290
292 void clear() {
293 erase(begin(), end());
294 }
295
297 typename ObjectVector<TYPE>::reference
298 operator[] (typename ObjectVector<TYPE>::size_type n) {
299 return m_vector[n];
300 }
301
303 typename ObjectVector<TYPE>::const_reference
304 operator[] (typename ObjectVector<TYPE>::size_type n) const {
305 return m_vector[n];
306 }
307
311 virtual long index( const ContainedObject* obj ) const {
312 long i;
313 for( i = 0; i < (long)m_vector.size(); i++ ) {
314 if( m_vector[i] == obj ) {
315 return i;
316 }
317 }
318 return -1;
319 }
320
322 virtual ContainedObject* containedObject( long dist ) const {
323 return m_vector[dist];
324 }
325
327 virtual std::ostream& fillStream( std::ostream& s ) const {
328 s << "class ObjectVector : size = "
329 << std::setw(12)
330 << size() << "\n";
331 // Output the base class
332 //ObjectContainerBase::fillStream(s);
333 if ( 0 != size() ) {
334 s << "\nContents of the STL vector :";
335 long count = 0;
336 typename ObjectVector<TYPE>::const_iterator iter;
337 for( iter = m_vector.begin(); iter != m_vector.end(); iter++, count++ ) {
338 s << "\nIndex "
339 << std::setw(12)
340 << count
341 << " of object of type " << **iter;
342 }
343 }
344 return s;
345 }
346
347 private:
349 std::vector<TYPE*> m_vector;
350 };
351
352 #endif // GAUDIKERNEL_OBJECTVECTOR_H
353