]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/python/object/make_instance.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / 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/type_traits.hpp>
13 # include <boost/python/detail/none.hpp>
14 # include <boost/mpl/assert.hpp>
15 # include <boost/mpl/or.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_<boost::python::detail::is_class<T>,
28 boost::python::detail::is_union<T> >));
29
30 PyTypeObject* type = Derived::get_class_object(x);
31
32 if (type == 0)
33 return python::detail::none();
34
35 PyObject* raw_result = type->tp_alloc(
36 type, objects::additional_instance_size<Holder>::value);
37
38 if (raw_result != 0)
39 {
40 python::detail::decref_guard protect(raw_result);
41
42 instance_t* instance = (instance_t*)raw_result;
43
44 // construct the new C++ object and install the pointer
45 // in the Python object.
46 Holder *holder =Derived::construct(instance->storage.bytes, (PyObject*)instance, x);
47 holder->install(raw_result);
48
49 // Note the position of the internally-stored Holder,
50 // for the sake of destruction
51 const size_t offset = reinterpret_cast<size_t>(holder) -
52 reinterpret_cast<size_t>(instance->storage.bytes) + offsetof(instance_t, storage);
53 Py_SET_SIZE(instance, offset);
54
55 // Release ownership of the python object
56 protect.cancel();
57 }
58 return raw_result;
59 }
60 };
61
62
63 template <class T, class Holder>
64 struct make_instance
65 : make_instance_impl<T, Holder, make_instance<T,Holder> >
66 {
67 template <class U>
68 static inline PyTypeObject* get_class_object(U&)
69 {
70 return converter::registered<T>::converters.get_class_object();
71 }
72
73 static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
74 {
75 size_t allocated = objects::additional_instance_size<Holder>::value;
76 void* aligned_storage = ::boost::alignment::align(boost::python::detail::alignment_of<Holder>::value,
77 sizeof(Holder), storage, allocated);
78 return new (aligned_storage) Holder(instance, x);
79 }
80 };
81
82
83 }}} // namespace boost::python::object
84
85 #endif // MAKE_INSTANCE_DWA200296_HPP