|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#ifndef OBJ_MGR_ARG_FROM_PYTHON_DWA2002628_HPP |
| 7 | +# define OBJ_MGR_ARG_FROM_PYTHON_DWA2002628_HPP |
| 8 | + |
| 9 | +# include <boost/python/detail/wrap_python.hpp> |
| 10 | +# include <boost/python/detail/referent_storage.hpp> |
| 11 | +# include <boost/python/detail/destroy.hpp> |
| 12 | +# include <boost/python/detail/construct.hpp> |
| 13 | +# include <boost/python/converter/object_manager.hpp> |
| 14 | + |
| 15 | +// |
| 16 | +// arg_from_python converters for Python type wrappers, to be used as |
| 17 | +// base classes for specializations. |
| 18 | +// |
| 19 | +namespace boost { namespace python { namespace converter { |
| 20 | + |
| 21 | +template <class T> |
| 22 | +struct object_manager_value_arg_from_python |
| 23 | +{ |
| 24 | + typedef T result_type; |
| 25 | + |
| 26 | + object_manager_value_arg_from_python(PyObject*); |
| 27 | + bool convertible() const; |
| 28 | + T operator()(PyObject*) const; |
| 29 | + private: |
| 30 | + PyObject* m_source; |
| 31 | +}; |
| 32 | + |
| 33 | +template <class Ref> |
| 34 | +struct object_manager_ref_arg_from_python |
| 35 | +{ |
| 36 | + typedef Ref result_type; |
| 37 | + |
| 38 | + object_manager_ref_arg_from_python(PyObject*); |
| 39 | + bool convertible() const; |
| 40 | + Ref operator()(PyObject*) const; |
| 41 | + ~object_manager_ref_arg_from_python(); |
| 42 | + private: |
| 43 | + typename python::detail::referent_storage<Ref>::type m_result; |
| 44 | +}; |
| 45 | + |
| 46 | +// |
| 47 | +// implementations |
| 48 | +// |
| 49 | + |
| 50 | +template <class T> |
| 51 | +inline object_manager_value_arg_from_python<T>::object_manager_value_arg_from_python(PyObject* x) |
| 52 | + : m_source(x) |
| 53 | +{ |
| 54 | +} |
| 55 | + |
| 56 | +template <class T> |
| 57 | +inline bool object_manager_value_arg_from_python<T>::convertible() const |
| 58 | +{ |
| 59 | + return extract_object_manager<T>::check(m_source); |
| 60 | +} |
| 61 | + |
| 62 | +template <class T> |
| 63 | +inline T object_manager_value_arg_from_python<T>::operator()(PyObject* x) const |
| 64 | +{ |
| 65 | + return T(python::detail::borrowed_reference(x)); |
| 66 | +} |
| 67 | + |
| 68 | +template <class Ref> |
| 69 | +inline object_manager_ref_arg_from_python<Ref>::object_manager_ref_arg_from_python(PyObject* x) |
| 70 | +{ |
| 71 | + python::detail::construct_referent<Ref>(&m_result.bytes, python::detail::borrowed_reference(x)); |
| 72 | +} |
| 73 | + |
| 74 | +template <class Ref> |
| 75 | +inline object_manager_ref_arg_from_python<Ref>::~object_manager_ref_arg_from_python() |
| 76 | +{ |
| 77 | + python::detail::destroy_referent<Ref>(this->m_result.bytes); |
| 78 | +} |
| 79 | + |
| 80 | +namespace detail |
| 81 | +{ |
| 82 | + template <class T> |
| 83 | + inline bool object_manager_ref_check(T const& x) |
| 84 | + { |
| 85 | + return extract_object_manager<T>::check((get_managed_object)(x)); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +template <class Ref> |
| 90 | +inline bool object_manager_ref_arg_from_python<Ref>::convertible() const |
| 91 | +{ |
| 92 | + return detail::object_manager_ref_check( |
| 93 | + python::detail::void_ptr_to_reference(this->m_result.bytes, (Ref(*)())0)); |
| 94 | +} |
| 95 | + |
| 96 | +template <class Ref> |
| 97 | +inline Ref object_manager_ref_arg_from_python<Ref>::operator()(PyObject*) const |
| 98 | +{ |
| 99 | + return python::detail::void_ptr_to_reference( |
| 100 | + this->m_result.bytes, (Ref(*)())0); |
| 101 | +} |
| 102 | + |
| 103 | +}}} // namespace boost::python::converter |
| 104 | + |
| 105 | +#endif // OBJ_MGR_ARG_FROM_PYTHON_DWA2002628_HPP |
0 commit comments