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
8 # include <boost/python/detail/prefix.hpp>
9 # include <boost/python/detail/type_traits.hpp>
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>
17 # include <boost/python/object/iterator_core.hpp>
18 # include <boost/python/object/class_detail.hpp>
19 # include <boost/python/object/function_object.hpp>
21 # include <boost/mpl/vector/vector10.hpp>
22 # include <boost/mpl/if.hpp>
24 # include <boost/python/detail/raw_pyobject.hpp>
26 # include <boost/type.hpp>
28 # include <boost/detail/iterator.hpp>
30 namespace boost { namespace python { namespace objects {
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;
39 // Instantiations of these are wrapped to produce Python iterators.
40 template <class NextPolicies, class Iterator>
43 iterator_range(object sequence, Iterator start, Iterator finish);
45 typedef boost::detail::iterator_traits<Iterator> traits_t;
49 typedef typename mpl::if_<
51 typename traits_t::reference
53 , typename traits_t::reference
54 , typename traits_t::value_type
58 operator()(iterator_range<NextPolicies,Iterator>& self)
60 if (self.m_start == self.m_finish)
61 stop_iteration_error();
62 return *self.m_start++;
65 # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
66 // CWPro8 has a codegen problem when this is an empty class
73 object m_sequence; // Keeps the sequence alive while iterating.
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>
84 object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
86 typedef iterator_range<NextPolicies,Iterator> range_;
88 // Check the registry. If one is already registered, return it.
90 objects::registered_class_object(python::type_id<range_>()));
92 if (class_obj.get() != 0)
93 return object(class_obj);
95 typedef typename range_::next_fn next_fn;
96 typedef typename next_fn::result_type result_type;
98 return class_<range_>(name, no_init)
99 .def("__iter__", identity_function())
101 #if PY_VERSION_HEX >= 0x03000000
109 , mpl::vector2<result_type,range_&>()
113 // A function object which builds an iterator_range.
123 py_iter_(Accessor1 const& get_start, Accessor2 const& get_finish)
124 : m_get_start(get_start)
125 , m_get_finish(get_finish)
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>
133 operator()(back_reference<Target&> x) const
135 // Make sure the Python class is instantiated.
136 detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies());
138 return iterator_range<NextPolicies,Iterator>(
140 , m_get_start(x.get())
141 , m_get_finish(x.get())
145 Accessor1 m_get_start;
146 Accessor2 m_get_finish;
149 template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
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>*
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&> >()
166 template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
167 inline object make_iterator_function(
168 Accessor1 const& get_start
169 , Accessor2 const& get_finish
170 , NextPolicies const& next_policies
172 , boost::type<Target>*
175 return make_iterator_function(
179 , (Iterator const&(*)())0
180 , (boost::type<Target>*)0
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
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;
205 return detail::make_iterator_function(
209 , (iterator_cref(*)())0
210 , (boost::type<Target>*)0
218 template <class NextPolicies, class Iterator>
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)
225 }}} // namespace boost::python::objects
227 #endif // ITERATOR_DWA2002510_HPP