1 // Copyright Gottfried Ganßauge 2003..2006.
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)
6 * Generic Conversion of opaque C++-pointers to a Python-Wrapper.
8 # ifndef OPAQUE_POINTER_CONVERTER_HPP_
9 # define OPAQUE_POINTER_CONVERTER_HPP_
11 # include <boost/python/detail/prefix.hpp>
12 # include <boost/python/lvalue_from_pytype.hpp>
13 # include <boost/python/to_python_converter.hpp>
14 # include <boost/python/converter/registrations.hpp>
15 # include <boost/python/detail/dealloc.hpp>
16 # include <boost/python/detail/type_traits.hpp>
17 # include <boost/python/detail/none.hpp>
18 # include <boost/python/type_id.hpp>
19 # include <boost/python/errors.hpp>
21 # include <boost/implicit_cast.hpp>
23 # include <boost/mpl/eval_if.hpp>
24 # include <boost/mpl/identity.hpp>
25 # include <boost/mpl/assert.hpp>
29 // registers to- and from- python conversions for a type Pointee.
32 // In addition you need to define specializations for type_id
33 // on the type pointed to by Pointer using
34 // BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(Pointee)
36 // For an example see libs/python/test/opaque.cpp
38 namespace boost { namespace python {
40 template <class Pointee>
45 if (type_object.tp_name == 0)
47 type_object.tp_name = const_cast<char*>(type_id<Pointee*>().name());
48 if (PyType_Ready (&type_object) < 0)
50 throw error_already_set();
53 this->register_self();
57 static opaque instance;
60 static void* extract(PyObject* op)
62 return PyObject_TypeCheck(op, &type_object)
63 ? static_cast<python_instance*>(implicit_cast<void*>(op))->x
68 static PyObject* wrap(void const* px)
70 Pointee* x = *static_cast<Pointee*const*>(px);
73 return detail::none();
75 if ( python_instance *o = PyObject_New(python_instance, &type_object) )
78 return static_cast<PyObject*>(implicit_cast<void*>(o));
82 throw error_already_set();
88 converter::registration const *existing =
89 converter::registry::query (type_id<Pointee*>());
91 if ((existing == 0) || (existing->m_to_python == 0))
93 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
94 converter::registry::insert(&extract, type_id<Pointee>(), &get_pytype);
95 converter::registry::insert(&wrap, type_id<Pointee*>(), &get_pytype);
97 converter::registry::insert(&extract, type_id<Pointee>());
98 converter::registry::insert(&wrap, type_id<Pointee*>());
103 struct python_instance
109 static PyTypeObject type_object;
110 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
111 static PyTypeObject const *get_pytype(){return &type_object; }
115 template <class Pointee>
116 opaque<Pointee> opaque<Pointee>::instance;
118 template <class Pointee>
119 PyTypeObject opaque<Pointee>::type_object =
121 PyVarObject_HEAD_INIT(NULL, 0)
123 sizeof( BOOST_DEDUCED_TYPENAME opaque<Pointee>::python_instance ),
125 ::boost::python::detail::dealloc,
131 0, /* tp_as_number */
132 0, /* tp_as_sequence */
133 0, /* tp_as_mapping */
139 0, /* tp_as_buffer */
144 0, /* tp_richcompare */
145 0, /* tp_weaklistoffset */
153 0, /* tp_descr_get */
154 0, /* tp_descr_set */
155 0, /* tp_dictoffset */
164 0, /* tp_subclasses */
166 #if PYTHON_API_VERSION >= 1012
170 }} // namespace boost::python
172 // If you change the below, don't forget to alter the end of type_id.hpp
173 # define BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(Pointee) \
174 namespace boost { namespace python { \
176 inline type_info type_id<Pointee>() \
178 return type_info (typeid (Pointee *)); \
181 inline type_info type_id<const volatile Pointee&>() \
183 return type_info (typeid (Pointee *)); \
187 # endif // OPAQUE_POINTER_CONVERTER_HPP_