]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TEvtGen/EvtGenBase/EvtStreamInputIterator.hh
o updates to fix the 11a pass4 problem of T0 (Alla)
[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 ptrdiff_t          difference_type;
27   typedef const Point*       pointer;
28   typedef const Point&       reference;
29
30   EvtStreamInputIterator() 
31     : _counter(0)
32   {}
33   
34   EvtStreamInputIterator(const EvtStreamInputIterator& other) 
35     : _counter(other._counter ? other._counter->clone() : 0),
36       _currentValue(other._currentValue)
37   {} 
38
39   EvtStreamInputIterator(EvtStreamAdapter<Point>& counter)
40     : _counter(counter.clone())
41   {
42     _currentValue = _counter->currentValue(); 
43   }
44   
45   ~EvtStreamInputIterator()
46   {
47     if(_counter) delete _counter;
48   }
49   
50   reference operator*() const 
51   {
52     return _currentValue; 
53   }
54   
55   EvtStreamInputIterator& operator++() 
56   {
57     _read();
58     return *this;
59   }
60   
61   EvtStreamInputIterator operator++(int)
62   {
63     EvtStreamInputIterator tmp = *this;
64     _read();
65     return tmp;
66   }
67   
68   bool operator==(const EvtStreamInputIterator& other) const 
69   {
70     // Equality is only defined for two past the end iterators
71     return (pastEnd() && other.pastEnd());
72   }
73   
74 protected:
75
76   EvtStreamAdapter<Point>* _counter;
77   value_type _currentValue;
78   
79   bool pastEnd() const 
80   {
81     bool ret = true;
82     if(_counter) ret = _counter->pastEnd();
83     return ret;
84   }
85
86   // Advances the iterator
87
88   void _read() {
89     
90     _counter->advance();
91     _currentValue = _counter->currentValue();
92   }
93 };
94
95
96 // For adaptable generators these shorthand functions can be used 
97 // to construct iterators.
98
99 template <class Generator>
100 EvtStreamInputIterator<typename Generator::result_type> iter(Generator gen, int N = 0)
101 {
102   typedef typename Generator::result_type Point;
103   EvtGenStreamAdapter<Point,Generator> counter(gen,N);
104   return EvtStreamInputIterator<Point>(counter);
105 }
106
107
108 #endif
109
110
111