source: NonGTP/Boost/boost/python/object/make_instance.hpp @ 857

Revision 857, 2.3 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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
14namespace boost { namespace python { namespace objects {
15
16template <class T, class Holder, class Derived>
17struct make_instance_impl
18{
19    typedef objects::instance<Holder> instance_t;
20       
21    template <class Arg>
22    static inline PyObject* execute(Arg& x)
23    {
24        BOOST_STATIC_ASSERT(is_class<T>::value);
25
26        PyTypeObject* type = Derived::get_class_object(x);
27
28        if (type == 0)
29            return python::detail::none();
30
31        PyObject* raw_result = type->tp_alloc(
32            type, objects::additional_instance_size<Holder>::value);
33         
34        if (raw_result != 0)
35        {
36            python::detail::decref_guard protect(raw_result);
37           
38            instance_t* instance = (instance_t*)raw_result;
39           
40            // construct the new C++ object and install the pointer
41            // in the Python object.
42            Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
43             
44            // Note the position of the internally-stored Holder,
45            // for the sake of destruction
46            instance->ob_size = offsetof(instance_t, storage);
47
48            // Release ownership of the python object
49            protect.cancel();
50        }
51        return raw_result;
52    }
53};
54   
55
56template <class T, class Holder>
57struct make_instance
58    : make_instance_impl<T, Holder, make_instance<T,Holder> >
59{
60    template <class U>
61    static inline PyTypeObject* get_class_object(U&)
62    {
63        return converter::registered<T>::converters.get_class_object();
64    }
65   
66    static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
67    {
68        return new (storage) Holder(instance, x);
69    }
70};
71 
72
73}}} // namespace boost::python::object
74
75#endif // MAKE_INSTANCE_DWA200296_HPP
Note: See TracBrowser for help on using the repository browser.