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