00001 // $Header: /local/reps/Gaudi/GaudiKernel/GaudiKernel/ObjectList.h,v 1.10 2008/10/09 16:46:49 marcocle 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*>::iterator iterator; 00048 typedef typename std::list<TYPE*>::const_iterator const_iterator; 00049 00050 typedef typename std::list<TYPE*>::reverse_iterator reverse_iterator; 00051 typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator; 00052 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 ObjectList() 00064 : m_list(0) { } 00065 ObjectList( const char* name ) 00066 : m_list(0) { } 00068 ObjectList( const ObjectList<TYPE>& value ) 00069 : m_list(value.m_list) { } 00070 00072 virtual ~ObjectList() { 00073 clear(); 00074 } 00075 00077 virtual const CLID& clID() const { 00078 return ObjectList<TYPE>::classID(); 00079 } 00080 static const CLID& classID() { 00081 static CLID clid = TYPE::classID() + CLID_ObjectList; 00082 return clid; 00083 } 00084 00086 const ObjectList<TYPE>& operator = (const ObjectList<TYPE> &right) { 00087 this->processingVersion = right.m_processingVersion; 00088 this->detectorDataObject = right.m_detectorDataObject; 00089 m_list = right.m_list; 00090 return *this; 00091 } 00092 00094 typename ObjectList<TYPE>::iterator begin () { 00095 return m_list.begin(); 00096 } 00097 00099 typename ObjectList<TYPE>::const_iterator begin () const { 00100 return m_list.begin(); 00101 } 00102 00104 typename ObjectList<TYPE>::iterator end () { 00105 return m_list.end(); 00106 } 00107 00109 typename ObjectList<TYPE>::const_iterator end () const { 00110 return m_list.end(); 00111 } 00112 00115 typename ObjectList<TYPE>::reverse_iterator rbegin () { 00116 return m_list.rbegin(); 00117 } 00118 00120 typename ObjectList<TYPE>::const_reverse_iterator rbegin () const { 00121 return m_list.rbegin(); 00122 } 00123 00125 typename ObjectList<TYPE>::reverse_iterator rend () { 00126 return m_list.rend(); 00127 } 00128 00130 typename ObjectList<TYPE>::const_reverse_iterator rend () const { 00131 return m_list.rend(); 00132 } 00133 00136 typename ObjectList<TYPE>::size_type size () const { 00137 return m_list.size(); 00138 } 00140 virtual typename ObjectList<TYPE>::size_type numberOfObjects() const { 00141 return m_list.size(); 00142 } 00143 00145 typename ObjectList<TYPE>::size_type max_size () const { 00146 return m_list.max_size(); 00147 } 00148 00150 bool empty () const { 00151 return m_list.empty(); 00152 } 00153 00155 typename ObjectList<TYPE>::reference front () { 00156 return m_list.front(); 00157 } 00158 00160 typename ObjectList<TYPE>::const_reference front () const { 00161 return m_list.front(); 00162 } 00163 00165 typename ObjectList<TYPE>::reference back () { 00166 return m_list.back(); 00167 } 00168 00170 typename ObjectList<TYPE>::const_reference back () const { 00171 return m_list.back(); 00172 } 00173 00175 void push_back( typename ObjectList<TYPE>::const_reference value ) { 00176 if( 0 != value->parent() ) { 00177 const_cast<ObjectContainerBase*>(value->parent())->remove(value); 00178 } 00179 value->setParent(this); 00180 m_list.push_back(value); 00181 } 00182 00184 virtual long add(ContainedObject* pObject) { 00185 try { 00186 typename ObjectList<TYPE>::value_type ptr = 00187 dynamic_cast<typename ObjectList<TYPE>::value_type>(pObject); 00188 if ( 0 != ptr ) { 00189 push_back(ptr); 00190 return m_list.size()-1; 00191 } 00192 } 00193 catch(...) { 00194 } 00195 return -1; 00196 } 00197 00200 void pop_back () { 00201 typename ObjectList<TYPE>::value_type position = m_list.back(); 00202 // Set the back pointer to 0 to avoid repetitional searching 00203 // for the object in the container, and deleting the object 00204 position->setParent (0); 00205 delete position; 00206 // Removing from the container itself 00207 m_list.pop_back(); 00208 } 00209 00212 virtual long remove(ContainedObject* value) { 00213 // Find the object of value value 00214 long idx = 0; 00215 typename ObjectList<TYPE>::iterator iter; 00216 for( iter = begin(); iter != end(); iter++, idx++ ) { 00217 if( value == *iter ) { 00218 break; 00219 } 00220 } 00221 if( end() == iter ) { 00222 // Object cannot be released from the conatiner, 00223 // as it is not contained in it 00224 return -1; 00225 } 00226 else { 00227 // Set the back pointer to 0 to avoid repetitional searching 00228 // for the object in the container and deleting the object 00229 (*iter)->setParent (0); 00230 erase(iter); 00231 return idx; 00232 } 00233 } 00234 00236 typename ObjectList<TYPE>::iterator insert( typename ObjectList<TYPE>::iterator position, 00237 typename ObjectList<TYPE>::const_reference value ) { 00238 value->setParent(this); 00239 typename ObjectList<TYPE>::iterator i = m_list.insert(position, value); 00240 return i; 00241 } 00242 00244 void erase( typename ObjectList<TYPE>::iterator position ) { 00245 if( 0 != (*position)->parent() ) { 00246 // Set the back pointer to 0 to avoid repetitional searching 00247 // for the object in the container, and deleting the object 00248 (*position)->setParent (0); 00249 delete *position; 00250 } 00251 // Removing from the container itself 00252 m_list.erase(position); 00253 } 00254 00256 void erase( typename ObjectList<TYPE>::iterator first, 00257 typename ObjectList<TYPE>::iterator last ) { 00258 for( typename ObjectList<TYPE>::iterator iter = first; iter != last; iter++ ) { 00259 // Set the back pointer to 0 to avoid repetitional searching 00260 // for the object in the container, and deleting the object 00261 (*iter)->setParent (0); 00262 delete *iter; 00263 } 00264 // Removing from the container itself 00265 m_list.erase(first, last); 00266 } 00267 00269 void clear() { 00270 erase(begin(), end()); 00271 } 00272 00275 virtual long index( const ContainedObject* obj ) const { 00276 long i = 0; 00277 typename ObjectList<TYPE>::const_iterator iter; 00278 for( iter = begin(); iter != end(); iter++ ) { 00279 if( *iter == obj ) { 00280 return i; 00281 } 00282 i++; 00283 } 00284 return -1; 00285 } 00286 00288 virtual ContainedObject* containedObject( long dist ) const { 00289 long i = 0; 00290 typename ObjectList<TYPE>::const_iterator iter; 00291 for( iter = begin(); iter != end(); iter++ ) { 00292 if( dist == i ) { 00293 return *iter; 00294 } 00295 i++; 00296 } 00297 return 0; 00298 } 00299 00301 virtual std::ostream& fillStream( std::ostream& s ) const { 00302 s << "class ObjectList : size = " 00303 << std::setw(12) 00304 << size() << "\n"; 00305 // Output the base class 00306 //ObjectContainerBase::fillStream(s); 00307 if ( 0 != size() ) { 00308 s << "\nContents of the STL list :"; 00309 long count = 0; 00310 typename ObjectList<TYPE>::const_iterator iter; 00311 for( iter = m_list.begin(); iter != m_list.end(); iter++, count++ ) { 00312 s << "\nIndex " 00313 << std::setw(12) 00314 << count 00315 << " of object of type "<< **iter; 00316 } 00317 } 00318 return s; 00319 } 00320 00321 private: 00322 00324 std::list<TYPE*> m_list; 00325 }; 00326 00327 00328 #endif // GAUDI_OBJECTLIST_H