Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef MAKE_PTR_INSTANCE_DWA200296_HPP
|
Chris@16
|
6 # define MAKE_PTR_INSTANCE_DWA200296_HPP
|
Chris@16
|
7
|
Chris@16
|
8 # include <boost/python/object/make_instance.hpp>
|
Chris@16
|
9 # include <boost/python/converter/registry.hpp>
|
Chris@16
|
10 # include <boost/type_traits/is_polymorphic.hpp>
|
Chris@16
|
11 # include <boost/get_pointer.hpp>
|
Chris@16
|
12 # include <boost/detail/workaround.hpp>
|
Chris@16
|
13 # include <typeinfo>
|
Chris@16
|
14
|
Chris@16
|
15 namespace boost { namespace python { namespace objects {
|
Chris@16
|
16
|
Chris@16
|
17 template <class T, class Holder>
|
Chris@16
|
18 struct make_ptr_instance
|
Chris@16
|
19 : make_instance_impl<T, Holder, make_ptr_instance<T,Holder> >
|
Chris@16
|
20 {
|
Chris@16
|
21 template <class Arg>
|
Chris@16
|
22 static inline Holder* construct(void* storage, PyObject*, Arg& x)
|
Chris@16
|
23 {
|
Chris@16
|
24 return new (storage) Holder(x);
|
Chris@16
|
25 }
|
Chris@16
|
26
|
Chris@16
|
27 template <class Ptr>
|
Chris@16
|
28 static inline PyTypeObject* get_class_object(Ptr const& x)
|
Chris@16
|
29 {
|
Chris@16
|
30 return get_class_object_impl(get_pointer(x));
|
Chris@16
|
31 }
|
Chris@16
|
32 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
|
Chris@16
|
33 static inline PyTypeObject const* get_pytype()
|
Chris@16
|
34 {
|
Chris@16
|
35 return converter::registered<T>::converters.get_class_object();
|
Chris@16
|
36 }
|
Chris@16
|
37 #endif
|
Chris@16
|
38 private:
|
Chris@16
|
39 template <class U>
|
Chris@16
|
40 static inline PyTypeObject* get_class_object_impl(U const volatile* p)
|
Chris@16
|
41 {
|
Chris@16
|
42 if (p == 0)
|
Chris@16
|
43 return 0; // means "return None".
|
Chris@16
|
44
|
Chris@16
|
45 PyTypeObject* derived = get_derived_class_object(
|
Chris@16
|
46 BOOST_DEDUCED_TYPENAME is_polymorphic<U>::type(), p);
|
Chris@16
|
47
|
Chris@16
|
48 if (derived)
|
Chris@16
|
49 return derived;
|
Chris@16
|
50 return converter::registered<T>::converters.get_class_object();
|
Chris@16
|
51 }
|
Chris@16
|
52
|
Chris@16
|
53 template <class U>
|
Chris@16
|
54 static inline PyTypeObject* get_derived_class_object(mpl::true_, U const volatile* x)
|
Chris@16
|
55 {
|
Chris@16
|
56 converter::registration const* r = converter::registry::query(
|
Chris@16
|
57 type_info(typeid(*get_pointer(x)))
|
Chris@16
|
58 );
|
Chris@16
|
59 return r ? r->m_class_object : 0;
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 template <class U>
|
Chris@16
|
63 static inline PyTypeObject* get_derived_class_object(mpl::false_, U*)
|
Chris@16
|
64 {
|
Chris@16
|
65 return 0;
|
Chris@16
|
66 }
|
Chris@16
|
67 };
|
Chris@16
|
68
|
Chris@16
|
69
|
Chris@16
|
70 }}} // namespace boost::python::object
|
Chris@16
|
71
|
Chris@16
|
72 #endif // MAKE_PTR_INSTANCE_DWA200296_HPP
|