...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::eof_iterator
// In header: <boost/program_options/eof_iterator.hpp> template<typename Derived, typename ValueType> class eof_iterator : public iterator_facade< Derived, const ValueType, forward_traversal_tag > { public: // construct/copy/destruct eof_iterator(); // protected member functions ValueType & value(); void found_eof(); // private member functions void increment(); bool equal(const eof_iterator &) const; const ValueType & dereference() const; };
The 'eof_iterator' class is useful for constructing forward iterators in cases where iterator extract data from some source and it's easy to detect 'eof' -- i.e. the situation where there's no data. One apparent example is reading lines from a file.
Implementing such iterators using 'iterator_facade' directly would require to create class with three core operation, a couple of constructors. When using 'eof_iterator', the derived class should define only one method to get new value, plus a couple of constructors.
The basic idea is that iterator has 'eof' bit. Two iterators are equal only if both have their 'eof' bits set. The 'get' method either obtains the new value or sets the 'eof' bit.
Specifically, derived class should define:
A default constructor, which creates iterator with 'eof' bit set. The constructor body should call 'found_eof' method defined here.
Some other constructor. It should initialize some 'data pointer' used in iterator operation and then call 'get'.
The 'get' method. It should operate this way:
look at some 'data pointer' to see if new element is available; if not, it should call 'found_eof'.
extract new element and store it at location returned by the 'value' method.
advance the data pointer.
Essentially, the 'get' method has the functionality of both 'increment' and 'dereference'. It's very good for the cases where data extraction implicitly moves data pointer, like for stream operation.
eof_iterator
private member functionsvoid increment();
bool equal(const eof_iterator & other) const;
const ValueType & dereference() const;