CGEM BOSS 6.6.5.i
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtStreamAdapter.hh
Go to the documentation of this file.
1/*******************************************************************************
2 * Project: BaBar detector at the SLAC PEP-II B-factory
3 * Package: EvtGenBase
4 * File: $Id: EvtStreamAdapter.hh,v 1.1.1.2 2007/10/26 05:03:14 pingrg Exp $
5 * Author: Alexei Dvoretskii, [email protected], 2001-2002
6 *
7 * Copyright (C) 2002 Caltech
8 *******************************************************************************/
9
10// Stream adapters are used to convert a stream-like input (for example,
11// a file containing N entries) to an STL like iterator interface. There
12// must be a way to get point from the stream, and also an indicator of the
13// end of the stream.
14
15#ifndef EVT_STREAM_ADAPTER_HH
16#define EVT_STREAM_ADAPTER_HH
17
18template <class Point> class EvtStreamAdapter {
19public:
20
24 {}
25 virtual EvtStreamAdapter* clone() const = 0;
26 virtual Point currentValue() = 0;
27 virtual void advance() = 0;
28 virtual bool pastEnd() = 0;
29
30};
31
32// N points are read from a generated stream.
33
34template <class Point,class Generator>
36public:
37 EvtGenStreamAdapter(Generator gen, int count)
38 : _gen(gen), _count(count)
39 {}
40
42 {}
43
45 {
46 return new EvtGenStreamAdapter(*this);
47 }
48 virtual Point currentValue() { return _gen(); }
49 virtual bool pastEnd() { return (_count <= 0); }
50 virtual void advance() { _count--; }
51
52private:
53 Generator _gen;
54 int _count; // also serves as past the end indicator
55};
56
57
58// Only points satisfying a predicate are read from the stream.
59
60template <class Point, class Iterator, class Predicate>
62public:
63 EvtPredStreamAdapter(Predicate pred, Iterator it, Iterator end)
64 : _pred(pred), _it(it), _end(end)
65 {}
67 {}
68
70 {
71 return new EvtPredStreamAdapter(*this);
72 }
73 virtual Point currentValue() {
74 Point value;
75 while(!pastEnd()) {
76
77 value = *_it;
78 if(_pred(value)) break;
79 _it++;
80 }
81 return value;
82 }
83
84 virtual bool pastEnd() { return _it == _end; }
85 virtual void advance() { _it++; }
86
87private:
88 Predicate _pred;
89 Iterator _it;
90 Iterator _end;
91};
92
93#endif
EvtGenStreamAdapter(Generator gen, int count)
virtual EvtStreamAdapter< Point > * clone() const
virtual Point currentValue()
virtual Point currentValue()
virtual EvtStreamAdapter< Point > * clone() const
EvtPredStreamAdapter(Predicate pred, Iterator it, Iterator end)
virtual void advance()=0
virtual ~EvtStreamAdapter()
virtual EvtStreamAdapter * clone() const =0
virtual Point currentValue()=0
virtual bool pastEnd()=0