iterator.hpp 6.9 KB

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