]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TEvtGen/EvtGenBase/EvtStreamInputIterator.hh
Coverity fix
[u/mrichter/AliRoot.git] / TEvtGen / EvtGenBase / EvtStreamInputIterator.hh
1 /*******************************************************************************
2  * Project: BaBar detector at the SLAC PEP-II B-factory
3  * Package: EvtGenBase
4  *    File: $Id: EvtStreamInputIterator.hh,v 1.1 2003/03/03 05:38:22 dvoretsk Exp $
5  *  Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002
6  *
7  * Copyright (C) 2002 Caltech
8  *******************************************************************************/
9
10 // Adapters are used to convert various types of input streams 
11 // into an iteratable interface.
12
13 #ifndef EVT_STREAM_INPUT_ITERATOR_HH
14 #define EVT_STREAM_INPUT_ITERATOR_HH
15
16 #include "EvtGenBase/EvtStreamAdapter.hh"
17 #include <iterator>
18 using std::input_iterator_tag;
19
20 template <class Point>
21 class EvtStreamInputIterator {  
22 public:
23
24   typedef input_iterator_tag iterator_category;
25   typedef Point              value_type;
26   typedef const Point*       pointer;
27   typedef const Point&       reference;
28
29   EvtStreamInputIterator() 
30     : _counter(0)
31   {}
32   
33   EvtStreamInputIterator(const EvtStreamInputIterator& other) 
34     : _counter(other._counter ? other._counter->clone() : 0),
35       _currentValue(other._currentValue)
36   {} 
37
38   EvtStreamInputIterator(EvtStreamAdapter<Point>& counter)
39     : _counter(counter.clone())
40   {
41     _currentValue = _counter->currentValue(); 
42   }
43   
44   ~EvtStreamInputIterator()
45   {
46     if(_counter) delete _counter;
47   }
48   
49   reference operator*() const 
50   {
51     return _currentValue; 
52   }
53   
54   EvtStreamInputIterator& operator++() 
55   {
56     _read();
57     return *this;
58   }
59   
60   EvtStreamInputIterator operator++(int)
61   {
62     EvtStreamInputIterator tmp = *this;
63     _read();
64     return tmp;
65   }
66   
67   bool operator==(const EvtStreamInputIterator& other) const 
68   {
69     // Equality is only defined for two past the end iterators
70     return (pastEnd() && other.pastEnd());
71   }
72   
73 protected:
74
75   EvtStreamAdapter<Point>* _counter;
76   value_type _currentValue;
77   
78   bool pastEnd() const 
79   {
80     bool ret = true;
81     if(_counter) ret = _counter->pastEnd();
82     return ret;
83   }
84
85   // Advances the iterator
86
87   void _read() {
88     
89     _counter->advance();
90     _currentValue = _counter->currentValue();
91   }
92 };
93
94
95 // For adaptable generators these shorthand functions can be used 
96 // to construct iterators.
97
98 template <class Generator>
99 EvtStreamInputIterator<typename Generator::result_type> iter(Generator gen, int N = 0)
100 {
101   typedef typename Generator::result_type Point;
102   EvtGenStreamAdapter<Point,Generator> counter(gen,N);
103   return EvtStreamInputIterator<Point>(counter);
104 }
105
106
107 #endif
108
109
110