Ticket #2428: generator_iterator.hpp

File generator_iterator.hpp, 1.9 KB (added by Johannes Brunen <JBrunen@…>, 14 years ago)
Line 
1// (C) Copyright Jens Maurer 2001.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5//
6// Revision History:
7
8// 15 Nov 2001 Jens Maurer
9// created.
10
11// See http://www.boost.org/libs/utility/iterator_adaptors.htm for documentation.
12
13#ifndef BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP
14#define BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP
15
16#include <boost/iterator/iterator_facade.hpp>
17#include <boost/ref.hpp>
18
19namespace boost {
20
21template<class Generator>
22class generator_iterator
23 : public iterator_facade<
24 generator_iterator<Generator>
25 , typename Generator::result_type
26 , single_pass_traversal_tag
27 , typename Generator::result_type const&
28 >
29{
30 typedef iterator_facade<
31 generator_iterator<Generator>
32 , typename Generator::result_type
33 , single_pass_traversal_tag
34 , typename Generator::result_type const&
35 > super_t;
36
37 public:
38 generator_iterator() : m_g(0), m_value(0) {}
39 generator_iterator(Generator* g) : m_g(g), m_value((*m_g)()) {}
40
41 void increment()
42 {
43 m_value = (*m_g)();
44 }
45
46 const typename Generator::result_type&
47 dereference() const
48 {
49 return m_value;
50 }
51
52 bool equal(generator_iterator const& y) const
53 {
54 return this->m_g == y.m_g && this->m_value == y.m_value;
55 }
56
57 private:
58 Generator* m_g;
59 typename Generator::result_type m_value;
60};
61
62template<class Generator>
63struct generator_iterator_generator
64{
65 typedef generator_iterator<Generator> type;
66};
67
68template <class Generator>
69inline generator_iterator<Generator>
70make_generator_iterator(Generator & gen)
71{
72 typedef generator_iterator<Generator> result_t;
73 return result_t(&gen);
74}
75
76} // namespace boost
77
78
79#endif // BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP
80