Changeset View
Changeset View
Standalone View
Standalone View
EvtGenBase/EvtStreamAdapter.hh
Show All 37 Lines | |||||
}; | }; | ||||
// N points are read from a generated stream. | // N points are read from a generated stream. | ||||
template <class Point, class Generator> | template <class Point, class Generator> | ||||
class EvtGenStreamAdapter : public EvtStreamAdapter<Point> { | class EvtGenStreamAdapter : public EvtStreamAdapter<Point> { | ||||
public: | public: | ||||
EvtGenStreamAdapter( Generator gen, int count ) : | EvtGenStreamAdapter( Generator gen, int count ) : | ||||
_gen( gen ), _count( count ) | m_gen( gen ), m_count( count ) | ||||
{ | { | ||||
} | } | ||||
virtual ~EvtGenStreamAdapter() {} | virtual ~EvtGenStreamAdapter() {} | ||||
EvtStreamAdapter<Point>* clone() const override | EvtStreamAdapter<Point>* clone() const override | ||||
{ | { | ||||
return new EvtGenStreamAdapter( *this ); | return new EvtGenStreamAdapter( *this ); | ||||
} | } | ||||
Point currentValue() override { return _gen(); } | Point currentValue() override { return m_gen(); } | ||||
bool pastEnd() override { return ( _count <= 0 ); } | bool pastEnd() override { return ( m_count <= 0 ); } | ||||
void advance() override { _count--; } | void advance() override { m_count--; } | ||||
private: | private: | ||||
Generator _gen; | Generator m_gen; | ||||
int _count; // also serves as past the end indicator | int m_count; // also serves as past the end indicator | ||||
}; | }; | ||||
// Only points satisfying a predicate are read from the stream. | // Only points satisfying a predicate are read from the stream. | ||||
template <class Point, class Iterator, class Predicate> | template <class Point, class Iterator, class Predicate> | ||||
class EvtPredStreamAdapter : public EvtStreamAdapter<Point> { | class EvtPredStreamAdapter : public EvtStreamAdapter<Point> { | ||||
public: | public: | ||||
EvtPredStreamAdapter( Predicate pred, Iterator it, Iterator end ) : | EvtPredStreamAdapter( Predicate pred, Iterator it, Iterator end ) : | ||||
_pred( pred ), _it( it ), _end( end ) | m_pred( pred ), m_it( it ), m_end( end ) | ||||
{ | { | ||||
} | } | ||||
virtual ~EvtPredStreamAdapter() {} | virtual ~EvtPredStreamAdapter() {} | ||||
virtual EvtStreamAdapter<Point>* clone() const | virtual EvtStreamAdapter<Point>* clone() const | ||||
{ | { | ||||
return new EvtPredStreamAdapter( *this ); | return new EvtPredStreamAdapter( *this ); | ||||
} | } | ||||
virtual Point currentValue() | virtual Point currentValue() | ||||
{ | { | ||||
Point value; | Point value; | ||||
while ( !pastEnd() ) { | while ( !pastEnd() ) { | ||||
value = *_it; | value = *m_it; | ||||
if ( _pred( value ) ) | if ( m_pred( value ) ) | ||||
break; | break; | ||||
_it++; | m_it++; | ||||
} | } | ||||
return value; | return value; | ||||
} | } | ||||
virtual bool pastEnd() { return _it == _end; } | virtual bool pastEnd() { return m_it == m_end; } | ||||
virtual void advance() { _it++; } | virtual void advance() { m_it++; } | ||||
private: | private: | ||||
Predicate _pred; | Predicate m_pred; | ||||
Iterator _it; | Iterator m_it; | ||||
Iterator _end; | Iterator m_end; | ||||
}; | }; | ||||
#endif | #endif |