Gaudi Framework, version v20r2

Generated: 18 Jul 2008

ObjectList.h

Go to the documentation of this file.
00001 // $Header: /local/reps/Gaudi/GaudiKernel/GaudiKernel/ObjectList.h,v 1.9 2005/07/21 16:31:02 hmd Exp $
00002 #ifndef GAUDIKERNEL_OBJECTLIST_H
00003 #define GAUDIKERNEL_OBJECTLIST_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 <list>
00013 #include <iomanip>
00014 
00015 
00016 // Definition of the CLID for this class  defined in ClassID.h
00017 // static const CLID CLID_ObjectList = (1<<18);  // ObjectList   (bit 18 set)
00018 
00037 template <class TYPE>
00038 class ObjectList : public ObjectContainerBase {
00039 
00040 public:
00041   typedef TYPE                                                 contained_type;
00042   typedef typename std::list<TYPE*>::value_type                value_type;
00043 
00044   typedef typename std::list<TYPE*>::reference                 reference;
00045   typedef typename std::list<TYPE*>::const_reference           const_reference;
00046 
00047   typedef typename std::list<TYPE*>::size_type                 size_type;
00048  
00049   typedef typename std::list<TYPE*>::iterator                  iterator;
00050   typedef typename std::list<TYPE*>::const_iterator            const_iterator;
00051 
00052   typedef typename std::list<TYPE*>::reverse_iterator          reverse_iterator;
00053   typedef typename std::list<TYPE*>::const_reverse_iterator    const_reverse_iterator;
00054 
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   ObjectList()
00066     : m_list(0) { }
00067   ObjectList( const char* name )
00068     : m_list(0) { }
00070   ObjectList( const ObjectList<TYPE>& value )
00071     : m_list(value.m_list) { }
00072 
00074   virtual ~ObjectList() {
00075     clear();
00076   }
00077 
00079   virtual const CLID& clID() const { 
00080     return ObjectList<TYPE>::classID(); 
00081   }
00082   static const CLID& classID() {
00083     static CLID clid = TYPE::classID() + CLID_ObjectList; 
00084     return clid;
00085   }
00086 
00088   const ObjectList<TYPE>& operator = (const ObjectList<TYPE> &right) {
00089     this->processingVersion = right.m_processingVersion;
00090     this->detectorDataObject = right.m_detectorDataObject;
00091     m_list = right.m_list;
00092     return *this;
00093   }
00094 
00096   typename ObjectList<TYPE>::iterator begin () {
00097     return m_list.begin();    
00098   }
00099 
00101   typename ObjectList<TYPE>::const_iterator begin () const {
00102     return m_list.begin();
00103   }
00104 
00106   typename ObjectList<TYPE>::iterator end () {
00107     return m_list.end();
00108   }
00109 
00111   typename ObjectList<TYPE>::const_iterator end () const {
00112     return m_list.end();
00113   }
00114 
00117   typename ObjectList<TYPE>::reverse_iterator rbegin () {
00118     return m_list.rbegin();
00119   }
00120 
00122   typename ObjectList<TYPE>::const_reverse_iterator rbegin () const {
00123     return m_list.rbegin();
00124   }
00125 
00127   typename ObjectList<TYPE>::reverse_iterator rend () {
00128     return m_list.rend();
00129   }
00130 
00132   typename ObjectList<TYPE>::const_reverse_iterator rend () const {
00133     return m_list.rend();
00134   }
00135 
00138   typename ObjectList<TYPE>::size_type size () const {
00139     return m_list.size();
00140   }
00142   virtual long numberOfObjects() const {
00143     return m_list.size();
00144   }
00145 
00147   typename ObjectList<TYPE>::size_type max_size () const {
00148     return m_list.max_size();
00149   }
00150 
00152   bool empty () const {
00153     return m_list.empty();
00154   }
00155 
00157   typename ObjectList<TYPE>::reference front () {
00158     return m_list.front();
00159   }
00160 
00162   typename ObjectList<TYPE>::const_reference front () const {
00163     return m_list.front();
00164   }
00165 
00167   typename ObjectList<TYPE>::reference back () {
00168     return m_list.back();
00169   }
00170 
00172   typename ObjectList<TYPE>::const_reference back () const {
00173     return m_list.back();
00174   }
00175 
00177   void push_back( typename ObjectList<TYPE>::const_reference value ) {
00178     if( 0 != value->parent() ) {
00179       const_cast<ObjectContainerBase*>(value->parent())->remove(value);
00180     }
00181     value->setParent(this);
00182     m_list.push_back(value);
00183   }
00184 
00186   virtual long add(ContainedObject* pObject) {
00187     try {
00188       typename ObjectList<TYPE>::value_type ptr =
00189             dynamic_cast<typename ObjectList<TYPE>::value_type>(pObject);
00190       if ( 0 != ptr ) {
00191         push_back(ptr);
00192         return m_list.size()-1;
00193       }
00194     }
00195     catch(...) {
00196     }
00197     return -1;
00198   }
00199 
00202   void pop_back () {
00203     typename ObjectList<TYPE>::value_type position = m_list.back();
00204     // Set the back pointer to 0 to avoid repetitional searching
00205     // for the object in the container, and deleting the object
00206     position->setParent (0);
00207     delete position;
00208     // Removing from the container itself
00209     m_list.pop_back();
00210   }
00211 
00214   virtual long remove(ContainedObject* value) {
00215     // Find the object of value value
00216     long idx = 0;
00217     typename ObjectList<TYPE>::iterator   iter;
00218     for( iter = begin(); iter != end(); iter++, idx++ )  {
00219       if( value == *iter ) {
00220         break;
00221       }
00222     }
00223     if( end() == iter )  {
00224       // Object cannot be released from the conatiner,
00225       // as it is not contained in it
00226       return -1;
00227     }
00228     else  {
00229       // Set the back pointer to 0 to avoid repetitional searching
00230       // for the object in the container and deleting the object
00231       (*iter)->setParent (0);
00232       erase(iter);
00233       return idx;
00234     }
00235   }
00236 
00238   typename ObjectList<TYPE>::iterator insert( typename ObjectList<TYPE>::iterator position,
00239                                      typename ObjectList<TYPE>::const_reference value ) {
00240     value->setParent(this);
00241     typename ObjectList<TYPE>::iterator i = m_list.insert(position, value);
00242     return i;
00243   }
00244 
00246   void erase( typename ObjectList<TYPE>::iterator position ) {
00247     if( 0 != (*position)->parent() ) {
00248       // Set the back pointer to 0 to avoid repetitional searching
00249       // for the object in the container, and deleting the object
00250       (*position)->setParent (0);
00251       delete *position;
00252     }
00253     // Removing from the container itself
00254     m_list.erase(position);
00255   }
00256 
00258   void erase( typename ObjectList<TYPE>::iterator first,
00259               typename ObjectList<TYPE>::iterator last ) {
00260     for( typename ObjectList<TYPE>::iterator iter = first; iter != last; iter++ )  {
00261       // Set the back pointer to 0 to avoid repetitional searching
00262       // for the object in the container, and deleting the object
00263       (*iter)->setParent (0);
00264       delete *iter;
00265     }
00266     // Removing from the container itself
00267     m_list.erase(first, last);
00268   }
00269 
00271   void clear()    {
00272     erase(begin(), end());
00273   }
00274 
00277   virtual long index( const ContainedObject* obj ) const {
00278     long i = 0;
00279     typename ObjectList<TYPE>::const_iterator   iter;
00280     for( iter = begin(); iter != end(); iter++ )  {
00281       if( *iter == obj ) {
00282         return i;
00283       }
00284       i++;
00285     }
00286     return -1;
00287   }
00288 
00290   virtual ContainedObject* containedObject( long dist ) const {
00291     long i = 0;
00292     typename ObjectList<TYPE>::const_iterator   iter;
00293     for( iter = begin(); iter != end(); iter++ )  {
00294       if( dist == i ) {
00295         return *iter;
00296       }
00297       i++;
00298     }
00299     return 0;
00300   }
00301 
00303   virtual std::ostream& fillStream( std::ostream& s ) const                    {
00304     s << "class ObjectList :    size = "
00305       << std::setw(12)
00306       << size() << "\n";
00307     // Output the base class
00308     //ObjectContainerBase::fillStream(s);
00309     if ( 0 != size() ) {
00310       s << "\nContents of the STL list :";
00311       long   count = 0;
00312       typename ObjectList<TYPE>::const_iterator iter;
00313       for( iter = m_list.begin(); iter != m_list.end(); iter++, count++ ) {
00314         s << "\nIndex "
00315           << std::setw(12)
00316           << count
00317           << " of object of type "<< **iter;
00318       }
00319     }
00320     return s;
00321   }
00322 
00323 private:
00324 
00326   std::list<TYPE*> m_list;
00327 };
00328 
00329 
00330 #endif    // GAUDI_OBJECTLIST_H

Generated at Fri Jul 18 11:59:21 2008 for Gaudi Framework, version v20r2 by Doxygen version 1.5.1 written by Dimitri van Heesch, © 1997-2004