| 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_PTR_INSTANCE_DWA200296_HPP |
|---|
| 6 | # define MAKE_PTR_INSTANCE_DWA200296_HPP |
|---|
| 7 | |
|---|
| 8 | # include <boost/python/object/make_instance.hpp> |
|---|
| 9 | # include <boost/python/converter/registry.hpp> |
|---|
| 10 | # include <boost/type_traits/is_polymorphic.hpp> |
|---|
| 11 | # include <boost/get_pointer.hpp> |
|---|
| 12 | # include <boost/detail/workaround.hpp> |
|---|
| 13 | # include <typeinfo> |
|---|
| 14 | |
|---|
| 15 | namespace boost { namespace python { namespace objects { |
|---|
| 16 | |
|---|
| 17 | template <class T, class Holder> |
|---|
| 18 | struct make_ptr_instance |
|---|
| 19 | : make_instance_impl<T, Holder, make_ptr_instance<T,Holder> > |
|---|
| 20 | { |
|---|
| 21 | template <class Arg> |
|---|
| 22 | static inline Holder* construct(void* storage, PyObject*, Arg& x) |
|---|
| 23 | { |
|---|
| 24 | return new (storage) Holder(x); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | template <class Ptr> |
|---|
| 28 | static inline PyTypeObject* get_class_object(Ptr const& x) |
|---|
| 29 | { |
|---|
| 30 | return get_class_object_impl(get_pointer(x)); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | private: |
|---|
| 34 | template <class U> |
|---|
| 35 | static inline PyTypeObject* get_class_object_impl(U const volatile* p) |
|---|
| 36 | { |
|---|
| 37 | if (p == 0) |
|---|
| 38 | return 0; // means "return None". |
|---|
| 39 | |
|---|
| 40 | PyTypeObject* derived = get_derived_class_object( |
|---|
| 41 | BOOST_DEDUCED_TYPENAME is_polymorphic<U>::type(), p); |
|---|
| 42 | |
|---|
| 43 | if (derived) |
|---|
| 44 | return derived; |
|---|
| 45 | return converter::registered<T>::converters.get_class_object(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | template <class U> |
|---|
| 49 | static inline PyTypeObject* get_derived_class_object(mpl::true_, U const volatile* x) |
|---|
| 50 | { |
|---|
| 51 | converter::registration const* r = converter::registry::query( |
|---|
| 52 | type_info(typeid(*get_pointer(x))) |
|---|
| 53 | ); |
|---|
| 54 | return r ? r->m_class_object : 0; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | template <class U> |
|---|
| 58 | static inline PyTypeObject* get_derived_class_object(mpl::false_, U*) |
|---|
| 59 | { |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | }}} // namespace boost::python::object |
|---|
| 66 | |
|---|
| 67 | #endif // MAKE_PTR_INSTANCE_DWA200296_HPP |
|---|