00001 /******************************************************************************* 00002 * Project: BaBar detector at the SLAC PEP-II B-factory 00003 * Package: EvtGenBase 00004 * File: $Id: EvtStreamAdapter.hh,v 1.1.1.2 2007/10/26 05:03:14 pingrg Exp $ 00005 * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002 00006 * 00007 * Copyright (C) 2002 Caltech 00008 *******************************************************************************/ 00009 00010 // Stream adapters are used to convert a stream-like input (for example, 00011 // a file containing N entries) to an STL like iterator interface. There 00012 // must be a way to get point from the stream, and also an indicator of the 00013 // end of the stream. 00014 00015 #ifndef EVT_STREAM_ADAPTER_HH 00016 #define EVT_STREAM_ADAPTER_HH 00017 00018 template <class Point> class EvtStreamAdapter { 00019 public: 00020 00021 EvtStreamAdapter() 00022 {} 00023 virtual ~EvtStreamAdapter() 00024 {} 00025 virtual EvtStreamAdapter* clone() const = 0; 00026 virtual Point currentValue() = 0; 00027 virtual void advance() = 0; 00028 virtual bool pastEnd() = 0; 00029 00030 }; 00031 00032 // N points are read from a generated stream. 00033 00034 template <class Point,class Generator> 00035 class EvtGenStreamAdapter : public EvtStreamAdapter<Point> { 00036 public: 00037 EvtGenStreamAdapter(Generator gen, int count) 00038 : _gen(gen), _count(count) 00039 {} 00040 00041 virtual ~EvtGenStreamAdapter() 00042 {} 00043 00044 virtual EvtStreamAdapter<Point>* clone() const 00045 { 00046 return new EvtGenStreamAdapter(*this); 00047 } 00048 virtual Point currentValue() { return _gen(); } 00049 virtual bool pastEnd() { return (_count <= 0); } 00050 virtual void advance() { _count--; } 00051 00052 private: 00053 Generator _gen; 00054 int _count; // also serves as past the end indicator 00055 }; 00056 00057 00058 // Only points satisfying a predicate are read from the stream. 00059 00060 template <class Point, class Iterator, class Predicate> 00061 class EvtPredStreamAdapter : public EvtStreamAdapter<Point> { 00062 public: 00063 EvtPredStreamAdapter(Predicate pred, Iterator it, Iterator end) 00064 : _pred(pred), _it(it), _end(end) 00065 {} 00066 virtual ~EvtPredStreamAdapter() 00067 {} 00068 00069 virtual EvtStreamAdapter<Point>* clone() const 00070 { 00071 return new EvtPredStreamAdapter(*this); 00072 } 00073 virtual Point currentValue() { 00074 Point value; 00075 while(!pastEnd()) { 00076 00077 value = *_it; 00078 if(_pred(value)) break; 00079 _it++; 00080 } 00081 return value; 00082 } 00083 00084 virtual bool pastEnd() { return _it == _end; } 00085 virtual void advance() { _it++; } 00086 00087 private: 00088 Predicate _pred; 00089 Iterator _it; 00090 Iterator _end; 00091 }; 00092 00093 #endif