Converting TEvtGen to native cmake
[u/mrichter/AliRoot.git] / TEvtGen / EvtGen / EvtGenBase / EvtStreamInputIterator.hh
CommitLineData
da0e9ce3 1/*******************************************************************************
2 * Project: BaBar detector at the SLAC PEP-II B-factory
3 * Package: EvtGenBase
0ca57c2f 4 * File: $Id: EvtStreamInputIterator.hh,v 1.2 2009-03-16 16:41:09 robbep Exp $
da0e9ce3 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"
0ca57c2f 17
da0e9ce3 18#include <iterator>
0ca57c2f 19#include <cstddef>
20
da0e9ce3 21using std::input_iterator_tag;
22
23template <class Point>
24class EvtStreamInputIterator {
25public:
26
27 typedef input_iterator_tag iterator_category;
28 typedef Point value_type;
0ca57c2f 29 typedef ptrdiff_t difference_type;
da0e9ce3 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
77protected:
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
102template <class Generator>
103EvtStreamInputIterator<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