function_input_iterator.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2009 (C) Dean Michael Berris <me@deanberris.com>
  2. // Copyright 2012 (C) Google, Inc.
  3. // Copyright 2012 (C) Jeffrey Lee Hellrung, Jr.
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_FUNCTION_INPUT_ITERATOR
  9. #define BOOST_FUNCTION_INPUT_ITERATOR
  10. #include <boost/config.hpp>
  11. #include <boost/assert.hpp>
  12. #include <boost/core/addressof.hpp>
  13. #include <boost/type_traits/conditional.hpp>
  14. #include <boost/function_types/is_function_pointer.hpp>
  15. #include <boost/function_types/result_type.hpp>
  16. #include <boost/iterator/iterator_facade.hpp>
  17. #include <boost/none.hpp>
  18. #include <boost/optional/optional.hpp>
  19. #include <boost/utility/result_of.hpp>
  20. #ifdef BOOST_RESULT_OF_USE_TR1
  21. #include <boost/type_traits/is_function.hpp>
  22. #endif
  23. namespace boost {
  24. namespace iterators {
  25. template <class Function, class Input>
  26. class function_input_iterator;
  27. namespace impl {
  28. // Computes the return type of an lvalue-call with an empty argument,
  29. // i.e. decltype(declval<F&>()()). F should be a nullary lvalue-callable
  30. // or function.
  31. template <class F>
  32. struct result_of_nullary_lvalue_call
  33. {
  34. typedef typename result_of<
  35. #ifdef BOOST_RESULT_OF_USE_TR1
  36. typename boost::conditional<is_function<F>::value, F&, F>::type()
  37. #else
  38. F&()
  39. #endif
  40. >::type type;
  41. };
  42. template <class Function, class Input>
  43. class function_object_input_iterator :
  44. public iterator_facade<
  45. iterators::function_input_iterator<Function, Input>,
  46. typename result_of_nullary_lvalue_call<Function>::type,
  47. single_pass_traversal_tag,
  48. typename result_of_nullary_lvalue_call<Function>::type const &
  49. >
  50. {
  51. public:
  52. function_object_input_iterator() {}
  53. function_object_input_iterator(Function & f_, Input state_ = Input())
  54. : f(boost::addressof(f_)), state(state_) {}
  55. void increment() {
  56. if (value)
  57. value = none;
  58. else
  59. (*f)();
  60. ++state;
  61. }
  62. typename result_of_nullary_lvalue_call<Function>::type const &
  63. dereference() const {
  64. if (!value)
  65. value = (*f)();
  66. return value.get();
  67. }
  68. bool equal(function_object_input_iterator const & other) const {
  69. return f == other.f && state == other.state;
  70. }
  71. private:
  72. Function * f;
  73. Input state;
  74. mutable optional<typename result_of_nullary_lvalue_call<Function>::type> value;
  75. };
  76. template <class Function, class Input>
  77. class function_pointer_input_iterator :
  78. public iterator_facade<
  79. iterators::function_input_iterator<Function, Input>,
  80. typename function_types::result_type<Function>::type,
  81. single_pass_traversal_tag,
  82. typename function_types::result_type<Function>::type const &
  83. >
  84. {
  85. public:
  86. function_pointer_input_iterator() {}
  87. function_pointer_input_iterator(Function &f_, Input state_ = Input())
  88. : f(f_), state(state_) {}
  89. void increment() {
  90. if (value)
  91. value = none;
  92. else
  93. (*f)();
  94. ++state;
  95. }
  96. typename function_types::result_type<Function>::type const &
  97. dereference() const {
  98. if (!value)
  99. value = (*f)();
  100. return value.get();
  101. }
  102. bool equal(function_pointer_input_iterator const & other) const {
  103. return f == other.f && state == other.state;
  104. }
  105. private:
  106. Function f;
  107. Input state;
  108. mutable optional<typename function_types::result_type<Function>::type> value;
  109. };
  110. } // namespace impl
  111. template <class Function, class Input>
  112. class function_input_iterator :
  113. public boost::conditional<
  114. function_types::is_function_pointer<Function>::value,
  115. impl::function_pointer_input_iterator<Function,Input>,
  116. impl::function_object_input_iterator<Function,Input>
  117. >::type
  118. {
  119. typedef typename boost::conditional<
  120. function_types::is_function_pointer<Function>::value,
  121. impl::function_pointer_input_iterator<Function,Input>,
  122. impl::function_object_input_iterator<Function,Input>
  123. >::type base_type;
  124. public:
  125. function_input_iterator(Function & f, Input i)
  126. : base_type(f, i) {}
  127. };
  128. template <class Function, class Input>
  129. inline function_input_iterator<Function, Input>
  130. make_function_input_iterator(Function & f, Input state) {
  131. typedef function_input_iterator<Function, Input> result_t;
  132. return result_t(f, state);
  133. }
  134. template <class Function, class Input>
  135. inline function_input_iterator<Function*, Input>
  136. make_function_input_iterator(Function * f, Input state) {
  137. typedef function_input_iterator<Function*, Input> result_t;
  138. return result_t(f, state);
  139. }
  140. struct infinite
  141. {
  142. infinite & operator++() { return *this; }
  143. infinite & operator++(int) { return *this; }
  144. bool operator==(infinite &) const { return false; };
  145. bool operator==(infinite const &) const { return false; };
  146. };
  147. } // namespace iterators
  148. using iterators::function_input_iterator;
  149. using iterators::make_function_input_iterator;
  150. using iterators::infinite;
  151. } // namespace boost
  152. #endif