]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/python/converter/arg_from_python.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / python / converter / arg_from_python.hpp
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 ARG_FROM_PYTHON_DWA2002127_HPP
6 # define ARG_FROM_PYTHON_DWA2002127_HPP
7
8 # include <boost/python/detail/prefix.hpp>
9 # include <boost/python/converter/from_python.hpp>
10 # include <boost/python/detail/indirect_traits.hpp>
11 # include <boost/python/detail/type_traits.hpp>
12 # include <boost/python/converter/rvalue_from_python_data.hpp>
13 # include <boost/mpl/eval_if.hpp>
14 # include <boost/mpl/if.hpp>
15 # include <boost/mpl/identity.hpp>
16 # include <boost/mpl/and.hpp>
17 # include <boost/mpl/or.hpp>
18 # include <boost/mpl/not.hpp>
19 # include <boost/python/converter/registry.hpp>
20 # include <boost/python/converter/registered.hpp>
21 # include <boost/python/converter/registered_pointee.hpp>
22 # include <boost/python/detail/void_ptr.hpp>
23 # include <boost/python/back_reference.hpp>
24 # include <boost/python/detail/referent_storage.hpp>
25 # include <boost/python/converter/obj_mgr_arg_from_python.hpp>
26
27 namespace boost { namespace python
28 {
29 template <class T> struct arg_from_python;
30 }}
31
32 // This header defines Python->C++ function argument converters,
33 // parametrized on the argument type.
34
35 namespace boost { namespace python { namespace converter {
36
37 //
38 // lvalue converters
39 //
40 // These require that an lvalue of the type U is stored somewhere in
41 // the Python object being converted.
42
43 // Used when T == U*const&
44 template <class T>
45 struct pointer_cref_arg_from_python
46 {
47 typedef T result_type;
48
49 pointer_cref_arg_from_python(PyObject*);
50 T operator()() const;
51 bool convertible() const;
52
53 private: // storage for a U*
54 // needed because not all compilers will let us declare U* as the
55 // return type of operator() -- we return U*const& instead
56 typename python::detail::referent_storage<T>::type m_result;
57 };
58
59 // Base class for pointer and reference converters
60 struct arg_lvalue_from_python_base
61 {
62 public: // member functions
63 arg_lvalue_from_python_base(void* result);
64 bool convertible() const;
65
66 protected: // member functions
67 void*const& result() const;
68
69 private: // data members
70 void* m_result;
71 };
72
73 // Used when T == U*
74 template <class T>
75 struct pointer_arg_from_python : arg_lvalue_from_python_base
76 {
77 typedef T result_type;
78
79 pointer_arg_from_python(PyObject*);
80 T operator()() const;
81 };
82
83 // Used when T == U& and (T != V const& or T == W volatile&)
84 template <class T>
85 struct reference_arg_from_python : arg_lvalue_from_python_base
86 {
87 typedef T result_type;
88
89 reference_arg_from_python(PyObject*);
90 T operator()() const;
91 };
92
93 // ===================
94
95 //
96 // rvalue converters
97 //
98 // These require only that an object of type T can be created from
99 // the given Python object, but not that the T object exist
100 // somewhere in storage.
101 //
102
103 // Used when T is a plain value (non-pointer, non-reference) type or
104 // a (non-volatile) const reference to a plain value type.
105 template <class T>
106 struct arg_rvalue_from_python
107 {
108 typedef typename boost::python::detail::add_lvalue_reference<
109 T
110 // We can't add_const here, or it would be impossible to pass
111 // auto_ptr<U> args from Python to C++
112 >::type result_type;
113
114 arg_rvalue_from_python(PyObject*);
115 bool convertible() const;
116
117 # if _MSC_FULL_VER > 13102196
118 typename arg_rvalue_from_python<T>::
119 # endif
120 result_type operator()();
121
122 private:
123 rvalue_from_python_data<result_type> m_data;
124 PyObject* m_source;
125 };
126
127
128 // ==================
129
130 // Converts to a (PyObject*,T) bundle, for when you need a reference
131 // back to the Python object
132 template <class T>
133 struct back_reference_arg_from_python
134 : boost::python::arg_from_python<typename T::type>
135 {
136 typedef T result_type;
137
138 back_reference_arg_from_python(PyObject*);
139 T operator()();
140 private:
141 typedef boost::python::arg_from_python<typename T::type> base;
142 PyObject* m_source;
143 };
144
145
146 // ==================
147
148 template <class C, class T, class F>
149 struct if_2
150 {
151 typedef typename mpl::eval_if<C, mpl::identity<T>, F>::type type;
152 };
153
154 // This metafunction selects the appropriate arg_from_python converter
155 // type for an argument of type T.
156 template <class T>
157 struct select_arg_from_python
158 {
159 typedef typename if_2<
160 is_object_manager<T>
161 , object_manager_value_arg_from_python<T>
162 , if_2<
163 is_reference_to_object_manager<T>
164 , object_manager_ref_arg_from_python<T>
165 , if_2<
166 is_pointer<T>
167 , pointer_arg_from_python<T>
168 , if_2<
169 mpl::and_<
170 indirect_traits::is_reference_to_pointer<T>
171 , indirect_traits::is_reference_to_const<T>
172 , mpl::not_<indirect_traits::is_reference_to_volatile<T> >
173 >
174 , pointer_cref_arg_from_python<T>
175 , if_2<
176 mpl::or_<
177 indirect_traits::is_reference_to_non_const<T>
178 , indirect_traits::is_reference_to_volatile<T>
179 >
180 , reference_arg_from_python<T>
181 , mpl::if_<
182 boost::python::is_back_reference<T>
183 , back_reference_arg_from_python<T>
184 , arg_rvalue_from_python<T>
185 >
186 >
187 >
188 >
189 >
190 >::type type;
191 };
192
193 // ==================
194
195 //
196 // implementations
197 //
198
199 // arg_lvalue_from_python_base
200 //
201 inline arg_lvalue_from_python_base::arg_lvalue_from_python_base(void* result)
202 : m_result(result)
203 {
204 }
205
206 inline bool arg_lvalue_from_python_base::convertible() const
207 {
208 return m_result != 0;
209 }
210
211 inline void*const& arg_lvalue_from_python_base::result() const
212 {
213 return m_result;
214 }
215
216 // pointer_cref_arg_from_python
217 //
218 namespace detail
219 {
220 // null_ptr_reference -- a function returning a reference to a null
221 // pointer of type U. Needed so that extractors for T*const& can
222 // convert Python's None.
223 template <class T>
224 struct null_ptr_owner
225 {
226 static T value;
227 };
228 template <class T> T null_ptr_owner<T>::value = 0;
229
230 template <class U>
231 inline U& null_ptr_reference(U&(*)())
232 {
233 return null_ptr_owner<U>::value;
234 }
235 }
236
237 template <class T>
238 inline pointer_cref_arg_from_python<T>::pointer_cref_arg_from_python(PyObject* p)
239 {
240 // T == U*const&: store a U* in the m_result storage. Nonzero
241 // indicates success. If find returns nonzero, it's a pointer to
242 // a U object.
243 python::detail::write_void_ptr_reference(
244 m_result.bytes
245 , p == Py_None ? p : converter::get_lvalue_from_python(p, registered_pointee<T>::converters)
246 , (T(*)())0);
247 }
248
249 template <class T>
250 inline bool pointer_cref_arg_from_python<T>::convertible() const
251 {
252 return python::detail::void_ptr_to_reference(m_result.bytes, (T(*)())0) != 0;
253 }
254 template <class T>
255 inline T pointer_cref_arg_from_python<T>::operator()() const
256 {
257 return (*(void**)m_result.bytes == Py_None) // None ==> 0
258 ? detail::null_ptr_reference((T(*)())0)
259 // Otherwise, return a U*const& to the m_result storage.
260 : python::detail::void_ptr_to_reference(m_result.bytes, (T(*)())0);
261 }
262
263 // pointer_arg_from_python
264 //
265 template <class T>
266 inline pointer_arg_from_python<T>::pointer_arg_from_python(PyObject* p)
267 : arg_lvalue_from_python_base(
268 p == Py_None ? p : converter::get_lvalue_from_python(p, registered_pointee<T>::converters))
269 {
270 }
271
272 template <class T>
273 inline T pointer_arg_from_python<T>::operator()() const
274 {
275 return (result() == Py_None) ? 0 : T(result());
276 }
277
278 // reference_arg_from_python
279 //
280 template <class T>
281 inline reference_arg_from_python<T>::reference_arg_from_python(PyObject* p)
282 : arg_lvalue_from_python_base(converter::get_lvalue_from_python(p,registered<T>::converters))
283 {
284 }
285
286 template <class T>
287 inline T reference_arg_from_python<T>::operator()() const
288 {
289 return python::detail::void_ptr_to_reference(result(), (T(*)())0);
290 }
291
292
293 // arg_rvalue_from_python
294 //
295 template <class T>
296 inline arg_rvalue_from_python<T>::arg_rvalue_from_python(PyObject* obj)
297 : m_data(converter::rvalue_from_python_stage1(obj, registered<T>::converters))
298 , m_source(obj)
299 {
300 }
301
302 template <class T>
303 inline bool arg_rvalue_from_python<T>::convertible() const
304 {
305 return m_data.stage1.convertible != 0;
306 }
307
308 template <class T>
309 inline typename arg_rvalue_from_python<T>::result_type
310 arg_rvalue_from_python<T>::operator()()
311 {
312 if (m_data.stage1.construct != 0)
313 m_data.stage1.construct(m_source, &m_data.stage1);
314
315 return python::detail::void_ptr_to_reference(m_data.stage1.convertible, (result_type(*)())0);
316 }
317
318 // back_reference_arg_from_python
319 //
320 template <class T>
321 back_reference_arg_from_python<T>::back_reference_arg_from_python(PyObject* x)
322 : base(x), m_source(x)
323 {
324 }
325
326 template <class T>
327 inline T
328 back_reference_arg_from_python<T>::operator()()
329 {
330 return T(m_source, base::operator()());
331 }
332
333 }}} // namespace boost::python::converter
334
335 #endif // ARG_FROM_PYTHON_DWA2002127_HPP