Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef MAKE_INSTANCE_DWA200296_HPP
|
Chris@16
|
6 # define MAKE_INSTANCE_DWA200296_HPP
|
Chris@16
|
7
|
Chris@16
|
8 # include <boost/python/detail/prefix.hpp>
|
Chris@16
|
9 # include <boost/python/object/instance.hpp>
|
Chris@16
|
10 # include <boost/python/converter/registered.hpp>
|
Chris@16
|
11 # include <boost/python/detail/decref_guard.hpp>
|
Chris@16
|
12 # include <boost/python/detail/none.hpp>
|
Chris@16
|
13 # include <boost/mpl/assert.hpp>
|
Chris@16
|
14 # include <boost/mpl/or.hpp>
|
Chris@16
|
15 # include <boost/type_traits/is_union.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost { namespace python { namespace objects {
|
Chris@16
|
18
|
Chris@16
|
19 template <class T, class Holder, class Derived>
|
Chris@16
|
20 struct make_instance_impl
|
Chris@16
|
21 {
|
Chris@16
|
22 typedef objects::instance<Holder> instance_t;
|
Chris@16
|
23
|
Chris@16
|
24 template <class Arg>
|
Chris@16
|
25 static inline PyObject* execute(Arg& x)
|
Chris@16
|
26 {
|
Chris@16
|
27 BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >));
|
Chris@16
|
28
|
Chris@16
|
29 PyTypeObject* type = Derived::get_class_object(x);
|
Chris@16
|
30
|
Chris@16
|
31 if (type == 0)
|
Chris@16
|
32 return python::detail::none();
|
Chris@16
|
33
|
Chris@16
|
34 PyObject* raw_result = type->tp_alloc(
|
Chris@16
|
35 type, objects::additional_instance_size<Holder>::value);
|
Chris@16
|
36
|
Chris@16
|
37 if (raw_result != 0)
|
Chris@16
|
38 {
|
Chris@16
|
39 python::detail::decref_guard protect(raw_result);
|
Chris@16
|
40
|
Chris@16
|
41 instance_t* instance = (instance_t*)raw_result;
|
Chris@16
|
42
|
Chris@16
|
43 // construct the new C++ object and install the pointer
|
Chris@16
|
44 // in the Python object.
|
Chris@16
|
45 Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
|
Chris@16
|
46
|
Chris@16
|
47 // Note the position of the internally-stored Holder,
|
Chris@16
|
48 // for the sake of destruction
|
Chris@16
|
49 Py_SIZE(instance) = offsetof(instance_t, storage);
|
Chris@16
|
50
|
Chris@16
|
51 // Release ownership of the python object
|
Chris@16
|
52 protect.cancel();
|
Chris@16
|
53 }
|
Chris@16
|
54 return raw_result;
|
Chris@16
|
55 }
|
Chris@16
|
56 };
|
Chris@16
|
57
|
Chris@16
|
58
|
Chris@16
|
59 template <class T, class Holder>
|
Chris@16
|
60 struct make_instance
|
Chris@16
|
61 : make_instance_impl<T, Holder, make_instance<T,Holder> >
|
Chris@16
|
62 {
|
Chris@16
|
63 template <class U>
|
Chris@16
|
64 static inline PyTypeObject* get_class_object(U&)
|
Chris@16
|
65 {
|
Chris@16
|
66 return converter::registered<T>::converters.get_class_object();
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
|
Chris@16
|
70 {
|
Chris@16
|
71 return new (storage) Holder(instance, x);
|
Chris@16
|
72 }
|
Chris@16
|
73 };
|
Chris@16
|
74
|
Chris@16
|
75
|
Chris@16
|
76 }}} // namespace boost::python::object
|
Chris@16
|
77
|
Chris@16
|
78 #endif // MAKE_INSTANCE_DWA200296_HPP
|