EvtGen 2.2.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
Loading...
Searching...
No Matches
EvtStreamAdapter.hh
Go to the documentation of this file.
1
2/***********************************************************************
3* Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4* *
5* This file is part of EvtGen. *
6* *
7* EvtGen is free software: you can redistribute it and/or modify *
8* it under the terms of the GNU General Public License as published by *
9* the Free Software Foundation, either version 3 of the License, or *
10* (at your option) any later version. *
11* *
12* EvtGen is distributed in the hope that it will be useful, *
13* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15* GNU General Public License for more details. *
16* *
17* You should have received a copy of the GNU General Public License *
18* along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19***********************************************************************/
20
21#ifndef EVT_STREAM_ADAPTER_HH
22#define EVT_STREAM_ADAPTER_HH
23
24// Stream adapters are used to convert a stream-like input (for example,
25// a file containing N entries) to an STL like iterator interface. There
26// must be a way to get point from the stream, and also an indicator of the
27// end of the stream.
28
29template <class Point>
31 public:
33 virtual ~EvtStreamAdapter() {}
34 virtual EvtStreamAdapter* clone() const = 0;
35 virtual Point currentValue() = 0;
36 virtual void advance() = 0;
37 virtual bool pastEnd() = 0;
38};
39
40// N points are read from a generated stream.
41
42template <class Point, class Generator>
44 public:
45 EvtGenStreamAdapter( Generator gen, int count ) :
46 m_gen( gen ), m_count( count )
47 {
48 }
49
51
53 {
54 return new EvtGenStreamAdapter( *this );
55 }
56 Point currentValue() override { return m_gen(); }
57 bool pastEnd() override { return ( m_count <= 0 ); }
58 void advance() override { m_count--; }
59
60 private:
61 Generator m_gen;
62 int m_count; // also serves as past the end indicator
63};
64
65// Only points satisfying a predicate are read from the stream.
66
67template <class Point, class Iterator, class Predicate>
69 public:
70 EvtPredStreamAdapter( Predicate pred, Iterator it, Iterator end ) :
71 m_pred( pred ), m_it( it ), m_end( end )
72 {
73 }
75
77 {
78 return new EvtPredStreamAdapter( *this );
79 }
80 virtual Point currentValue()
81 {
82 Point value;
83 while ( !pastEnd() ) {
84 value = *m_it;
85 if ( m_pred( value ) )
86 break;
87 m_it++;
88 }
89 return value;
90 }
91
92 virtual bool pastEnd() { return m_it == m_end; }
93 virtual void advance() { m_it++; }
94
95 private:
96 Predicate m_pred;
97 Iterator m_it;
98 Iterator m_end;
99};
100
101#endif
bool pastEnd() override
void advance() override
EvtStreamAdapter< Point > * clone() const override
Point currentValue() override
EvtGenStreamAdapter(Generator gen, int count)
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