00001 /******************************************************************************* 00002 * Project: BaBar detector at the SLAC PEP-II B-factory 00003 * Package: EvtGenBase 00004 * File: $Id: EvtStreamInputIterator.hh,v 1.2 2015/04/07 07:39:37 pingrg Exp $ 00005 * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002 00006 * 00007 * Copyright (C) 2002 Caltech 00008 *******************************************************************************/ 00009 00010 // Adapters are used to convert various types of input streams 00011 // into an iteratable interface. 00012 00013 #ifndef EVT_STREAM_INPUT_ITERATOR_HH 00014 #define EVT_STREAM_INPUT_ITERATOR_HH 00015 00016 #include "EvtGenBase/EvtStreamAdapter.hh" 00017 #include <iterator> 00018 using std::input_iterator_tag; 00019 00020 template <class Point> 00021 class EvtStreamInputIterator { 00022 public: 00023 00024 typedef input_iterator_tag iterator_category; 00025 typedef Point value_type; 00026 //typedef ptrdiff_t difference_type; 00027 typedef const Point* pointer; 00028 typedef const Point& reference; 00029 00030 EvtStreamInputIterator() 00031 : _counter(0) 00032 {} 00033 00034 EvtStreamInputIterator(const EvtStreamInputIterator& other) 00035 : _counter(other._counter ? other._counter->clone() : 0), 00036 _currentValue(other._currentValue) 00037 {} 00038 00039 EvtStreamInputIterator(EvtStreamAdapter<Point>& counter) 00040 : _counter(counter.clone()) 00041 { 00042 _currentValue = _counter->currentValue(); 00043 } 00044 00045 ~EvtStreamInputIterator() 00046 { 00047 if(_counter) delete _counter; 00048 } 00049 00050 reference operator*() const 00051 { 00052 return _currentValue; 00053 } 00054 00055 EvtStreamInputIterator& operator++() 00056 { 00057 _read(); 00058 return *this; 00059 } 00060 00061 EvtStreamInputIterator operator++(int) 00062 { 00063 EvtStreamInputIterator tmp = *this; 00064 _read(); 00065 return tmp; 00066 } 00067 00068 bool operator==(const EvtStreamInputIterator& other) const 00069 { 00070 // Equality is only defined for two past the end iterators 00071 return (pastEnd() && other.pastEnd()); 00072 } 00073 00074 protected: 00075 00076 EvtStreamAdapter<Point>* _counter; 00077 value_type _currentValue; 00078 00079 bool pastEnd() const 00080 { 00081 bool ret = true; 00082 if(_counter) ret = _counter->pastEnd(); 00083 return ret; 00084 } 00085 00086 // Advances the iterator 00087 00088 void _read() { 00089 00090 _counter->advance(); 00091 _currentValue = _counter->currentValue(); 00092 } 00093 }; 00094 00095 00096 // For adaptable generators these shorthand functions can be used 00097 // to construct iterators. 00098 00099 template <class Generator> 00100 EvtStreamInputIterator<typename Generator::result_type> iter(Generator gen, int N = 0) 00101 { 00102 typedef typename Generator::result_type Point; 00103 EvtGenStreamAdapter<Point,Generator> counter(gen,N); 00104 return EvtStreamInputIterator<Point>(counter); 00105 } 00106 00107 00108 #endif 00109 00110 00111