LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LCTypedVector.h
Go to the documentation of this file.
1 #ifndef LCTypedVector_h
2 #define LCTypedVector_h 1
3 
4 #include <vector>
5 
6 #include "EVENT/LCObject.h"
7 #include "EVENT/LCCollection.h"
8 #include "Exceptions.h"
9 
10 #include <sstream>
11 #include <typeinfo>
12 
13 
14 namespace UTIL{
15 
16 
28  template <class T>
29  class LCTypedVector : public std::vector<T*> {
30 
31  public:
32  LCTypedVector() = default ;
33  LCTypedVector(const LCTypedVector& ) = delete ;
34  LCTypedVector& operator=(const LCTypedVector& ) = delete ;
35 
36  LCTypedVector( EVENT::LCCollection* collection ) : _col( collection) {
37 
38  this->resize( _col->getNumberOfElements() ) ;
39 
40  for(int i=0;i<_col->getNumberOfElements();i++ ) {
41 
42  (*this)[i] = dynamic_cast<T*>( _col->getElementAt(i) ) ;
43 
44  // check the first element for the proper type
45  if( i == 0 && (*this)[i] == 0 ){
46 
47  std::stringstream str ;
48  str << "LCTypedVector: cannot convert " << _col->getTypeName() << " to "
49  << typeid(T).name() ;
50 
51  throw EVENT::Exception( str.str().c_str() ) ;
52  }
53  }
54  }
55 
56  EVENT::LCCollection* col() { return _col ; }
57 
58  protected:
60  } ;
61 
62 } // namespace
63 #endif
64 
65 
Base exception class for LCIO - all other exceptions extend this.
Definition: Exceptions.h:21
EVENT::LCCollection * _col
Definition: LCTypedVector.h:59
T * resize(T *...args)
LCTypedVector()=default
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
LCTypedVector(EVENT::LCCollection *collection)
Definition: LCTypedVector.h:36
LCTypedVector & operator=(const LCTypedVector &)=delete
T str(T...args)
virtual const std::string & getTypeName() const =0
Returns the type name of the collection - valid names are defined in LCIO.
EVENT::LCCollection * col()
Definition: LCTypedVector.h:56
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
Templated helper class that is an std::vector&lt;T*&gt; of the elements in the LCCollection.
Definition: LCTypedVector.h:29
The generic collection used in LCIO.
Definition: LCCollection.h:29