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