File indexing completed on 2025-09-15 08:38:52
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP_INCLUDED_
0010 #define BOOST_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP_INCLUDED_
0011
0012 #include <memory>
0013 #include <type_traits>
0014
0015 #include <boost/iterator/iterator_facade.hpp>
0016 #include <boost/iterator/iterator_categories.hpp>
0017 #include <boost/iterator/detail/type_traits/conjunction.hpp>
0018 #include <boost/optional/optional.hpp>
0019
0020 namespace boost {
0021 namespace iterators {
0022
0023 template< typename Function, typename Input >
0024 class function_input_iterator;
0025
0026 namespace detail {
0027
0028 template< typename Function, typename Input >
0029 using function_input_iterator_facade_base_t = iterator_facade<
0030 iterators::function_input_iterator< Function, Input >,
0031 decltype(std::declval< Function& >()()),
0032 single_pass_traversal_tag,
0033 decltype(std::declval< Function& >()()) const&
0034 >;
0035
0036 template< typename Function, typename Input >
0037 class function_object_input_iterator :
0038 public function_input_iterator_facade_base_t< Function, Input >
0039 {
0040 private:
0041 using base_type = function_input_iterator_facade_base_t< Function, Input >;
0042
0043 protected:
0044 using function_arg_type = Function&;
0045
0046 public:
0047 using value_type = typename base_type::value_type;
0048
0049 public:
0050 function_object_input_iterator(function_arg_type f, Input state) :
0051 m_f(std::addressof(f)), m_state(state)
0052 {}
0053
0054 protected:
0055 typename std::add_pointer< Function >::type m_f;
0056 Input m_state;
0057 mutable optional< value_type > m_value;
0058 };
0059
0060 template< typename Function, typename Input >
0061 class function_pointer_input_iterator :
0062 public function_input_iterator_facade_base_t< Function, Input >
0063 {
0064 private:
0065 using base_type = function_input_iterator_facade_base_t< Function, Input >;
0066
0067 protected:
0068 using function_arg_type = Function;
0069
0070 public:
0071 using value_type = typename base_type::value_type;
0072
0073 public:
0074 function_pointer_input_iterator(function_arg_type f, Input state) :
0075 m_f(f), m_state(state)
0076 {}
0077
0078 protected:
0079 Function m_f;
0080 Input m_state;
0081 mutable optional< value_type > m_value;
0082 };
0083
0084 template< typename Function, typename Input >
0085 using function_input_iterator_base_t = typename std::conditional<
0086 detail::conjunction<
0087 std::is_pointer< Function >,
0088 std::is_function< typename std::remove_pointer< Function >::type >
0089 >::value,
0090 detail::function_pointer_input_iterator< Function, Input >,
0091 detail::function_object_input_iterator< Function, Input >
0092 >::type;
0093
0094 }
0095
0096 template< typename Function, typename Input >
0097 class function_input_iterator :
0098 public detail::function_input_iterator_base_t< Function, Input >
0099 {
0100 private:
0101 using base_type = detail::function_input_iterator_base_t< Function, Input >;
0102 using function_arg_type = typename base_type::function_arg_type;
0103
0104 public:
0105 using reference = typename base_type::reference;
0106
0107 public:
0108 function_input_iterator(function_arg_type f, Input i) :
0109 base_type(f, i)
0110 {}
0111
0112 void increment()
0113 {
0114 if (this->m_value)
0115 this->m_value.reset();
0116 else
0117 (*this->m_f)();
0118 ++this->m_state;
0119 }
0120
0121 reference dereference() const
0122 {
0123 if (!this->m_value)
0124 this->m_value = (*this->m_f)();
0125 return this->m_value.get();
0126 }
0127
0128 bool equal(function_input_iterator const& other) const
0129 {
0130 return this->m_f == other.m_f && this->m_state == other.m_state;
0131 }
0132 };
0133
0134 template< typename Function, typename Input >
0135 inline function_input_iterator< Function, Input > make_function_input_iterator(Function& f, Input state)
0136 {
0137 return function_input_iterator< Function, Input >(f, state);
0138 }
0139
0140 template< typename Function, typename Input >
0141 inline function_input_iterator< Function*, Input > make_function_input_iterator(Function* f, Input state)
0142 {
0143 return function_input_iterator< Function*, Input >(f, state);
0144 }
0145
0146 struct infinite
0147 {
0148 infinite& operator++() { return *this; }
0149 infinite& operator++(int) { return *this; }
0150 bool operator==(infinite&) const { return false; };
0151 bool operator==(infinite const&) const { return false; };
0152 };
0153
0154 }
0155
0156 using iterators::function_input_iterator;
0157 using iterators::make_function_input_iterator;
0158 using iterators::infinite;
0159
0160 }
0161
0162 #endif