|
| 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 PYTYPE_ARG_FROM_PYTHON_DWA2002628_HPP |
| 7 | +# define PYTYPE_ARG_FROM_PYTHON_DWA2002628_HPP |
| 8 | + |
| 9 | +# include <boost/python/detail/wrap_python.hpp> |
| 10 | + |
| 11 | +// |
| 12 | +// arg_from_python converters for Python type wrappers, to be used as |
| 13 | +// base classes for specializations. |
| 14 | +// |
| 15 | +namespace boost { namespace python { namespace converter { |
| 16 | + |
| 17 | +template <PyTypeObject* python_type> |
| 18 | +struct pytype_arg_from_python |
| 19 | +{ |
| 20 | + pytype_arg_from_python(PyObject*); |
| 21 | + bool convertible() const; |
| 22 | + private: |
| 23 | + PyObject* m_src; |
| 24 | +}; |
| 25 | + |
| 26 | +// rvalue converter base |
| 27 | +template <class Wrapper, PyTypeObject* python_type> |
| 28 | +struct pytype_wrapper_value_arg_from_python |
| 29 | + : pytype_arg_from_python<python_type> |
| 30 | +{ |
| 31 | + typedef Wrapper result_type; |
| 32 | + |
| 33 | + pytype_wrapper_value_arg_from_python(PyObject*); |
| 34 | + Wrapper operator()(PyObject*) const; |
| 35 | +}; |
| 36 | + |
| 37 | +// Special case for Wrapper& - must store an lvalue internally. This |
| 38 | +// OK because the entire state of the object is actually in the Python |
| 39 | +// object. |
| 40 | +template <class Wrapper, PyTypeObject* python_type> |
| 41 | +struct pytype_wrapper_ref_arg_from_python |
| 42 | + : pytype_arg_from_python<python_type> |
| 43 | +{ |
| 44 | + typedef Wrapper& result_type; |
| 45 | + |
| 46 | + pytype_wrapper_ref_arg_from_python(PyObject*); |
| 47 | + Wrapper& operator()(PyObject*) const; |
| 48 | + private: |
| 49 | + mutable Wrapper m_result; |
| 50 | +}; |
| 51 | + |
| 52 | +// |
| 53 | +// implementations |
| 54 | +// |
| 55 | + |
| 56 | +template <PyTypeObject* python_type> |
| 57 | +inline pytype_arg_from_python<python_type>::pytype_arg_from_python(PyObject* x) |
| 58 | + : m_src(x) |
| 59 | +{ |
| 60 | +} |
| 61 | + |
| 62 | +template <PyTypeObject* python_type> |
| 63 | +inline bool pytype_arg_from_python<python_type>::convertible() const |
| 64 | +{ |
| 65 | + return PyObject_IsInstance(m_src, (PyObject*)python_type); |
| 66 | +} |
| 67 | + |
| 68 | +template <class Wrapper, PyTypeObject* python_type> |
| 69 | +pytype_wrapper_value_arg_from_python<Wrapper,python_type>::pytype_wrapper_value_arg_from_python( |
| 70 | + PyObject* p) |
| 71 | + : pytype_arg_from_python<python_type>(p) |
| 72 | +{ |
| 73 | +} |
| 74 | + |
| 75 | +template <class Wrapper, PyTypeObject* python_type> |
| 76 | +Wrapper pytype_wrapper_value_arg_from_python<Wrapper,python_type>::operator()( |
| 77 | + PyObject* x) const |
| 78 | +{ |
| 79 | + return Wrapper(python::detail::borrowed_reference(x)); |
| 80 | +} |
| 81 | + |
| 82 | +template <class Wrapper, PyTypeObject* python_type> |
| 83 | +pytype_wrapper_ref_arg_from_python<Wrapper,python_type>::pytype_wrapper_ref_arg_from_python( |
| 84 | + PyObject* p) |
| 85 | + : pytype_arg_from_python<python_type>(p) |
| 86 | + , m_result(python::detail::borrowed_reference(p)) |
| 87 | +{ |
| 88 | +} |
| 89 | + |
| 90 | +template <class Wrapper, PyTypeObject* python_type> |
| 91 | +Wrapper& pytype_wrapper_ref_arg_from_python<Wrapper,python_type>::operator()( |
| 92 | + PyObject* x) const |
| 93 | +{ |
| 94 | + return m_result; |
| 95 | +} |
| 96 | + |
| 97 | +}}} // namespace boost::python::converter |
| 98 | + |
| 99 | +#endif // PYTYPE_ARG_FROM_PYTHON_DWA2002628_HPP |
0 commit comments