| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

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

- Search for |

- File List
- File Members

# ObjectVector.h

Go to the documentation of this file.

    00001 // $Header: /local/reps/Gaudi/GaudiKernel/GaudiKernel/ObjectVector.h,v 1.10 2005/07/25 15:58:37 hmd Exp $
    00002 #ifndef GAUDIKERNEL_OBJECTVECTOR_H
    00003 #define GAUDIKERNEL_OBJECTVECTOR_H
    00004
    00005
    00006 // Include files
    00007 #include "GaudiKernel/Kernel.h"
    00008 #include "GaudiKernel/ClassID.h"
    00009 #include "GaudiKernel/StreamBuffer.h"
    00010 #include "GaudiKernel/ObjectContainerBase.h"
    00011
    00012 #include <vector>
    00013 #include <iomanip>
    00014
    00015
    00016 // Definition of the CLID for this class defined in ClassID.h
    00017 //static const CLID CLID_ObjectList = (1<<17);  // ObjectVector   (bit 17 set)
    00018
    00038 template <class TYPE>
    00039 class ObjectVector : public ObjectContainerBase {
    00040
    00041 public:
    00042   typedef TYPE                                                 contained_type;
    00043   typedef typename std::vector<TYPE*>::value_type              value_type;
    00044
    00045   typedef typename std::vector<TYPE*>::reference               reference;
    00046   typedef typename std::vector<TYPE*>::const_reference         const_reference;
    00047
    00048   typedef typename std::vector<TYPE*>::size_type               size_type;
    00049
    00050   typedef typename std::vector<TYPE*>::iterator                iterator;
    00051   typedef typename std::vector<TYPE*>::const_iterator          const_iterator;
    00052
    00053   typedef typename std::vector<TYPE*>::reverse_iterator        reverse_iterator;
    00054   typedef typename std::vector<TYPE*>::const_reverse_iterator  const_reverse_iterator;
    00055 #ifdef _WIN32
    00056   typedef typename std::vector<TYPE*>::_Tptr                   pointer;
    00057   typedef typename std::vector<TYPE*>::_Ctptr                  const_pointer;
    00058 #else
    00059   typedef typename std::vector<TYPE*>::pointer                 pointer;
    00060   typedef typename std::vector<TYPE*>::const_pointer           const_pointer;
    00061 #endif
    00062
    00063 public:
    00065   ObjectVector()
    00066     : m_vector(0) { }
    00067   ObjectVector( const char* /* name */ )
    00068     : m_vector(0) { }
    00070   ObjectVector( const ObjectVector<TYPE>& value )
    00071     : ObjectContainerBase(), m_vector(value.m_vector) { }
    00072
    00074   virtual ~ObjectVector() {
    00075     for( typename ObjectVector<TYPE>::iterator i = begin(); i != end(); i++ ) {
    00076       // Set the back pointer to 0 to avoid repetitional searching
    00077       // for the object in the container, and deleting the object
    00078       (*i)->setParent (0);
    00079       delete *i;
    00080     }
    00081   }
    00082
    00084   virtual const CLID& clID() const {
    00085     return ObjectVector<TYPE>::classID();
    00086   }
    00088   static const CLID& classID() {
    00089     static CLID clid = TYPE::classID() + CLID_ObjectVector;
    00090     return clid;
    00091   }
    00092
    00094   const ObjectVector<TYPE>& operator = (const ObjectVector<TYPE> &right) {
    00095     m_vector = right.m_vector;
    00096     return *this;
    00097   }
    00098
    00100   typename ObjectVector<TYPE>::iterator begin () {
    00101     return m_vector.begin();
    00102   }
    00103
    00105   typename ObjectVector<TYPE>::const_iterator begin () const {
    00106     return m_vector.begin();
    00107   }
    00108
    00110   typename ObjectVector<TYPE>::iterator end () {
    00111     return m_vector.end();
    00112   }
    00113
    00115   typename ObjectVector<TYPE>::const_iterator end () const {
    00116     return m_vector.end();
    00117   }
    00118
    00120   typename ObjectVector<TYPE>::reverse_iterator rbegin () {
    00121     return m_vector.rbegin();
    00122   }
    00123
    00126   typename ObjectVector<TYPE>::const_reverse_iterator rbegin () const {
    00127     return m_vector.rbegin();
    00128   }
    00129
    00131   typename ObjectVector<TYPE>::reverse_iterator rend () {
    00132     return m_vector.rend();
    00133   }
    00134
    00136   typename ObjectVector<TYPE>::const_reverse_iterator rend () const {
    00137     return m_vector.rend();
    00138   }
    00139
    00144   typename ObjectVector<TYPE>::size_type size () const {
    00145     return m_vector.size();
    00146   }
    00147
    00149   virtual long numberOfObjects() const {
    00150     return m_vector.size();
    00151   }
    00152
    00154   typename ObjectVector<TYPE>::size_type max_size () const {
    00155     return m_vector.max_size();
    00156   }
    00157
    00160   typename ObjectVector<TYPE>::size_type capacity () const {
    00161     return m_vector.capacity();
    00162   }
    00163
    00171   void reserve( typename ObjectVector<TYPE>::size_type value ) {
    00172     m_vector.reserve( value );
    00173   }
    00174
    00176   bool empty () const {
    00177     return m_vector.empty();
    00178   }
    00179
    00181   typename ObjectVector<TYPE>::reference front () {
    00182     return m_vector.front();
    00183   }
    00184
    00186   typename ObjectVector<TYPE>::const_reference front () const {
    00187     return m_vector.front();
    00188   }
    00189
    00191   typename ObjectVector<TYPE>::reference back () {
    00192     return m_vector.back();
    00193   }
    00194
    00196   typename ObjectVector<TYPE>::const_reference back () const {
    00197     return m_vector.back();
    00198   }
    00199
    00201   void push_back( typename ObjectVector<TYPE>::const_reference value )  {
    00202     if( 0 != value->parent() ) {
    00203       const_cast<ObjectContainerBase*>(value->parent())->remove(value);
    00204     }
    00205     value->setParent(this);
    00206     m_vector.push_back(value);
    00207   }
    00208
    00210   virtual long add(ContainedObject* pObject) {
    00211     try {
    00212       typename ObjectVector<TYPE>::value_type ptr =
    00213             dynamic_cast<typename ObjectVector<TYPE>::value_type>(pObject);
    00214       if ( 0 != ptr ) {
    00215         push_back(ptr);
    00216         return m_vector.size()-1;
    00217       }
    00218     }
    00219     catch(...) {
    00220     }
    00221     return -1;
    00222   }
    00223
    00226   void pop_back () {
    00227     typename ObjectVector<TYPE>::value_type position = m_vector.back();
    00228     // Set the back pointer to 0 to avoid repetitional searching
    00229     // for the object in the container, and deleting the object
    00230     position->setParent (0);
    00231     delete position;
    00232     // Removing from the container itself
    00233     m_vector.pop_back();
    00234   }
    00235
    00238   virtual long remove(ContainedObject* value) {
    00239     // Find the object of the value value
    00240     typename ObjectVector<TYPE>::iterator i;
    00241     for( i = begin(); i != end(); i++ ) {
    00242       if( value == *i ) {
    00243         break;
    00244       }
    00245     }
    00246     if( end() == i )  {
    00247       // Object cannot be released from the conatiner,
    00248       // as it is not contained in it
    00249       return StatusCode::FAILURE;
    00250     }
    00251     else  {
    00252       // Set the back pointer to 0 to avoid repetitional searching
    00253       // for the object in the container and deleting the object
    00254       (*i)->setParent (0);
    00255       erase(i);
    00256       return StatusCode::SUCCESS;
    00257     }
    00258   }
    00259
    00261   typename ObjectVector<TYPE>::iterator insert( typename ObjectVector<TYPE>::iterator position,
    00262                                                 typename ObjectVector<TYPE>::const_reference value ) {
    00263     value->setParent(this);
    00264     typename ObjectVector<TYPE>::iterator i = m_vector.insert(position, value);
    00265     return i;
    00266   }
    00267
    00269   void erase( typename ObjectVector<TYPE>::iterator position ) {
    00270     if( 0 != (*position)->parent() ) {
    00271       // Set the back pointer to 0 to avoid repetitional searching
    00272       // for the object in the container, and deleting the object
    00273       (*position)->setParent (0);
    00274       delete *position;
    00275     }
    00276     // Removing from the container itself
    00277     m_vector.erase(position);
    00278   }
    00279
    00281   void erase( typename ObjectVector<TYPE>::iterator first,
    00282               typename ObjectVector<TYPE>::iterator last ) {
    00283     for(typename ObjectVector<TYPE>::iterator i = first; i != last; i++ ) {
    00284       // Set the back pointer to 0 to avoid repetitional searching
    00285       // for the object in the container, and deleting the object
    00286       (*i)->setParent(0);
    00287       delete *i;
    00288     }
    00289     // Removing from the container itself
    00290     m_vector.erase(first, last);
    00291   }
    00292
    00294   void clear()    {
    00295     erase(begin(), end());
    00296   }
    00297
    00299   typename ObjectVector<TYPE>::reference
    00300     operator[] (typename ObjectVector<TYPE>::size_type n) {
    00301     return m_vector[n];
    00302   }
    00303
    00305   typename ObjectVector<TYPE>::const_reference
    00306     operator[] (typename ObjectVector<TYPE>::size_type n) const {
    00307     return m_vector[n];
    00308   }
    00309
    00313   virtual long index( const ContainedObject* obj ) const {
    00314     long i;
    00315     for( i = 0; i < (long)m_vector.size(); i++ ) {
    00316       if( m_vector[i] == obj ) {
    00317         return i;
    00318       }
    00319     }
    00320     return -1;
    00321   }
    00322
    00324   virtual ContainedObject* containedObject( long dist ) const {
    00325     return m_vector[dist];
    00326   }
    00327
    00329   virtual std::ostream& fillStream( std::ostream& s ) const {
    00330     s << "class ObjectVector :    size = "
    00331       << std::setw(12)
    00332       << size() << "\n";
    00333     // Output the base class
    00334     //ObjectContainerBase::fillStream(s);
    00335     if ( 0 != size() ) {
    00336       s << "\nContents of the STL vector :";
    00337       long   count = 0;
    00338       typename ObjectVector<TYPE>::const_iterator iter;
    00339       for( iter = m_vector.begin(); iter != m_vector.end(); iter++, count++ ) {
    00340         s << "\nIndex "
    00341           << std::setw(12)
    00342           << count
    00343           << " of object of type " << **iter;
    00344       }
    00345     }
    00346     return s;
    00347   }
    00348
    00349 private:
    00351   std::vector<TYPE*>     m_vector;
    00352 };
    00353
    00354 #endif    // GAUDIKERNEL_OBJECTVECTOR_H
    00355