00001
00002 #ifndef UTIL_LCIterator_include
00003 #define UTIL_LCIterator_include
00004
00005 #include <string>
00006 #include <sstream>
00007 #include <typeinfo>
00008 #include "EVENT/LCEvent.h"
00009 #include "EVENT/LCCollection.h"
00010 #include "lcio.h"
00011
00012 namespace UTIL {
00013
00038 template <class T>
00039 class LCIterator{
00040
00041 LCIterator<T>() {}
00042
00043 public:
00044
00050 LCIterator<T>( EVENT::LCEvent* evt, const std::string& name ) : _i(0), _col(0) {
00051
00052
00053 try{
00054
00055 _col = evt->getCollection( name ) ;
00056
00057 } catch( EVENT::DataNotAvailableException& ) { }
00058
00059 _n = (_col ? _col->getNumberOfElements() : 0 ) ;
00060
00061 if( _n > 0 ){
00062
00063 T* t = dynamic_cast<T*>( _col->getElementAt(0) );
00064
00065 if( t == 0 ){
00066
00067 std::stringstream s ;
00068 s << " invalid iterator type : " << typeid( t ).name() << " for collection " << name << std::endl ;
00069 throw lcio::Exception( s.str() ) ;
00070 }
00071 }
00072 }
00073
00074
00077 LCIterator<T>( EVENT::LCCollection* col) : _i(0) , _col( col ) {
00078
00079 _n = (_col ? _col->getNumberOfElements() : 0 ) ;
00080
00081 if( _n > 0 ){
00082
00083 T* t = dynamic_cast<T*>( _col->getElementAt(0) );
00084
00085 if( t == 0 ){
00086
00087 std::stringstream s ;
00088 s << " invalid iterator type : " << typeid( t ).name() << " for collection " << std::endl ;
00089 throw lcio::Exception( s.str() ) ;
00090 }
00091 }
00092 }
00093
00096 T* next(){
00097
00098 if( _i < _n )
00099 return (T*)_col->getElementAt( _i++ ) ;
00100
00101 else
00102 return 0 ;
00103 }
00104
00107 int size() { return _n ; }
00108
00109
00112 EVENT::LCCollection* operator->() { return _col ; }
00113
00116 EVENT::LCCollection* operator()() { return _col ; }
00117
00118 private:
00119 int _n{0}, _i ;
00120 EVENT::LCCollection* _col ;
00121 } ;
00122
00123 }
00124
00125 #endif