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_TO_PYTHON_DWA200265_HPP
6 # define ARG_TO_PYTHON_DWA200265_HPP
8 # include <boost/python/ptr.hpp>
9 # include <boost/python/tag.hpp>
10 # include <boost/python/to_python_indirect.hpp>
12 # include <boost/python/converter/registered.hpp>
13 # include <boost/python/converter/registered_pointee.hpp>
14 # include <boost/python/converter/arg_to_python_base.hpp>
15 # include <boost/python/converter/shared_ptr_to_python.hpp>
16 // Bring in specializations
17 # include <boost/python/converter/builtin_converters.hpp>
19 # include <boost/python/object/function_handle.hpp>
21 # include <boost/python/base_type_traits.hpp>
23 # include <boost/python/detail/indirect_traits.hpp>
24 # include <boost/python/detail/convertible.hpp>
25 # include <boost/python/detail/string_literal.hpp>
26 # include <boost/python/detail/value_is_shared_ptr.hpp>
28 # include <boost/type_traits/cv_traits.hpp>
29 # include <boost/type_traits/composite_traits.hpp>
30 # include <boost/type_traits/function_traits.hpp>
33 # include <boost/mpl/or.hpp>
35 namespace boost { namespace python { namespace converter {
37 template <class T> struct is_object_manager;
42 struct function_arg_to_python : handle<>
44 function_arg_to_python(T const& x);
48 struct reference_arg_to_python : handle<>
50 reference_arg_to_python(T& x);
52 static PyObject* get_object(T& x);
56 struct shared_ptr_arg_to_python : handle<>
58 shared_ptr_arg_to_python(T const& x);
60 static PyObject* get_object(T& x);
64 struct value_arg_to_python : arg_to_python_base
66 // Throw an exception if the conversion can't succeed
67 value_arg_to_python(T const&);
71 struct pointer_deep_arg_to_python : arg_to_python_base
73 // Throw an exception if the conversion can't succeed
74 pointer_deep_arg_to_python(Ptr);
78 struct pointer_shallow_arg_to_python : handle<>
80 // Throw an exception if the conversion can't succeed
81 pointer_shallow_arg_to_python(Ptr);
83 static PyObject* get_object(Ptr p);
86 // Convert types that manage a Python object to_python
88 struct object_manager_arg_to_python
90 object_manager_arg_to_python(T const& x) : m_src(x) {}
94 return python::upcast<PyObject>(get_managed_object(m_src, tag));
102 struct select_arg_to_python
104 typedef typename unwrap_reference<T>::type unwrapped_referent;
105 typedef typename unwrap_pointer<T>::type unwrapped_ptr;
107 typedef typename mpl::if_<
108 // Special handling for char const[N]; interpret them as char
109 // const* for the sake of conversion
110 python::detail::is_string_literal<T const>
111 , arg_to_python<char const*>
114 python::detail::value_is_shared_ptr<T>
115 , shared_ptr_arg_to_python<T>
120 , indirect_traits::is_pointer_to_function<T>
121 , is_member_function_pointer<T>
123 , function_arg_to_python<T>
127 , object_manager_arg_to_python<T>
131 , pointer_deep_arg_to_python<T>
134 is_pointer_wrapper<T>
135 , pointer_shallow_arg_to_python<unwrapped_ptr>
138 is_reference_wrapper<T>
139 , reference_arg_to_python<unwrapped_referent>
140 , value_arg_to_python<T>
155 : detail::select_arg_to_python<T>::type
157 typedef typename detail::select_arg_to_python<T>::type base;
158 public: // member functions
159 // Throw an exception if the conversion can't succeed
160 arg_to_python(T const& x);
168 // reject_raw_object_ptr -- cause a compile-time error if the user
169 // should pass a raw Python object pointer
170 using python::detail::yes_convertible;
171 using python::detail::no_convertible;
172 using python::detail::unspecialized;
174 template <class T> struct cannot_convert_raw_PyObject;
176 template <class T, class Convertibility>
177 struct reject_raw_object_helper
179 static void error(Convertibility)
181 cannot_convert_raw_PyObject<T*>::to_python_use_handle_instead();
183 static void error(...) {}
187 inline void reject_raw_object_ptr(T*)
189 reject_raw_object_helper<T,yes_convertible>::error(
190 python::detail::convertible<PyObject const volatile*>::check((T*)0));
192 typedef typename remove_cv<T>::type value_type;
194 reject_raw_object_helper<T,no_convertible>::error(
195 python::detail::convertible<unspecialized*>::check(
196 (base_type_traits<value_type>*)0
202 inline function_arg_to_python<T>::function_arg_to_python(T const& x)
203 : handle<>(python::objects::make_function_handle(x))
208 inline value_arg_to_python<T>::value_arg_to_python(T const& x)
209 : arg_to_python_base(&x, registered<T>::converters)
214 inline pointer_deep_arg_to_python<Ptr>::pointer_deep_arg_to_python(Ptr x)
215 : arg_to_python_base(x, registered_pointee<Ptr>::converters)
217 detail::reject_raw_object_ptr((Ptr)0);
221 inline PyObject* reference_arg_to_python<T>::get_object(T& x)
223 to_python_indirect<T&,python::detail::make_reference_holder> convert;
228 inline reference_arg_to_python<T>::reference_arg_to_python(T& x)
229 : handle<>(reference_arg_to_python<T>::get_object(x))
234 inline shared_ptr_arg_to_python<T>::shared_ptr_arg_to_python(T const& x)
235 : handle<>(shared_ptr_to_python(x))
240 inline pointer_shallow_arg_to_python<Ptr>::pointer_shallow_arg_to_python(Ptr x)
241 : handle<>(pointer_shallow_arg_to_python<Ptr>::get_object(x))
243 detail::reject_raw_object_ptr((Ptr)0);
247 inline PyObject* pointer_shallow_arg_to_python<Ptr>::get_object(Ptr x)
249 to_python_indirect<Ptr,python::detail::make_reference_holder> convert;
255 inline arg_to_python<T>::arg_to_python(T const& x)
259 }}} // namespace boost::python::converter
261 #endif // ARG_TO_PYTHON_DWA200265_HPP