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