LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LCIterator.h
Go to the documentation of this file.
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 
42 
43  public:
44 
50  LCIterator<T>( EVENT::LCEvent* evt, const std::string& name ) : _i(0), _col(0) {
51 
52 
53  try{
54 
55  _col = evt->getCollection( name ) ;
56 
57  } catch( EVENT::DataNotAvailableException& ) { }
58 
59  _n = (_col ? _col->getNumberOfElements() : 0 ) ;
60 
61  if( _n > 0 ){
62 
63  T* t = dynamic_cast<T*>( _col->getElementAt(0) );
64 
65  if( t == 0 ){
66 
68  s << " invalid iterator type : " << typeid( t ).name() << " for collection " << name << std::endl ;
69  throw lcio::Exception( s.str() ) ;
70  }
71  }
72  }
73 
74 
77  LCIterator<T>( const EVENT::LCCollection* col) : _i(0) , _col( col ) {
78 
79  _n = (_col ? _col->getNumberOfElements() : 0 ) ;
80 
81  if( _n > 0 ){
82 
83  T* t = dynamic_cast<T*>( _col->getElementAt(0) );
84 
85  if( t == 0 ){
86 
88  s << " invalid iterator type : " << typeid( t ).name() << " for collection " << std::endl ;
89  throw lcio::Exception( s.str() ) ;
90  }
91  }
92  }
93 
96  T* next(){
97 
98  if( _i < _n )
99  return (T*)_col->getElementAt( _i++ ) ;
100  // return dynamic_cast<T*>( _col->getElementAt( _i++ ) ) ;
101  else
102  return 0 ;
103  }
104 
107  int size() { return _n ; }
108 
109 
112  const EVENT::LCCollection* operator->() { return _col ; }
113 
116  const EVENT::LCCollection* operator()() { return _col ; }
117 
118  private:
119  int _n{0}, _i ;
121  } ;
122 
123 } // namespace UTIL
124 
125 #endif
T endl(T...args)
const EVENT::LCCollection * operator()()
Return pointer to LCCollection, e.g.
Definition: LCIterator.h:116
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
STL class.
T * next()
Returns the next element as long as there is one, otherwise 0 is returned.
Definition: LCIterator.h:96
const EVENT::LCCollection * _col
Definition: LCIterator.h:120
T str(T...args)
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
The main event interface.
Definition: LCEvent.h:31
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:107
const EVENT::LCCollection * operator->()
Serves as a handle to the LCCollection itself, to provide access to the collection parameters etc...
Definition: LCIterator.h:112
Simple convenient iterator class for LCCollections that saves some boiler plate code.
Definition: LCIterator.h:39