Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/python/object/iterator.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
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 | |
61 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 # ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION | |
75 // for compilers which can't deduce the value_type of pointers, we | |
76 // have a special implementation of next. This takes advantage of | |
77 // the fact that T* results are treated like T& results by | |
78 // Boost.Python's function wrappers. | |
79 struct next_ptr | |
80 { | |
81 typedef Iterator result_type; | |
82 | |
83 result_type | |
84 operator()(iterator_range<NextPolicies,Iterator>& self) | |
85 { | |
86 if (self.m_start == self.m_finish) | |
87 stop_iteration_error(); | |
88 return self.m_start++; | |
89 } | |
90 }; | |
91 | |
92 typedef mpl::if_< | |
93 is_same< | |
94 boost::detail::please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee<Iterator> | |
95 , typename traits_t::value_type | |
96 > | |
97 , next_ptr | |
98 , next | |
99 >::type next_fn; | |
100 # else | |
101 typedef next next_fn; | |
102 # endif | |
103 | |
104 object m_sequence; // Keeps the sequence alive while iterating. | |
105 Iterator m_start; | |
106 Iterator m_finish; | |
107 }; | |
108 | |
109 namespace detail | |
110 { | |
111 // Get a Python class which contains the given iterator and | |
112 // policies, creating it if necessary. Requires: NextPolicies is | |
113 // default-constructible. | |
114 template <class Iterator, class NextPolicies> | |
115 object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies()) | |
116 { | |
117 typedef iterator_range<NextPolicies,Iterator> range_; | |
118 | |
119 // Check the registry. If one is already registered, return it. | |
120 handle<> class_obj( | |
121 objects::registered_class_object(python::type_id<range_>())); | |
122 | |
123 if (class_obj.get() != 0) | |
124 return object(class_obj); | |
125 | |
126 typedef typename range_::next_fn next_fn; | |
127 typedef typename next_fn::result_type result_type; | |
128 | |
129 return class_<range_>(name, no_init) | |
130 .def("__iter__", identity_function()) | |
131 .def( | |
132 #if PY_VERSION_HEX >= 0x03000000 | |
133 "__next__" | |
134 #else | |
135 "next" | |
136 #endif | |
137 , make_function( | |
138 next_fn() | |
139 , policies | |
140 , mpl::vector2<result_type,range_&>() | |
141 )); | |
142 } | |
143 | |
144 // A function object which builds an iterator_range. | |
145 template < | |
146 class Target | |
147 , class Iterator | |
148 , class Accessor1 | |
149 , class Accessor2 | |
150 , class NextPolicies | |
151 > | |
152 struct py_iter_ | |
153 { | |
154 py_iter_(Accessor1 const& get_start, Accessor2 const& get_finish) | |
155 : m_get_start(get_start) | |
156 , m_get_finish(get_finish) | |
157 {} | |
158 | |
159 // Extract an object x of the Target type from the first Python | |
160 // argument, and invoke get_start(x)/get_finish(x) to produce | |
161 // iterators, which are used to construct a new iterator_range<> | |
162 // object that gets wrapped into a Python iterator. | |
163 iterator_range<NextPolicies,Iterator> | |
164 operator()(back_reference<Target&> x) const | |
165 { | |
166 // Make sure the Python class is instantiated. | |
167 detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies()); | |
168 | |
169 return iterator_range<NextPolicies,Iterator>( | |
170 x.source() | |
171 , m_get_start(x.get()) | |
172 , m_get_finish(x.get()) | |
173 ); | |
174 } | |
175 private: | |
176 Accessor1 m_get_start; | |
177 Accessor2 m_get_finish; | |
178 }; | |
179 | |
180 template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2> | |
181 inline object make_iterator_function( | |
182 Accessor1 const& get_start | |
183 , Accessor2 const& get_finish | |
184 , NextPolicies const& /*next_policies*/ | |
185 , Iterator const& (*)() | |
186 , boost::type<Target>* | |
187 , int | |
188 ) | |
189 { | |
190 return make_function( | |
191 py_iter_<Target,Iterator,Accessor1,Accessor2,NextPolicies>(get_start, get_finish) | |
192 , default_call_policies() | |
193 , mpl::vector2<iterator_range<NextPolicies,Iterator>, back_reference<Target&> >() | |
194 ); | |
195 } | |
196 | |
197 template <class Target, class Iterator, class NextPolicies, class Accessor1, class Accessor2> | |
198 inline object make_iterator_function( | |
199 Accessor1 const& get_start | |
200 , Accessor2 const& get_finish | |
201 , NextPolicies const& next_policies | |
202 , Iterator& (*)() | |
203 , boost::type<Target>* | |
204 , ...) | |
205 { | |
206 return make_iterator_function( | |
207 get_start | |
208 , get_finish | |
209 , next_policies | |
210 , (Iterator const&(*)())0 | |
211 , (boost::type<Target>*)0 | |
212 , 0 | |
213 ); | |
214 } | |
215 | |
216 } | |
217 | |
218 // Create a Python callable object which accepts a single argument | |
219 // convertible to the C++ Target type and returns a Python | |
220 // iterator. The Python iterator uses get_start(x) and get_finish(x) | |
221 // (where x is an instance of Target) to produce begin and end | |
222 // iterators for the range, and an instance of NextPolicies is used as | |
223 // CallPolicies for the Python iterator's next() function. | |
224 template <class Target, class NextPolicies, class Accessor1, class Accessor2> | |
225 inline object make_iterator_function( | |
226 Accessor1 const& get_start | |
227 , Accessor2 const& get_finish | |
228 , NextPolicies const& next_policies | |
229 , boost::type<Target>* = 0 | |
230 ) | |
231 { | |
232 typedef typename Accessor1::result_type iterator; | |
233 typedef typename add_const<iterator>::type iterator_const; | |
234 typedef typename add_reference<iterator_const>::type iterator_cref; | |
235 | |
236 return detail::make_iterator_function( | |
237 get_start | |
238 , get_finish | |
239 , next_policies | |
240 , (iterator_cref(*)())0 | |
241 , (boost::type<Target>*)0 | |
242 , 0 | |
243 ); | |
244 } | |
245 | |
246 // | |
247 // implementation | |
248 // | |
249 template <class NextPolicies, class Iterator> | |
250 inline iterator_range<NextPolicies,Iterator>::iterator_range( | |
251 object sequence, Iterator start, Iterator finish) | |
252 : m_sequence(sequence), m_start(start), m_finish(finish) | |
253 { | |
254 } | |
255 | |
256 }}} // namespace boost::python::objects | |
257 | |
258 #endif // ITERATOR_DWA2002510_HPP |