Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/python/object/make_instance.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef MAKE_INSTANCE_DWA200296_HPP
0006 # define MAKE_INSTANCE_DWA200296_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 # include <boost/python/object/instance.hpp>
0010 # include <boost/python/converter/registered.hpp>
0011 # include <boost/python/detail/decref_guard.hpp>
0012 # include <boost/python/detail/type_traits.hpp>
0013 # include <boost/python/detail/none.hpp>
0014 # include <boost/mpl/assert.hpp>
0015 # include <boost/mpl/or.hpp>
0016 
0017 namespace boost { namespace python { namespace objects { 
0018 
0019 template <class T, class Holder, class Derived>
0020 struct make_instance_impl
0021 {
0022     typedef objects::instance<Holder> instance_t;
0023         
0024     template <class Arg>
0025     static inline PyObject* execute(Arg& x)
0026     {
0027         BOOST_MPL_ASSERT((mpl::or_<boost::python::detail::is_class<T>,
0028                 boost::python::detail::is_union<T> >));
0029 
0030         PyTypeObject* type = Derived::get_class_object(x);
0031 
0032         if (type == 0)
0033             return python::detail::none();
0034 
0035         PyObject* raw_result = type->tp_alloc(
0036             type, objects::additional_instance_size<Holder>::value);
0037           
0038         if (raw_result != 0)
0039         {
0040             python::detail::decref_guard protect(raw_result);
0041             
0042             instance_t* instance = (instance_t*)raw_result;
0043             
0044             // construct the new C++ object and install the pointer
0045             // in the Python object.
0046             Holder *holder =Derived::construct(instance->storage.bytes, (PyObject*)instance, x);
0047             holder->install(raw_result);
0048               
0049             // Note the position of the internally-stored Holder,
0050             // for the sake of destruction
0051             const size_t offset = reinterpret_cast<size_t>(holder) -
0052               reinterpret_cast<size_t>(instance->storage.bytes) + offsetof(instance_t, storage);
0053             Py_SET_SIZE(instance, offset);
0054 
0055             // Release ownership of the python object
0056             protect.cancel();
0057         }
0058         return raw_result;
0059     }
0060 };
0061     
0062 
0063 template <class T, class Holder>
0064 struct make_instance
0065     : make_instance_impl<T, Holder, make_instance<T,Holder> >
0066 {
0067     template <class U>
0068     static inline PyTypeObject* get_class_object(U&)
0069     {
0070         return converter::registered<T>::converters.get_class_object();
0071     }
0072     
0073     static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
0074     {
0075         size_t allocated = objects::additional_instance_size<Holder>::value;
0076         void* aligned_storage = ::boost::alignment::align(boost::python::detail::alignment_of<Holder>::value,
0077                                                           sizeof(Holder), storage, allocated);
0078         return new (aligned_storage) Holder(instance, x);
0079     }
0080 };
0081   
0082 
0083 }}} // namespace boost::python::object
0084 
0085 #endif // MAKE_INSTANCE_DWA200296_HPP