66// http://www.boost.org/LICENSE_1_0.txt)
77//
88
9- #ifndef BOOST_FUNCTION_INPUT_ITERATOR
10- #define BOOST_FUNCTION_INPUT_ITERATOR
9+ #ifndef BOOST_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP_INCLUDED_
10+ #define BOOST_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP_INCLUDED_
1111
12- #include < type_traits>
1312#include < memory>
13+ #include < type_traits>
1414
15- #include < boost/config.hpp>
16- #include < boost/function_types/is_function_pointer.hpp>
17- #include < boost/function_types/result_type.hpp>
1815#include < boost/iterator/iterator_facade.hpp>
19- #include < boost/none.hpp>
16+ #include < boost/iterator/iterator_categories.hpp>
17+ #include < boost/iterator/detail/type_traits/conjunction.hpp>
2018#include < boost/optional/optional.hpp>
21- #include < boost/utility/result_of.hpp>
2219
2320namespace boost {
24-
2521namespace iterators {
2622
27- template <class Function , class Input >
28- class function_input_iterator ;
29-
30- namespace impl {
31-
32- // Computes the return type of an lvalue-call with an empty argument,
33- // i.e. decltype(declval<F&>()()). F should be a nullary lvalue-callable
34- // or function.
35- template <class F >
36- struct result_of_nullary_lvalue_call
37- {
38- typedef typename result_of<
39- #ifdef BOOST_RESULT_OF_USE_TR1
40- typename std::conditional<std::is_function<F>::value, F&, F>::type()
41- #else
42- F&()
43- #endif
44- >::type type;
45- };
46-
47- template <class Function , class Input >
48- class function_object_input_iterator :
49- public iterator_facade<
50- iterators::function_input_iterator<Function, Input>,
51- typename result_of_nullary_lvalue_call<Function>::type,
52- single_pass_traversal_tag,
53- typename result_of_nullary_lvalue_call<Function>::type const &
54- >
55- {
56- public:
57- function_object_input_iterator () {}
58- function_object_input_iterator (Function& f_, Input state_ = Input())
59- : f(std::addressof(f_)), state(state_) {}
60-
61- void increment () {
62- if (value)
63- value = none;
64- else
65- (*f)();
66- ++state;
67- }
68-
69- typename result_of_nullary_lvalue_call<Function>::type const &
70- dereference () const {
71- if (!value)
72- value = (*f)();
73- return value.get ();
74- }
75-
76- bool equal (function_object_input_iterator const & other) const {
77- return f == other.f && state == other.state ;
78- }
79-
80- private:
81- Function * f;
82- Input state;
83- mutable optional<typename result_of_nullary_lvalue_call<Function>::type> value;
84- };
85-
86- template <class Function , class Input >
87- class function_pointer_input_iterator :
88- public iterator_facade<
89- iterators::function_input_iterator<Function, Input>,
90- typename function_types::result_type<Function>::type,
91- single_pass_traversal_tag,
92- typename function_types::result_type<Function>::type const &
93- >
94- {
95- public:
96- function_pointer_input_iterator () {}
97- function_pointer_input_iterator (Function& f_, Input state_ = Input())
98- : f(f_), state(state_) {}
99-
100- void increment () {
101- if (value)
102- value = none;
103- else
104- (*f)();
105- ++state;
106- }
107-
108- typename function_types::result_type<Function>::type const &
109- dereference () const {
110- if (!value)
111- value = (*f)();
112- return value.get ();
113- }
114-
115- bool equal (function_pointer_input_iterator const & other) const {
116- return f == other.f && state == other.state ;
117- }
118-
119- private:
120- Function f;
121- Input state;
122- mutable optional<typename function_types::result_type<Function>::type> value;
123- };
124-
125- } // namespace impl
126-
127- template <class Function , class Input >
128- class function_input_iterator :
129- public std::conditional<
130- function_types::is_function_pointer<Function>::value,
131- impl::function_pointer_input_iterator<Function,Input>,
132- impl::function_object_input_iterator<Function,Input>
133- >::type
23+ template < typename Function, typename Input >
24+ class function_input_iterator ;
25+
26+ namespace detail {
27+
28+ // Computes the return type of an lvalue-call with an empty argument.
29+ // F should be a nullary lvalue-callable or function.
30+ template < typename F >
31+ using result_of_nullary_lvalue_call_t = decltype (std::declval< F& >()());
32+
33+ template < typename Function, typename Input >
34+ using function_input_iterator_facade_base_t = iterator_facade<
35+ iterators::function_input_iterator< Function, Input >,
36+ result_of_nullary_lvalue_call_t < Function >,
37+ single_pass_traversal_tag,
38+ result_of_nullary_lvalue_call_t < Function > const &
39+ >;
40+
41+ template < typename Function, typename Input >
42+ class function_object_input_iterator :
43+ public function_input_iterator_facade_base_t < Function, Input >
44+ {
45+ private:
46+ using base_type = function_input_iterator_facade_base_t < Function, Input >;
47+
48+ protected:
49+ using function_arg_type = Function&;
50+
51+ public:
52+ using value_type = typename base_type::value_type;
53+
54+ public:
55+ function_object_input_iterator (function_arg_type f, Input state) :
56+ m_f (std::addressof(f)), m_state(state)
57+ {}
58+
59+ protected:
60+ typename std::add_pointer< Function >::type m_f;
61+ Input m_state;
62+ mutable optional< value_type > m_value;
63+ };
64+
65+ template < typename Function, typename Input >
66+ class function_pointer_input_iterator :
67+ public function_input_iterator_facade_base_t < Function, Input >
68+ {
69+ private:
70+ using base_type = function_input_iterator_facade_base_t < Function, Input >;
71+
72+ protected:
73+ using function_arg_type = Function;
74+
75+ public:
76+ using value_type = typename base_type::value_type;
77+
78+ public:
79+ function_pointer_input_iterator (function_arg_type f, Input state) :
80+ m_f (f), m_state(state)
81+ {}
82+
83+ protected:
84+ Function m_f;
85+ Input m_state;
86+ mutable optional< value_type > m_value;
87+ };
88+
89+ template < typename Function, typename Input >
90+ using function_input_iterator_base_t = typename std::conditional<
91+ detail::conjunction<
92+ std::is_pointer< Function >,
93+ std::is_function< typename std::remove_pointer< Function >::type >
94+ >::value,
95+ detail::function_pointer_input_iterator< Function, Input >,
96+ detail::function_object_input_iterator< Function, Input >
97+ >::type;
98+
99+ } // namespace detail
100+
101+ template < typename Function, typename Input >
102+ class function_input_iterator :
103+ public detail::function_input_iterator_base_t < Function, Input >
104+ {
105+ private:
106+ using base_type = detail::function_input_iterator_base_t < Function, Input >;
107+ using function_arg_type = typename base_type::function_arg_type;
108+
109+ public:
110+ using reference = typename base_type::reference;
111+
112+ public:
113+ function_input_iterator (function_arg_type f, Input i) :
114+ base_type (f, i)
115+ {}
116+
117+ void increment ()
134118 {
135- typedef typename std::conditional<
136- function_types::is_function_pointer<Function>::value,
137- impl::function_pointer_input_iterator<Function,Input>,
138- impl::function_object_input_iterator<Function,Input>
139- >::type base_type;
140- public:
141- function_input_iterator (Function& f, Input i)
142- : base_type(f, i) {}
143- };
144-
145- template <class Function , class Input >
146- inline function_input_iterator<Function, Input>
147- make_function_input_iterator (Function& f, Input state) {
148- typedef function_input_iterator<Function, Input> result_t ;
149- return result_t (f, state);
119+ if (this ->m_value )
120+ this ->m_value .reset ();
121+ else
122+ (*this ->m_f )();
123+ ++this ->m_state ;
150124 }
151125
152- template < class Function , class Input >
153- inline function_input_iterator<Function*, Input>
154- make_function_input_iterator (Function* f, Input state) {
155- typedef function_input_iterator<Function*, Input> result_t ;
156- return result_t (f, state );
126+ reference dereference () const
127+ {
128+ if (! this -> m_value )
129+ this -> m_value = (* this -> m_f )() ;
130+ return this -> m_value . get ( );
157131 }
158132
159- struct infinite
133+ bool equal (function_input_iterator const & other) const
160134 {
161- infinite& operator ++() { return *this ; }
162- infinite& operator ++(int ) { return *this ; }
163- bool operator ==(infinite&) const { return false ; };
164- bool operator ==(infinite const &) const { return false ; };
165- };
135+ return this ->m_f == other.m_f && this ->m_state == other.m_state ;
136+ }
137+ };
138+
139+ template < typename Function, typename Input >
140+ inline function_input_iterator< Function, Input > make_function_input_iterator (Function& f, Input state)
141+ {
142+ return function_input_iterator< Function, Input >(f, state);
143+ }
144+
145+ template < typename Function, typename Input >
146+ inline function_input_iterator< Function*, Input > make_function_input_iterator (Function* f, Input state)
147+ {
148+ return function_input_iterator< Function*, Input >(f, state);
149+ }
150+
151+ struct infinite
152+ {
153+ infinite& operator ++() { return *this ; }
154+ infinite& operator ++(int ) { return *this ; }
155+ bool operator ==(infinite&) const { return false ; };
156+ bool operator ==(infinite const &) const { return false ; };
157+ };
166158
167159} // namespace iterators
168160
@@ -172,5 +164,4 @@ using iterators::infinite;
172164
173165} // namespace boost
174166
175- #endif
176-
167+ #endif // BOOST_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP_INCLUDED_
0 commit comments