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 RETURN_FROM_PYTHON_DWA200265_HPP
6 # define RETURN_FROM_PYTHON_DWA200265_HPP
8 # include <boost/python/converter/from_python.hpp>
9 # include <boost/python/converter/rvalue_from_python_data.hpp>
10 # include <boost/python/converter/registered.hpp>
11 # include <boost/python/converter/registered_pointee.hpp>
12 # include <boost/python/converter/object_manager.hpp>
13 # include <boost/python/detail/void_ptr.hpp>
14 # include <boost/python/detail/void_return.hpp>
15 # include <boost/python/errors.hpp>
16 # include <boost/python/handle.hpp>
17 # include <boost/type_traits/has_trivial_copy.hpp>
18 # include <boost/mpl/and.hpp>
19 # include <boost/mpl/bool.hpp>
21 namespace boost { namespace python { namespace converter {
23 template <class T> struct is_object_manager;
28 struct return_pointer_from_python
30 typedef T result_type;
31 T operator()(PyObject*) const;
35 struct return_reference_from_python
37 typedef T result_type;
38 T operator()(PyObject*) const;
42 struct return_rvalue_from_python
44 typedef T result_type;
46 return_rvalue_from_python();
47 result_type operator()(PyObject*);
49 rvalue_from_python_data<T> m_data;
53 struct return_object_manager_from_python
55 typedef T result_type;
56 result_type operator()(PyObject*) const;
60 struct select_return_from_python
62 BOOST_STATIC_CONSTANT(
63 bool, obj_mgr = is_object_manager<T>::value);
65 BOOST_STATIC_CONSTANT(
66 bool, ptr = is_pointer<T>::value);
68 BOOST_STATIC_CONSTANT(
69 bool, ref = is_reference<T>::value);
71 typedef typename mpl::if_c<
73 , return_object_manager_from_python<T>
76 , return_pointer_from_python<T>
79 , return_reference_from_python<T>
80 , return_rvalue_from_python<T>
88 struct return_from_python
89 : detail::select_return_from_python<T>::type
93 // Specialization as a convenience for call and call_method
95 struct return_from_python<void>
97 typedef python::detail::returnable<void>::type result_type;
99 result_type operator()(PyObject* x) const
101 (void_result_from_python)(x);
102 # ifdef BOOST_NO_VOID_RETURNS
103 return result_type();
114 inline return_rvalue_from_python<T>::return_rvalue_from_python()
116 const_cast<registration*>(®istered<T>::converters)
122 inline typename return_rvalue_from_python<T>::result_type
123 return_rvalue_from_python<T>::operator()(PyObject* obj)
125 // Take possession of the source object here. If the result is in
126 // fact going to be a copy of an lvalue embedded in the object,
127 // and we take possession inside rvalue_result_from_python, it
128 // will be destroyed too early.
129 handle<> holder(obj);
132 (rvalue_result_from_python)(obj, m_data.stage1);
136 inline T return_reference_from_python<T>::operator()(PyObject* obj) const
138 return python::detail::void_ptr_to_reference(
139 (reference_result_from_python)(obj, registered<T>::converters)
144 inline T return_pointer_from_python<T>::operator()(PyObject* obj) const
147 (pointer_result_from_python)(obj, registered_pointee<T>::converters)
152 inline T return_object_manager_from_python<T>::operator()(PyObject* obj) const
155 object_manager_traits<T>::adopt(expect_non_null(obj))
160 }}} // namespace boost::python::converter
162 #endif // RETURN_FROM_PYTHON_DWA200265_HPP