1 /*******************************************************************************
2 * Project: BaBar detector at the SLAC PEP-II B-factory
4 * File: $Id: EvtStreamAdapter.hh,v 1.2 2009-03-16 16:40:16 robbep Exp $
5 * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002
7 * Copyright (C) 2002 Caltech
8 *******************************************************************************/
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
15 #ifndef EVT_STREAM_ADAPTER_HH
16 #define EVT_STREAM_ADAPTER_HH
18 template <class Point> class EvtStreamAdapter {
23 virtual ~EvtStreamAdapter()
25 virtual EvtStreamAdapter* clone() const = 0;
26 virtual Point currentValue() = 0;
27 virtual void advance() = 0;
28 virtual bool pastEnd() = 0;
32 // N points are read from a generated stream.
34 template <class Point,class Generator>
35 class EvtGenStreamAdapter : public EvtStreamAdapter<Point> {
37 EvtGenStreamAdapter(Generator gen, int count)
38 : _gen(gen), _count(count)
41 virtual ~EvtGenStreamAdapter()
44 virtual EvtStreamAdapter<Point>* clone() const
46 return new EvtGenStreamAdapter(*this);
48 virtual Point currentValue() { return _gen(); }
49 virtual bool pastEnd() { return (_count <= 0); }
50 virtual void advance() { _count--; }
54 int _count; // also serves as past the end indicator
58 // Only points satisfying a predicate are read from the stream.
60 template <class Point, class Iterator, class Predicate>
61 class EvtPredStreamAdapter : public EvtStreamAdapter<Point> {
63 EvtPredStreamAdapter(Predicate pred, Iterator it, Iterator end)
64 : _pred(pred), _it(it), _end(end)
66 virtual ~EvtPredStreamAdapter()
69 virtual EvtStreamAdapter<Point>* clone() const
71 return new EvtPredStreamAdapter(*this);
73 virtual Point currentValue() {
78 if(_pred(value)) break;
84 virtual bool pastEnd() { return _it == _end; }
85 virtual void advance() { _it++; }