eof_iterator.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright Vladimir Prus 2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_EOF_ITERATOR_VP_2004_03_12
  6. #define BOOST_EOF_ITERATOR_VP_2004_03_12
  7. #include <boost/iterator/iterator_facade.hpp>
  8. namespace boost {
  9. /** The 'eof_iterator' class is useful for constructing forward iterators
  10. in cases where iterator extract data from some source and it's easy
  11. to detect 'eof' \-- i.e. the situation where there's no data. One
  12. apparent example is reading lines from a file.
  13. Implementing such iterators using 'iterator_facade' directly would
  14. require to create class with three core operation, a couple of
  15. constructors. When using 'eof_iterator', the derived class should define
  16. only one method to get new value, plus a couple of constructors.
  17. The basic idea is that iterator has 'eof' bit. Two iterators are equal
  18. only if both have their 'eof' bits set. The 'get' method either obtains
  19. the new value or sets the 'eof' bit.
  20. Specifically, derived class should define:
  21. 1. A default constructor, which creates iterator with 'eof' bit set. The
  22. constructor body should call 'found_eof' method defined here.
  23. 2. Some other constructor. It should initialize some 'data pointer' used
  24. in iterator operation and then call 'get'.
  25. 3. The 'get' method. It should operate this way:
  26. - look at some 'data pointer' to see if new element is available;
  27. if not, it should call 'found_eof'.
  28. - extract new element and store it at location returned by the 'value'
  29. method.
  30. - advance the data pointer.
  31. Essentially, the 'get' method has the functionality of both 'increment'
  32. and 'dereference'. It's very good for the cases where data extraction
  33. implicitly moves data pointer, like for stream operation.
  34. */
  35. template<class Derived, class ValueType>
  36. class eof_iterator : public iterator_facade<Derived, const ValueType,
  37. forward_traversal_tag>
  38. {
  39. public:
  40. eof_iterator()
  41. : m_at_eof(false)
  42. {}
  43. protected: // interface for derived
  44. /** Returns the reference which should be used by derived
  45. class to store the next value. */
  46. ValueType& value()
  47. {
  48. return m_value;
  49. }
  50. /** Should be called by derived class to indicate that it can't
  51. produce next element. */
  52. void found_eof()
  53. {
  54. m_at_eof = true;
  55. }
  56. private: // iterator core operations
  57. #ifdef __DCC__
  58. friend class boost::iterator_core_access;
  59. #else
  60. friend class iterator_core_access;
  61. #endif
  62. void increment()
  63. {
  64. static_cast<Derived&>(*this).get();
  65. }
  66. bool equal(const eof_iterator& other) const
  67. {
  68. if (m_at_eof && other.m_at_eof)
  69. return true;
  70. else
  71. return false;
  72. }
  73. const ValueType& dereference() const
  74. {
  75. return m_value;
  76. }
  77. bool m_at_eof;
  78. ValueType m_value;
  79. };
  80. }
  81. #endif