StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EvtStreamAdapter.hh
1 /*******************************************************************************
2  * Project: BaBar detector at the SLAC PEP-II B-factory
3  * Package: EvtGenBase
4  * File: $Id: EvtStreamAdapter.hh,v 1.1 2016/09/23 18:37:33 jwebb Exp $
5  * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 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 
18 template <class Point> class EvtStreamAdapter {
19 public:
20 
22  {}
23  virtual ~EvtStreamAdapter()
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 
34 template <class Point,class Generator>
35 class EvtGenStreamAdapter : public EvtStreamAdapter<Point> {
36 public:
37  EvtGenStreamAdapter(Generator gen, int count)
38  : _gen(gen), _count(count)
39  {}
40 
41  virtual ~EvtGenStreamAdapter()
42  {}
43 
44  virtual EvtStreamAdapter<Point>* clone() const
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 
52 private:
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 
60 template <class Point, class Iterator, class Predicate>
61 class EvtPredStreamAdapter : public EvtStreamAdapter<Point> {
62 public:
63  EvtPredStreamAdapter(Predicate pred, Iterator it, Iterator end)
64  : _pred(pred), _it(it), _end(end)
65  {}
66  virtual ~EvtPredStreamAdapter()
67  {}
68 
69  virtual EvtStreamAdapter<Point>* clone() const
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 
87 private:
88  Predicate _pred;
89  Iterator _it;
90  Iterator _end;
91 };
92 
93 #endif