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