// Copyright David Abrahams 2002. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef ARG_TO_PYTHON_DWA200265_HPP # define ARG_TO_PYTHON_DWA200265_HPP # include # include # include # include # include # include # include // Bring in specializations # include # include # include # include # include # include # include # include # include namespace boost { namespace python { namespace converter { template struct is_object_manager; namespace detail { template struct function_arg_to_python : handle<> { function_arg_to_python(T const& x); }; template struct reference_arg_to_python : handle<> { reference_arg_to_python(T& x); private: static PyObject* get_object(T& x); }; template struct shared_ptr_arg_to_python : handle<> { shared_ptr_arg_to_python(T const& x); private: static PyObject* get_object(T& x); }; template struct value_arg_to_python : arg_to_python_base { // Throw an exception if the conversion can't succeed value_arg_to_python(T const&); }; template struct pointer_deep_arg_to_python : arg_to_python_base { // Throw an exception if the conversion can't succeed pointer_deep_arg_to_python(Ptr); }; template struct pointer_shallow_arg_to_python : handle<> { // Throw an exception if the conversion can't succeed pointer_shallow_arg_to_python(Ptr); private: static PyObject* get_object(Ptr p); }; // Convert types that manage a Python object to_python template struct object_manager_arg_to_python { object_manager_arg_to_python(T const& x) : m_src(x) {} PyObject* get() const { return python::upcast(get_managed_object(m_src, tag)); } private: T const& m_src; }; template struct select_arg_to_python { typedef typename unwrap_reference::type unwrapped_referent; typedef typename unwrap_pointer::type unwrapped_ptr; typedef typename mpl::if_< // Special handling for char const[N]; interpret them as char // const* for the sake of conversion python::detail::is_string_literal , arg_to_python , typename mpl::if_< python::detail::value_is_shared_ptr , shared_ptr_arg_to_python , typename mpl::if_< mpl::or_< boost::python::detail::is_function , indirect_traits::is_pointer_to_function , boost::python::detail::is_member_function_pointer > , function_arg_to_python , typename mpl::if_< is_object_manager , object_manager_arg_to_python , typename mpl::if_< boost::python::detail::is_pointer , pointer_deep_arg_to_python , typename mpl::if_< is_pointer_wrapper , pointer_shallow_arg_to_python , typename mpl::if_< is_reference_wrapper , reference_arg_to_python , value_arg_to_python >::type >::type >::type >::type >::type >::type >::type type; }; } template struct arg_to_python : detail::select_arg_to_python::type { typedef typename detail::select_arg_to_python::type base; public: // member functions // Throw an exception if the conversion can't succeed arg_to_python(T const& x); }; // // implementations // namespace detail { // reject_raw_object_ptr -- cause a compile-time error if the user // should pass a raw Python object pointer using python::detail::yes_convertible; using python::detail::no_convertible; using python::detail::unspecialized; template struct cannot_convert_raw_PyObject; template struct reject_raw_object_helper { static void error(Convertibility) { cannot_convert_raw_PyObject::to_python_use_handle_instead(); } static void error(...) {} }; template inline void reject_raw_object_ptr(T*) { reject_raw_object_helper::error( python::detail::convertible::check((T*)0)); typedef typename remove_cv::type value_type; reject_raw_object_helper::error( python::detail::convertible::check( (base_type_traits*)0 )); } // --------- template inline function_arg_to_python::function_arg_to_python(T const& x) : handle<>(python::objects::make_function_handle(x)) { } template inline value_arg_to_python::value_arg_to_python(T const& x) : arg_to_python_base(&x, registered::converters) { } template inline pointer_deep_arg_to_python::pointer_deep_arg_to_python(Ptr x) : arg_to_python_base(x, registered_pointee::converters) { detail::reject_raw_object_ptr((Ptr)0); } template inline PyObject* reference_arg_to_python::get_object(T& x) { to_python_indirect convert; return convert(x); } template inline reference_arg_to_python::reference_arg_to_python(T& x) : handle<>(reference_arg_to_python::get_object(x)) { } template inline shared_ptr_arg_to_python::shared_ptr_arg_to_python(T const& x) : handle<>(shared_ptr_to_python(x)) { } template inline pointer_shallow_arg_to_python::pointer_shallow_arg_to_python(Ptr x) : handle<>(pointer_shallow_arg_to_python::get_object(x)) { detail::reject_raw_object_ptr((Ptr)0); } template inline PyObject* pointer_shallow_arg_to_python::get_object(Ptr x) { to_python_indirect convert; return convert(x); } } template inline arg_to_python::arg_to_python(T const& x) : base(x) {} }}} // namespace boost::python::converter #endif // ARG_TO_PYTHON_DWA200265_HPP