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
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>
27 namespace boost { namespace python
29 template <class T> struct arg_from_python;
32 // This header defines Python->C++ function argument converters,
33 // parametrized on the argument type.
35 namespace boost { namespace python { namespace converter {
40 // These require that an lvalue of the type U is stored somewhere in
41 // the Python object being converted.
43 // Used when T == U*const&
45 struct pointer_cref_arg_from_python
47 typedef T result_type;
49 pointer_cref_arg_from_python(PyObject*);
51 bool convertible() const;
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;
59 // Base class for pointer and reference converters
60 struct arg_lvalue_from_python_base
62 public: // member functions
63 arg_lvalue_from_python_base(void* result);
64 bool convertible() const;
66 protected: // member functions
67 void*const& result() const;
69 private: // data members
75 struct pointer_arg_from_python : arg_lvalue_from_python_base
77 typedef T result_type;
79 pointer_arg_from_python(PyObject*);
83 // Used when T == U& and (T != V const& or T == W volatile&)
85 struct reference_arg_from_python : arg_lvalue_from_python_base
87 typedef T result_type;
89 reference_arg_from_python(PyObject*);
93 // ===================
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.
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.
106 struct arg_rvalue_from_python
108 typedef typename boost::python::detail::add_lvalue_reference<
110 // We can't add_const here, or it would be impossible to pass
111 // auto_ptr<U> args from Python to C++
114 arg_rvalue_from_python(PyObject*);
115 bool convertible() const;
117 # if _MSC_FULL_VER > 13102196
118 typename arg_rvalue_from_python<T>::
120 result_type operator()();
123 rvalue_from_python_data<result_type> m_data;
128 // ==================
130 // Converts to a (PyObject*,T) bundle, for when you need a reference
131 // back to the Python object
133 struct back_reference_arg_from_python
134 : boost::python::arg_from_python<typename T::type>
136 typedef T result_type;
138 back_reference_arg_from_python(PyObject*);
141 typedef boost::python::arg_from_python<typename T::type> base;
146 // ==================
148 template <class C, class T, class F>
151 typedef typename mpl::eval_if<C, mpl::identity<T>, F>::type type;
154 // This metafunction selects the appropriate arg_from_python converter
155 // type for an argument of type T.
157 struct select_arg_from_python
159 typedef typename if_2<
161 , object_manager_value_arg_from_python<T>
163 is_reference_to_object_manager<T>
164 , object_manager_ref_arg_from_python<T>
167 , pointer_arg_from_python<T>
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> >
174 , pointer_cref_arg_from_python<T>
177 indirect_traits::is_reference_to_non_const<T>
178 , indirect_traits::is_reference_to_volatile<T>
180 , reference_arg_from_python<T>
182 boost::python::is_back_reference<T>
183 , back_reference_arg_from_python<T>
184 , arg_rvalue_from_python<T>
193 // ==================
199 // arg_lvalue_from_python_base
201 inline arg_lvalue_from_python_base::arg_lvalue_from_python_base(void* result)
206 inline bool arg_lvalue_from_python_base::convertible() const
208 return m_result != 0;
211 inline void*const& arg_lvalue_from_python_base::result() const
216 // pointer_cref_arg_from_python
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.
224 struct null_ptr_owner
228 template <class T> T null_ptr_owner<T>::value = 0;
231 inline U& null_ptr_reference(U&(*)())
233 return null_ptr_owner<U>::value;
238 inline pointer_cref_arg_from_python<T>::pointer_cref_arg_from_python(PyObject* p)
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
243 python::detail::write_void_ptr_reference(
245 , p == Py_None ? p : converter::get_lvalue_from_python(p, registered_pointee<T>::converters)
250 inline bool pointer_cref_arg_from_python<T>::convertible() const
252 return python::detail::void_ptr_to_reference(m_result.bytes, (T(*)())0) != 0;
255 inline T pointer_cref_arg_from_python<T>::operator()() const
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);
263 // pointer_arg_from_python
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))
273 inline T pointer_arg_from_python<T>::operator()() const
275 return (result() == Py_None) ? 0 : T(result());
278 // reference_arg_from_python
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))
287 inline T reference_arg_from_python<T>::operator()() const
289 return python::detail::void_ptr_to_reference(result(), (T(*)())0);
293 // arg_rvalue_from_python
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))
303 inline bool arg_rvalue_from_python<T>::convertible() const
305 return m_data.stage1.convertible != 0;
309 inline typename arg_rvalue_from_python<T>::result_type
310 arg_rvalue_from_python<T>::operator()()
312 if (m_data.stage1.construct != 0)
313 m_data.stage1.construct(m_source, &m_data.stage1);
315 return python::detail::void_ptr_to_reference(m_data.stage1.convertible, (result_type(*)())0);
318 // back_reference_arg_from_python
321 back_reference_arg_from_python<T>::back_reference_arg_from_python(PyObject* x)
322 : base(x), m_source(x)
328 back_reference_arg_from_python<T>::operator()()
330 return T(m_source, base::operator()());
333 }}} // namespace boost::python::converter
335 #endif // ARG_FROM_PYTHON_DWA2002127_HPP