iterator.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright David Abrahams 2002.
  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. #ifndef ITERATOR_DWA2002510_HPP
  6. # define ITERATOR_DWA2002510_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/class.hpp>
  9. # include <boost/python/return_value_policy.hpp>
  10. # include <boost/python/return_by_value.hpp>
  11. # include <boost/python/handle.hpp>
  12. # include <boost/python/make_function.hpp>
  13. # include <boost/python/object/iterator_core.hpp>
  14. # include <boost/python/object/class_detail.hpp>
  15. # include <boost/python/object/function_object.hpp>
  16. # include <boost/mpl/vector/vector10.hpp>
  17. # include <boost/mpl/if.hpp>
  18. # include <boost/python/detail/raw_pyobject.hpp>
  19. # include <boost/type.hpp>
  20. # include <boost/type_traits/is_same.hpp>
  21. # include <boost/type_traits/add_reference.hpp>
  22. # include <boost/type_traits/add_const.hpp>
  23. # include <boost/detail/iterator.hpp>
  24. namespace boost { namespace python { namespace objects {
  25. // CallPolicies for the next() method of iterators. We don't want
  26. // users to have to explicitly specify that the references returned by
  27. // iterators are copied, so we just replace the result_converter from
  28. // the default_iterator_call_policies with a permissive one which
  29. // always copies the result.
  30. typedef return_value_policy<return_by_value> default_iterator_call_policies;
  31. // Instantiations of these are wrapped to produce Python iterators.
  32. template <class NextPolicies, class Iterator>
  33. struct iterator_range
  34. {
  35. iterator_range(object sequence, Iterator start, Iterator finish);
  36. typedef boost::detail::iterator_traits<Iterator> traits_t;
  37. struct next
  38. {
  39. typedef typename mpl::if_<
  40. is_reference<
  41. typename traits_t::reference
  42. >
  43. , typename traits_t::reference
  44. , typename traits_t::value_type
  45. >::type result_type;
  46. result_type
  47. operator()(iterator_range<NextPolicies,Iterator>& self)
  48. {
  49. if (self.m_start == self.m_finish)
  50. stop_iteration_error();
  51. return *self.m_start++;
  52. }
  53. # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  54. // CWPro8 has a codegen problem when this is an empty class
  55. int garbage;
  56. # endif
  57. };
  58. typedef next next_fn;
  59. object m_sequence; // Keeps the sequence alive while iterating.
  60. Iterator m_start;
  61. Iterator m_finish;
  62. };
  63. namespace detail
  64. {
  65. // Get a Python class which contains the given iterator and
  66. // policies, creating it if necessary. Requires: NextPolicies is
  67. // default-constructible.
  68. template <class Iterator, class NextPolicies>
  69. object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
  70. {
  71. typedef iterator_range<NextPolicies,Iterator> range_;
  72. // Check the registry. If one is already registered, return it.
  73. handle<> class_obj(
  74. objects::registered_class_object(python::type_id<range_>()));
  75. if (class_obj.get() != 0)
  76. return object(class_obj);
  77. typedef typename range_::next_fn next_fn;
  78. typedef typename next_fn::result_type result_type;
  79. return class_<range_>(name, no_init)
  80. .def("__iter__", identity_function())
  81. .def(
  82. #if PY_VERSION_HEX >= 0x03000000
  83. "__next__"
  84. #else
  85. "next"
  86. #endif
  87. , make_function(
  88. next_fn()
  89. , policies
  90. , mpl::vector2<result_type,range_&>()
  91. ));
  92. }
  93. // A function object which builds an iterator_range.
  94. template <
  95. class Target
  96. , class Iterator
  97. , class Accessor1
  98. , class Accessor2
  99. , class NextPolicies
  100. >
  101. struct py_iter_
  102. {
  103. py_iter_(Accessor1 const& get_start, Accessor2 const& get_finish)
  104. : m_get_start(get_start)
  105. , m_get_finish(get_finish)
  106. {}
  107. // Extract an object x of the Target type from the first Python
  108. // argument, and invoke get_start(x)/get_finish(x) to produce
  109. // iterators, which are used to construct a new iterator_range<>
  110. // object that gets wrapped into a Python iterator.
  111. iterator_range<NextPolicies,Iterator>
  112. operator()(back_reference<Target&> x) const
  113. {
  114. // Make sure the Python class is instantiated.
  115. detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies());
  116. return iterator_range<NextPolicies,Iterator>(
  117. x.source()
  118. , m_get_start(x.get())
  119. , m_get_finish(x.get())
  120. );
  121. }
  122. private:
  123. Accessor1 m_get_start;
  124. Accessor2 m_get_finish;
  125. };
  126. template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
  127. inline object make_iterator_function(
  128. Accessor1 const& get_start
  129. , Accessor2 const& get_finish
  130. , NextPolicies const& /*next_policies*/
  131. , Iterator const& (*)()
  132. , boost::type<Target>*
  133. , int
  134. )
  135. {
  136. return make_function(
  137. py_iter_<Target,Iterator,Accessor1,Accessor2,NextPolicies>(get_start, get_finish)
  138. , default_call_policies()
  139. , mpl::vector2<iterator_range<NextPolicies,Iterator>, back_reference<Target&> >()
  140. );
  141. }
  142. template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
  143. inline object make_iterator_function(
  144. Accessor1 const& get_start
  145. , Accessor2 const& get_finish
  146. , NextPolicies const& next_policies
  147. , Iterator& (*)()
  148. , boost::type<Target>*
  149. , ...)
  150. {
  151. return make_iterator_function(
  152. get_start
  153. , get_finish
  154. , next_policies
  155. , (Iterator const&(*)())0
  156. , (boost::type<Target>*)0
  157. , 0
  158. );
  159. }
  160. }
  161. // Create a Python callable object which accepts a single argument
  162. // convertible to the C++ Target type and returns a Python
  163. // iterator. The Python iterator uses get_start(x) and get_finish(x)
  164. // (where x is an instance of Target) to produce begin and end
  165. // iterators for the range, and an instance of NextPolicies is used as
  166. // CallPolicies for the Python iterator's next() function.
  167. template <class Target, class NextPolicies, class Accessor1, class Accessor2>
  168. inline object make_iterator_function(
  169. Accessor1 const& get_start
  170. , Accessor2 const& get_finish
  171. , NextPolicies const& next_policies
  172. , boost::type<Target>* = 0
  173. )
  174. {
  175. typedef typename Accessor1::result_type iterator;
  176. typedef typename add_const<iterator>::type iterator_const;
  177. typedef typename add_reference<iterator_const>::type iterator_cref;
  178. return detail::make_iterator_function(
  179. get_start
  180. , get_finish
  181. , next_policies
  182. , (iterator_cref(*)())0
  183. , (boost::type<Target>*)0
  184. , 0
  185. );
  186. }
  187. //
  188. // implementation
  189. //
  190. template <class NextPolicies, class Iterator>
  191. inline iterator_range<NextPolicies,Iterator>::iterator_range(
  192. object sequence, Iterator start, Iterator finish)
  193. : m_sequence(sequence), m_start(start), m_finish(finish)
  194. {
  195. }
  196. }}} // namespace boost::python::objects
  197. #endif // ITERATOR_DWA2002510_HPP