| # Gaudi Framework, version v21r8 |

Home |  | Generated: 17 Mar 2010

# ObjectVector.h

Go to the documentation of this file.

    00001 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/GaudiKernel/ObjectVector.h,v 1.11 2008/10/09 16:46:49 marcocle 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*>::iterator                iterator;
    00049   typedef typename std::vector<TYPE*>::const_iterator          const_iterator;
    00050
    00051   typedef typename std::vector<TYPE*>::reverse_iterator        reverse_iterator;
    00052   typedef typename std::vector<TYPE*>::const_reverse_iterator  const_reverse_iterator;
    00053 #ifdef _WIN32
    00054   typedef typename std::vector<TYPE*>::_Tptr                   pointer;
    00055   typedef typename std::vector<TYPE*>::_Ctptr                  const_pointer;
    00056 #else
    00057   typedef typename std::vector<TYPE*>::pointer                 pointer;
    00058   typedef typename std::vector<TYPE*>::const_pointer           const_pointer;
    00059 #endif
    00060
    00061 public:
    00063   ObjectVector()
    00064     : m_vector(0) { }
    00065   ObjectVector( const char* /* name */ )
    00066     : m_vector(0) { }
    00068   ObjectVector( const ObjectVector<TYPE>& value )
    00069     : ObjectContainerBase(), m_vector(value.m_vector) { }
    00070
    00072   virtual ~ObjectVector() {
    00073     for( typename ObjectVector<TYPE>::iterator i = begin(); i != end(); i++ ) {
    00074       // Set the back pointer to 0 to avoid repetitional searching
    00075       // for the object in the container, and deleting the object
    00076       (*i)->setParent (0);
    00077       delete *i;
    00078     }
    00079   }
    00080
    00082   virtual const CLID& clID() const {
    00083     return ObjectVector<TYPE>::classID();
    00084   }
    00086   static const CLID& classID() {
    00087     static CLID clid = TYPE::classID() + CLID_ObjectVector;
    00088     return clid;
    00089   }
    00090
    00092   const ObjectVector<TYPE>& operator = (const ObjectVector<TYPE> &right) {
    00093     m_vector = right.m_vector;
    00094     return *this;
    00095   }
    00096
    00098   typename ObjectVector<TYPE>::iterator begin () {
    00099     return m_vector.begin();
    00100   }
    00101
    00103   typename ObjectVector<TYPE>::const_iterator begin () const {
    00104     return m_vector.begin();
    00105   }
    00106
    00108   typename ObjectVector<TYPE>::iterator end () {
    00109     return m_vector.end();
    00110   }
    00111
    00113   typename ObjectVector<TYPE>::const_iterator end () const {
    00114     return m_vector.end();
    00115   }
    00116
    00118   typename ObjectVector<TYPE>::reverse_iterator rbegin () {
    00119     return m_vector.rbegin();
    00120   }
    00121
    00124   typename ObjectVector<TYPE>::const_reverse_iterator rbegin () const {
    00125     return m_vector.rbegin();
    00126   }
    00127
    00129   typename ObjectVector<TYPE>::reverse_iterator rend () {
    00130     return m_vector.rend();
    00131   }
    00132
    00134   typename ObjectVector<TYPE>::const_reverse_iterator rend () const {
    00135     return m_vector.rend();
    00136   }
    00137
    00142   typename ObjectVector<TYPE>::size_type size () const {
    00143     return m_vector.size();
    00144   }
    00145
    00147   virtual typename ObjectVector<TYPE>::size_type numberOfObjects() const {
    00148     return m_vector.size();
    00149   }
    00150
    00152   typename ObjectVector<TYPE>::size_type max_size () const {
    00153     return m_vector.max_size();
    00154   }
    00155
    00158   typename ObjectVector<TYPE>::size_type capacity () const {
    00159     return m_vector.capacity();
    00160   }
    00161
    00169   void reserve( typename ObjectVector<TYPE>::size_type value ) {
    00170     m_vector.reserve( value );
    00171   }
    00172
    00174   bool empty () const {
    00175     return m_vector.empty();
    00176   }
    00177
    00179   typename ObjectVector<TYPE>::reference front () {
    00180     return m_vector.front();
    00181   }
    00182
    00184   typename ObjectVector<TYPE>::const_reference front () const {
    00185     return m_vector.front();
    00186   }
    00187
    00189   typename ObjectVector<TYPE>::reference back () {
    00190     return m_vector.back();
    00191   }
    00192
    00194   typename ObjectVector<TYPE>::const_reference back () const {
    00195     return m_vector.back();
    00196   }
    00197
    00199   void push_back( typename ObjectVector<TYPE>::const_reference value )  {
    00200     if( 0 != value->parent() ) {
    00201       const_cast<ObjectContainerBase*>(value->parent())->remove(value);
    00202     }
    00203     value->setParent(this);
    00204     m_vector.push_back(value);
    00205   }
    00206
    00208   virtual long add(ContainedObject* pObject) {
    00209     try {
    00210       typename ObjectVector<TYPE>::value_type ptr =
    00211             dynamic_cast<typename ObjectVector<TYPE>::value_type>(pObject);
    00212       if ( 0 != ptr ) {
    00213         push_back(ptr);
    00214         return m_vector.size()-1;
    00215       }
    00216     }
    00217     catch(...) {
    00218     }
    00219     return -1;
    00220   }
    00221
    00224   void pop_back () {
    00225     typename ObjectVector<TYPE>::value_type position = m_vector.back();
    00226     // Set the back pointer to 0 to avoid repetitional searching
    00227     // for the object in the container, and deleting the object
    00228     position->setParent (0);
    00229     delete position;
    00230     // Removing from the container itself
    00231     m_vector.pop_back();
    00232   }
    00233
    00236   virtual long remove(ContainedObject* value) {
    00237     // Find the object of the value value
    00238     typename ObjectVector<TYPE>::iterator i;
    00239     for( i = begin(); i != end(); i++ ) {
    00240       if( value == *i ) {
    00241         break;
    00242       }
    00243     }
    00244     if( end() == i )  {
    00245       // Object cannot be released from the conatiner,
    00246       // as it is not contained in it
    00247       return StatusCode::FAILURE;
    00248     }
    00249     else  {
    00250       // Set the back pointer to 0 to avoid repetitional searching
    00251       // for the object in the container and deleting the object
    00252       (*i)->setParent (0);
    00253       erase(i);
    00254       return StatusCode::SUCCESS;
    00255     }
    00256   }
    00257
    00259   typename ObjectVector<TYPE>::iterator insert( typename ObjectVector<TYPE>::iterator position,
    00260                                                 typename ObjectVector<TYPE>::const_reference value ) {
    00261     value->setParent(this);
    00262     typename ObjectVector<TYPE>::iterator i = m_vector.insert(position, value);
    00263     return i;
    00264   }
    00265
    00267   void erase( typename ObjectVector<TYPE>::iterator position ) {
    00268     if( 0 != (*position)->parent() ) {
    00269       // Set the back pointer to 0 to avoid repetitional searching
    00270       // for the object in the container, and deleting the object
    00271       (*position)->setParent (0);
    00272       delete *position;
    00273     }
    00274     // Removing from the container itself
    00275     m_vector.erase(position);
    00276   }
    00277
    00279   void erase( typename ObjectVector<TYPE>::iterator first,
    00280               typename ObjectVector<TYPE>::iterator last ) {
    00281     for(typename ObjectVector<TYPE>::iterator i = first; i != last; i++ ) {
    00282       // Set the back pointer to 0 to avoid repetitional searching
    00283       // for the object in the container, and deleting the object
    00284       (*i)->setParent(0);
    00285       delete *i;
    00286     }
    00287     // Removing from the container itself
    00288     m_vector.erase(first, last);
    00289   }
    00290
    00292   void clear()    {
    00293     erase(begin(), end());
    00294   }
    00295
    00297   typename ObjectVector<TYPE>::reference
    00298     operator[] (typename ObjectVector<TYPE>::size_type n) {
    00299     return m_vector[n];
    00300   }
    00301
    00303   typename ObjectVector<TYPE>::const_reference
    00304     operator[] (typename ObjectVector<TYPE>::size_type n) const {
    00305     return m_vector[n];
    00306   }
    00307
    00311   virtual long index( const ContainedObject* obj ) const {
    00312     long i;
    00313     for( i = 0; i < (long)m_vector.size(); i++ ) {
    00314       if( m_vector[i] == obj ) {
    00315         return i;
    00316       }
    00317     }
    00318     return -1;
    00319   }
    00320
    00322   virtual ContainedObject* containedObject( long dist ) const {
    00323     return m_vector[dist];
    00324   }
    00325
    00327   virtual std::ostream& fillStream( std::ostream& s ) const {
    00328     s << "class ObjectVector :    size = "
    00329       << std::setw(12)
    00330       << size() << "\n";
    00331     // Output the base class
    00332     //ObjectContainerBase::fillStream(s);
    00333     if ( 0 != size() ) {
    00334       s << "\nContents of the STL vector :";
    00335       long   count = 0;
    00336       typename ObjectVector<TYPE>::const_iterator iter;
    00337       for( iter = m_vector.begin(); iter != m_vector.end(); iter++, count++ ) {
    00338         s << "\nIndex "
    00339           << std::setw(12)
    00340           << count
    00341           << " of object of type " << **iter;
    00342       }
    00343     }
    00344     return s;
    00345   }
    00346
    00347 private:
    00349   std::vector<TYPE*>     m_vector;
    00350 };
    00351
    00352 #endif    // GAUDIKERNEL_OBJECTVECTOR_H
    00353