| 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 TO_PYTHON_VALUE_DWA200221_HPP |
|---|
| 6 | # define TO_PYTHON_VALUE_DWA200221_HPP |
|---|
| 7 | |
|---|
| 8 | # include <boost/python/detail/prefix.hpp> |
|---|
| 9 | |
|---|
| 10 | # include <boost/python/refcount.hpp> |
|---|
| 11 | # include <boost/python/tag.hpp> |
|---|
| 12 | |
|---|
| 13 | # include <boost/python/converter/registry.hpp> |
|---|
| 14 | # include <boost/python/converter/registered.hpp> |
|---|
| 15 | # include <boost/python/converter/builtin_converters.hpp> |
|---|
| 16 | # include <boost/python/converter/object_manager.hpp> |
|---|
| 17 | # include <boost/python/converter/object_manager.hpp> |
|---|
| 18 | # include <boost/python/converter/shared_ptr_to_python.hpp> |
|---|
| 19 | |
|---|
| 20 | # include <boost/python/detail/value_is_shared_ptr.hpp> |
|---|
| 21 | # include <boost/python/detail/value_arg.hpp> |
|---|
| 22 | |
|---|
| 23 | # include <boost/type_traits/transform_traits.hpp> |
|---|
| 24 | |
|---|
| 25 | # include <boost/mpl/if.hpp> |
|---|
| 26 | # include <boost/mpl/or.hpp> |
|---|
| 27 | |
|---|
| 28 | namespace boost { namespace python { |
|---|
| 29 | |
|---|
| 30 | namespace detail |
|---|
| 31 | { |
|---|
| 32 | template <class T> |
|---|
| 33 | struct object_manager_to_python_value |
|---|
| 34 | { |
|---|
| 35 | typedef typename value_arg<T>::type argument_type; |
|---|
| 36 | |
|---|
| 37 | PyObject* operator()(argument_type) const; |
|---|
| 38 | |
|---|
| 39 | // This information helps make_getter() decide whether to try to |
|---|
| 40 | // return an internal reference or not. I don't like it much, |
|---|
| 41 | // but it will have to serve for now. |
|---|
| 42 | BOOST_STATIC_CONSTANT(bool, uses_registry = false); |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | template <class T> |
|---|
| 47 | struct registry_to_python_value |
|---|
| 48 | { |
|---|
| 49 | typedef typename value_arg<T>::type argument_type; |
|---|
| 50 | |
|---|
| 51 | PyObject* operator()(argument_type) const; |
|---|
| 52 | |
|---|
| 53 | // This information helps make_getter() decide whether to try to |
|---|
| 54 | // return an internal reference or not. I don't like it much, |
|---|
| 55 | // but it will have to serve for now. |
|---|
| 56 | BOOST_STATIC_CONSTANT(bool, uses_registry = true); |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | template <class T> |
|---|
| 60 | struct shared_ptr_to_python_value |
|---|
| 61 | { |
|---|
| 62 | typedef typename value_arg<T>::type argument_type; |
|---|
| 63 | |
|---|
| 64 | PyObject* operator()(argument_type) const; |
|---|
| 65 | |
|---|
| 66 | // This information helps make_getter() decide whether to try to |
|---|
| 67 | // return an internal reference or not. I don't like it much, |
|---|
| 68 | // but it will have to serve for now. |
|---|
| 69 | BOOST_STATIC_CONSTANT(bool, uses_registry = false); |
|---|
| 70 | }; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | template <class T> |
|---|
| 74 | struct to_python_value |
|---|
| 75 | : mpl::if_< |
|---|
| 76 | detail::value_is_shared_ptr<T> |
|---|
| 77 | , detail::shared_ptr_to_python_value<T> |
|---|
| 78 | , typename mpl::if_< |
|---|
| 79 | mpl::or_< |
|---|
| 80 | converter::is_object_manager<T> |
|---|
| 81 | , converter::is_reference_to_object_manager<T> |
|---|
| 82 | > |
|---|
| 83 | , detail::object_manager_to_python_value<T> |
|---|
| 84 | , detail::registry_to_python_value<T> |
|---|
| 85 | >::type |
|---|
| 86 | >::type |
|---|
| 87 | { |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | // |
|---|
| 91 | // implementation |
|---|
| 92 | // |
|---|
| 93 | namespace detail |
|---|
| 94 | { |
|---|
| 95 | template <class T> |
|---|
| 96 | inline PyObject* registry_to_python_value<T>::operator()(argument_type x) const |
|---|
| 97 | { |
|---|
| 98 | typedef converter::registered<argument_type> r; |
|---|
| 99 | # if BOOST_WORKAROUND(__GNUC__, < 3) |
|---|
| 100 | // suppresses an ICE, somehow |
|---|
| 101 | (void)r::converters; |
|---|
| 102 | # endif |
|---|
| 103 | return converter::registered<argument_type>::converters.to_python(&x); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | template <class T> |
|---|
| 107 | inline PyObject* object_manager_to_python_value<T>::operator()(argument_type x) const |
|---|
| 108 | { |
|---|
| 109 | return python::upcast<PyObject>( |
|---|
| 110 | python::xincref( |
|---|
| 111 | get_managed_object(x, tag)) |
|---|
| 112 | ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | template <class T> |
|---|
| 116 | inline PyObject* shared_ptr_to_python_value<T>::operator()(argument_type x) const |
|---|
| 117 | { |
|---|
| 118 | return converter::shared_ptr_to_python(x); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | }} // namespace boost::python |
|---|
| 123 | |
|---|
| 124 | #endif // TO_PYTHON_VALUE_DWA200221_HPP |
|---|