evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
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 
29 template <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 
42 template <class Point, class Generator>
43 class EvtGenStreamAdapter : public EvtStreamAdapter<Point> {
44  public:
45  EvtGenStreamAdapter( Generator gen, int count ) :
46  _gen( gen ), _count( count )
47  {
48  }
49 
50  virtual ~EvtGenStreamAdapter() {}
51 
52  EvtStreamAdapter<Point>* clone() const override
53  {
54  return new EvtGenStreamAdapter( *this );
55  }
56  Point currentValue() override { return _gen(); }
57  bool pastEnd() override { return ( _count <= 0 ); }
58  void advance() override { _count--; }
59 
60  private:
61  Generator _gen;
62  int _count; // also serves as past the end indicator
63 };
64 
65 // Only points satisfying a predicate are read from the stream.
66 
67 template <class Point, class Iterator, class Predicate>
68 class EvtPredStreamAdapter : public EvtStreamAdapter<Point> {
69  public:
70  EvtPredStreamAdapter( Predicate pred, Iterator it, Iterator end ) :
71  _pred( pred ), _it( it ), _end( end )
72  {
73  }
74  virtual ~EvtPredStreamAdapter() {}
75 
76  virtual EvtStreamAdapter<Point>* clone() const
77  {
78  return new EvtPredStreamAdapter( *this );
79  }
80  virtual Point currentValue()
81  {
82  Point value;
83  while ( !pastEnd() ) {
84  value = *_it;
85  if ( _pred( value ) )
86  break;
87  _it++;
88  }
89  return value;
90  }
91 
92  virtual bool pastEnd() { return _it == _end; }
93  virtual void advance() { _it++; }
94 
95  private:
96  Predicate _pred;
97  Iterator _it;
98  Iterator _end;
99 };
100 
101 #endif
virtual EvtStreamAdapter< Point > * clone() const
virtual ~EvtGenStreamAdapter()
EvtPredStreamAdapter(Predicate pred, Iterator it, Iterator end)
EvtGenStreamAdapter(Generator gen, int count)
Point currentValue() override
virtual Point currentValue()
EvtStreamAdapter< Point > * clone() const override
virtual EvtStreamAdapter * clone() const =0
virtual void advance()
virtual bool pastEnd()
bool pastEnd() override
virtual ~EvtStreamAdapter()
virtual void advance()=0
virtual bool pastEnd()=0
virtual Point currentValue()=0
void advance() override