]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/include/boost/python/object/make_instance.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / python / include / boost / python / object / make_instance.hpp
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 MAKE_INSTANCE_DWA200296_HPP
6 # define MAKE_INSTANCE_DWA200296_HPP
7
8 # include <boost/python/detail/prefix.hpp>
9 # include <boost/python/object/instance.hpp>
10 # include <boost/python/converter/registered.hpp>
11 # include <boost/python/detail/decref_guard.hpp>
12 # include <boost/python/detail/none.hpp>
13 # include <boost/mpl/assert.hpp>
14 # include <boost/mpl/or.hpp>
15 # include <boost/type_traits/is_union.hpp>
16
17 namespace boost { namespace python { namespace objects {
18
19 template <class T, class Holder, class Derived>
20 struct make_instance_impl
21 {
22 typedef objects::instance<Holder> instance_t;
23
24 template <class Arg>
25 static inline PyObject* execute(Arg& x)
26 {
27 BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >));
28
29 PyTypeObject* type = Derived::get_class_object(x);
30
31 if (type == 0)
32 return python::detail::none();
33
34 PyObject* raw_result = type->tp_alloc(
35 type, objects::additional_instance_size<Holder>::value);
36
37 if (raw_result != 0)
38 {
39 python::detail::decref_guard protect(raw_result);
40
41 instance_t* instance = (instance_t*)raw_result;
42
43 // construct the new C++ object and install the pointer
44 // in the Python object.
45 Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
46
47 // Note the position of the internally-stored Holder,
48 // for the sake of destruction
49 Py_SIZE(instance) = offsetof(instance_t, storage);
50
51 // Release ownership of the python object
52 protect.cancel();
53 }
54 return raw_result;
55 }
56 };
57
58
59 template <class T, class Holder>
60 struct make_instance
61 : make_instance_impl<T, Holder, make_instance<T,Holder> >
62 {
63 template <class U>
64 static inline PyTypeObject* get_class_object(U&)
65 {
66 return converter::registered<T>::converters.get_class_object();
67 }
68
69 static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
70 {
71 return new (storage) Holder(instance, x);
72 }
73 };
74
75
76 }}} // namespace boost::python::object
77
78 #endif // MAKE_INSTANCE_DWA200296_HPP