iterator.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. # ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  59. // for compilers which can't deduce the value_type of pointers, we
  60. // have a special implementation of next. This takes advantage of
  61. // the fact that T* results are treated like T& results by
  62. // Boost.Python's function wrappers.
  63. struct next_ptr
  64. {
  65. typedef Iterator result_type;
  66. result_type
  67. operator()(iterator_range<NextPolicies,Iterator>& self)
  68. {
  69. if (self.m_start == self.m_finish)
  70. stop_iteration_error();
  71. return self.m_start++;
  72. }
  73. };
  74. typedef mpl::if_<
  75. is_same<
  76. boost::detail::please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee<Iterator>
  77. , typename traits_t::value_type
  78. >
  79. , next_ptr
  80. , next
  81. >::type next_fn;
  82. # else
  83. typedef next next_fn;
  84. # endif
  85. object m_sequence; // Keeps the sequence alive while iterating.
  86. Iterator m_start;
  87. Iterator m_finish;
  88. };
  89. namespace detail
  90. {
  91. // Get a Python class which contains the given iterator and
  92. // policies, creating it if necessary. Requires: NextPolicies is
  93. // default-constructible.
  94. template <class Iterator, class NextPolicies>
  95. object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
  96. {
  97. typedef iterator_range<NextPolicies,Iterator> range_;
  98. // Check the registry. If one is already registered, return it.
  99. handle<> class_obj(
  100. objects::registered_class_object(python::type_id<range_>()));
  101. if (class_obj.get() != 0)
  102. return object(class_obj);
  103. typedef typename range_::next_fn next_fn;
  104. typedef typename next_fn::result_type result_type;
  105. return class_<range_>(name, no_init)
  106. .def("__iter__", identity_function())
  107. .def(
  108. #if PY_VERSION_HEX >= 0x03000000
  109. "__next__"
  110. #else
  111. "next"
  112. #endif
  113. , make_function(
  114. next_fn()
  115. , policies
  116. , mpl::vector2<result_type,range_&>()
  117. ));
  118. }
  119. // A function object which builds an iterator_range.
  120. template <
  121. class Target
  122. , class Iterator
  123. , class Accessor1
  124. , class Accessor2
  125. , class NextPolicies
  126. >
  127. struct py_iter_
  128. {
  129. py_iter_(Accessor1 const& get_start, Accessor2 const& get_finish)
  130. : m_get_start(get_start)
  131. , m_get_finish(get_finish)
  132. {}
  133. // Extract an object x of the Target type from the first Python
  134. // argument, and invoke get_start(x)/get_finish(x) to produce
  135. // iterators, which are used to construct a new iterator_range<>
  136. // object that gets wrapped into a Python iterator.
  137. iterator_range<NextPolicies,Iterator>
  138. operator()(back_reference<Target&> x) const
  139. {
  140. // Make sure the Python class is instantiated.
  141. detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies());
  142. return iterator_range<NextPolicies,Iterator>(
  143. x.source()
  144. , m_get_start(x.get())
  145. , m_get_finish(x.get())
  146. );
  147. }
  148. private:
  149. Accessor1 m_get_start;
  150. Accessor2 m_get_finish;
  151. };
  152. template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
  153. inline object make_iterator_function(
  154. Accessor1 const& get_start
  155. , Accessor2 const& get_finish
  156. , NextPolicies const& /*next_policies*/
  157. , Iterator const& (*)()
  158. , boost::type<Target>*
  159. , int
  160. )
  161. {
  162. return make_function(
  163. py_iter_<Target,Iterator,Accessor1,Accessor2,NextPolicies>(get_start, get_finish)
  164. , default_call_policies()
  165. , mpl::vector2<iterator_range<NextPolicies,Iterator>, back_reference<Target&> >()
  166. );
  167. }
  168. template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
  169. inline object make_iterator_function(
  170. Accessor1 const& get_start
  171. , Accessor2 const& get_finish
  172. , NextPolicies const& next_policies
  173. , Iterator& (*)()
  174. , boost::type<Target>*
  175. , ...)
  176. {
  177. return make_iterator_function(
  178. get_start
  179. , get_finish
  180. , next_policies
  181. , (Iterator const&(*)())0
  182. , (boost::type<Target>*)0
  183. , 0
  184. );
  185. }
  186. }
  187. // Create a Python callable object which accepts a single argument
  188. // convertible to the C++ Target type and returns a Python
  189. // iterator. The Python iterator uses get_start(x) and get_finish(x)
  190. // (where x is an instance of Target) to produce begin and end
  191. // iterators for the range, and an instance of NextPolicies is used as
  192. // CallPolicies for the Python iterator's next() function.
  193. template <class Target, class NextPolicies, class Accessor1, class Accessor2>
  194. inline object make_iterator_function(
  195. Accessor1 const& get_start
  196. , Accessor2 const& get_finish
  197. , NextPolicies const& next_policies
  198. , boost::type<Target>* = 0
  199. )
  200. {
  201. typedef typename Accessor1::result_type iterator;
  202. typedef typename add_const<iterator>::type iterator_const;
  203. typedef typename add_reference<iterator_const>::type iterator_cref;
  204. return detail::make_iterator_function(
  205. get_start
  206. , get_finish
  207. , next_policies
  208. , (iterator_cref(*)())0
  209. , (boost::type<Target>*)0
  210. , 0
  211. );
  212. }
  213. //
  214. // implementation
  215. //
  216. template <class NextPolicies, class Iterator>
  217. inline iterator_range<NextPolicies,Iterator>::iterator_range(
  218. object sequence, Iterator start, Iterator finish)
  219. : m_sequence(sequence), m_start(start), m_finish(finish)
  220. {
  221. }
  222. }}} // namespace boost::python::objects
  223. #endif // ITERATOR_DWA2002510_HPP