LCIO  "2.7.4"
 All Classes Namespaces Functions Variables Typedefs Friends Pages
LCIterator.h
1 // -*- C++ -*-
2 #ifndef UTIL_LCIterator_include
3 #define UTIL_LCIterator_include
4 
5 #include <string>
6 #include <sstream>
7 #include <typeinfo>
8 #include "EVENT/LCEvent.h"
9 #include "EVENT/LCCollection.h"
10 #include "lcio.h"
11 
12 namespace UTIL {
13 
38  template <class T>
39  class LCIterator{
40 
41  LCIterator<T>() {}
42 
43  public:
44 
50  LCIterator<T>( EVENT::LCEvent* evt, const std::string& name ) : _i(0) {
51 
52  _col = 0 ;
53 
54  try{
55 
56  _col = evt->getCollection( name ) ;
57 
58  } catch( EVENT::DataNotAvailableException& ) { }
59 
60  _n = (_col ? _col->getNumberOfElements() : 0 ) ;
61 
62  if( _n > 0 ){
63 
64  T* t = dynamic_cast<T*>( _col->getElementAt(0) );
65 
66  if( t == 0 ){
67 
68  std::stringstream s ;
69  s << " invalid iterator type : " << typeid( t ).name() << " for collection " << name << std::endl ;
70  throw lcio::Exception( s.str() ) ;
71  }
72  }
73  }
74 
75 
78  LCIterator<T>( EVENT::LCCollection* col) : _i(0) , _col( col ) {
79 
80  _n = (_col ? _col->getNumberOfElements() : 0 ) ;
81 
82  if( _n > 0 ){
83 
84  T* t = dynamic_cast<T*>( _col->getElementAt(0) );
85 
86  if( t == 0 ){
87 
88  std::stringstream s ;
89  s << " invalid iterator type : " << typeid( t ).name() << " for collection " << std::endl ;
90  throw lcio::Exception( s.str() ) ;
91  }
92  }
93  }
94 
97  T* next(){
98 
99  if( _i < _n )
100  return (T*)_col->getElementAt( _i++ ) ;
101  // return dynamic_cast<T*>( _col->getElementAt( _i++ ) ) ;
102  else
103  return 0 ;
104  }
105 
108  int size() { return _n ; }
109 
110 
113  EVENT::LCCollection* operator->() { return _col ; }
114 
117  EVENT::LCCollection* operator()() { return _col ; }
118 
119  private:
120  int _n, _i ;
121  EVENT::LCCollection* _col ;
122  } ;
123 
124 } // namespace UTIL
125 
126 #endif
Base exception class for LCIO - all other exceptions extend this.
Definition: Exceptions.h:21
EVENT::LCCollection * operator()()
Return pointer to LCCollection, e.g.
Definition: LCIterator.h:117
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
T * next()
Returns the next element as long as there is one, otherwise 0 is returned.
Definition: LCIterator.h:97
EVENT::LCCollection * operator->()
Serves as a handle to the LCCollection itself, to provide access to the collection parameters etc...
Definition: LCIterator.h:113
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
The main event interface.
Definition: LCEvent.h:30
EventException used for data not available.
Definition: Exceptions.h:60
The generic collection used in LCIO.
Definition: LCCollection.h:29
int size()
Size of the collection.
Definition: LCIterator.h:108
Simple convenient iterator class for LCCollections that saves some boiler plate code.
Definition: LCIterator.h:39