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>
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>
16 # include <boost/python/object/iterator_core.hpp>
17 # include <boost/python/object/class_detail.hpp>
18 # include <boost/python/object/function_object.hpp>
20 # include <boost/mpl/vector/vector10.hpp>
21 # include <boost/mpl/if.hpp>
23 # include <boost/python/detail/raw_pyobject.hpp>
25 # include <boost/type.hpp>
27 # include <boost/type_traits/is_same.hpp>
28 # include <boost/type_traits/add_reference.hpp>
29 # include <boost/type_traits/add_const.hpp>
31 # include <boost/detail/iterator.hpp>
33 namespace boost { namespace python { namespace objects {
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;
42 // Instantiations of these are wrapped to produce Python iterators.
43 template <class NextPolicies, class Iterator>
46 iterator_range(object sequence, Iterator start, Iterator finish);
48 typedef boost::detail::iterator_traits<Iterator> traits_t;
52 typedef typename mpl::if_<
54 typename traits_t::reference
56 , typename traits_t::reference
57 , typename traits_t::value_type
61 operator()(iterator_range<NextPolicies,Iterator>& self)
63 if (self.m_start == self.m_finish)
64 stop_iteration_error();
65 return *self.m_start++;
68 # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
69 // CWPro8 has a codegen problem when this is an empty class
76 object m_sequence; // Keeps the sequence alive while iterating.
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>
87 object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
89 typedef iterator_range<NextPolicies,Iterator> range_;
91 // Check the registry. If one is already registered, return it.
93 objects::registered_class_object(python::type_id<range_>()));
95 if (class_obj.get() != 0)
96 return object(class_obj);
98 typedef typename range_::next_fn next_fn;
99 typedef typename next_fn::result_type result_type;
101 return class_<range_>(name, no_init)
102 .def("__iter__", identity_function())
104 #if PY_VERSION_HEX >= 0x03000000
112 , mpl::vector2<result_type,range_&>()
116 // A function object which builds an iterator_range.
126 py_iter_(Accessor1 const& get_start, Accessor2 const& get_finish)
127 : m_get_start(get_start)
128 , m_get_finish(get_finish)
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>
136 operator()(back_reference<Target&> x) const
138 // Make sure the Python class is instantiated.
139 detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies());
141 return iterator_range<NextPolicies,Iterator>(
143 , m_get_start(x.get())
144 , m_get_finish(x.get())
148 Accessor1 m_get_start;
149 Accessor2 m_get_finish;
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>*
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&> >()
169 template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2>
170 inline object make_iterator_function(
171 Accessor1 const& get_start
172 , Accessor2 const& get_finish
173 , NextPolicies const& next_policies
175 , boost::type<Target>*
178 return make_iterator_function(
182 , (Iterator const&(*)())0
183 , (boost::type<Target>*)0
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>
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
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;
208 return detail::make_iterator_function(
212 , (iterator_cref(*)())0
213 , (boost::type<Target>*)0
221 template <class NextPolicies, class Iterator>
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)
228 }}} // namespace boost::python::objects
230 #endif // ITERATOR_DWA2002510_HPP