Changeset View
Changeset View
Standalone View
Standalone View
EvtGenBase/EvtStreamInputIterator.hh
Show All 34 Lines | |||||
class EvtStreamInputIterator { | class EvtStreamInputIterator { | ||||
public: | public: | ||||
typedef input_iterator_tag iterator_category; | typedef input_iterator_tag iterator_category; | ||||
typedef Point value_type; | typedef Point value_type; | ||||
typedef ptrdiff_t difference_type; | typedef ptrdiff_t difference_type; | ||||
typedef const Point* pointer; | typedef const Point* pointer; | ||||
typedef const Point& reference; | typedef const Point& reference; | ||||
EvtStreamInputIterator() : _counter( 0 ) {} | EvtStreamInputIterator() : m_counter( 0 ) {} | ||||
EvtStreamInputIterator( const EvtStreamInputIterator& other ) : | EvtStreamInputIterator( const EvtStreamInputIterator& other ) : | ||||
_counter( other._counter ? other._counter->clone() : 0 ), | m_counter( other.m_counter ? other.m_counter->clone() : 0 ), | ||||
_currentValue( other._currentValue ) | m_currentValue( other.m_currentValue ) | ||||
{ | { | ||||
} | } | ||||
EvtStreamInputIterator( EvtStreamAdapter<Point>& counter ) : | EvtStreamInputIterator( EvtStreamAdapter<Point>& counter ) : | ||||
_counter( counter.clone() ) | m_counter( counter.clone() ) | ||||
{ | { | ||||
_currentValue = _counter->currentValue(); | m_currentValue = m_counter->currentValue(); | ||||
} | } | ||||
~EvtStreamInputIterator() | ~EvtStreamInputIterator() | ||||
{ | { | ||||
if ( _counter ) | if ( m_counter ) | ||||
delete _counter; | delete m_counter; | ||||
} | } | ||||
reference operator*() const { return _currentValue; } | reference operator*() const { return m_currentValue; } | ||||
EvtStreamInputIterator& operator++() | EvtStreamInputIterator& operator++() | ||||
{ | { | ||||
_read(); | m_read(); | ||||
return *this; | return *this; | ||||
} | } | ||||
EvtStreamInputIterator operator++( int ) | EvtStreamInputIterator operator++( int ) | ||||
{ | { | ||||
EvtStreamInputIterator tmp = *this; | EvtStreamInputIterator tmp = *this; | ||||
_read(); | m_read(); | ||||
return tmp; | return tmp; | ||||
} | } | ||||
bool operator==( const EvtStreamInputIterator& other ) const | bool operator==( const EvtStreamInputIterator& other ) const | ||||
{ | { | ||||
// Equality is only defined for two past the end iterators | // Equality is only defined for two past the end iterators | ||||
return ( pastEnd() && other.pastEnd() ); | return ( pastEnd() && other.pastEnd() ); | ||||
} | } | ||||
protected: | protected: | ||||
EvtStreamAdapter<Point>* _counter; | EvtStreamAdapter<Point>* m_counter; | ||||
value_type _currentValue; | value_type m_currentValue; | ||||
bool pastEnd() const | bool pastEnd() const | ||||
{ | { | ||||
bool ret = true; | bool ret = true; | ||||
if ( _counter ) | if ( m_counter ) | ||||
ret = _counter->pastEnd(); | ret = m_counter->pastEnd(); | ||||
return ret; | return ret; | ||||
} | } | ||||
// Advances the iterator | // Advances the iterator | ||||
void _read() | void m_read() | ||||
{ | { | ||||
_counter->advance(); | m_counter->advance(); | ||||
_currentValue = _counter->currentValue(); | m_currentValue = m_counter->currentValue(); | ||||
} | } | ||||
}; | }; | ||||
// For adaptable generators these shorthand functions can be used | // For adaptable generators these shorthand functions can be used | ||||
// to construct iterators. | // to construct iterators. | ||||
template <class Generator> | template <class Generator> | ||||
EvtStreamInputIterator<typename Generator::result_type> iter( Generator gen, | EvtStreamInputIterator<typename Generator::result_type> iter( Generator gen, | ||||
int N = 0 ) | int N = 0 ) | ||||
{ | { | ||||
typedef typename Generator::result_type Point; | typedef typename Generator::result_type Point; | ||||
EvtGenStreamAdapter<Point, Generator> counter( gen, N ); | EvtGenStreamAdapter<Point, Generator> counter( gen, N ); | ||||
return EvtStreamInputIterator<Point>( counter ); | return EvtStreamInputIterator<Point>( counter ); | ||||
} | } | ||||
#endif | #endif |