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 INHERITANCE_DWA200216_HPP
6 # define INHERITANCE_DWA200216_HPP
7 
8 # include <boost/python/type_id.hpp>
9 # include <boost/shared_ptr.hpp>
10 # include <boost/mpl/if.hpp>
11 # include <boost/detail/workaround.hpp>
12 # include <boost/python/detail/type_traits.hpp>
13 
14 namespace boost { namespace python { namespace objects {
15 
16 typedef type_info class_id;
17 using python::type_id;
18 
19 // Types used to get address and id of most derived type
20 typedef std::pair<void*,class_id> dynamic_id_t;
21 typedef dynamic_id_t (*dynamic_id_function)(void*);
22 
23 BOOST_PYTHON_DECL void register_dynamic_id_aux(
24     class_id static_id, dynamic_id_function get_dynamic_id);
25 
26 BOOST_PYTHON_DECL void add_cast(
27     class_id src_t, class_id dst_t, void* (*cast)(void*), bool is_downcast);
28 
29 //
30 // a generator with an execute() function which, given a source type
31 // and a pointer to an object of that type, returns its most-derived
32 // /reachable/ type identifier and object pointer.
33 //
34 
35 // first, the case where T has virtual functions
36 template <class T>
37 struct polymorphic_id_generator
38 {
executeboost::python::objects::polymorphic_id_generator39     static dynamic_id_t execute(void* p_)
40     {
41         T* p = static_cast<T*>(p_);
42         return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
43     }
44 };
45 
46 // now, the non-polymorphic case.
47 template <class T>
48 struct non_polymorphic_id_generator
49 {
executeboost::python::objects::non_polymorphic_id_generator50     static dynamic_id_t execute(void* p_)
51     {
52         return std::make_pair(p_, python::type_id<T>());
53     }
54 };
55 
56 // Now the generalized selector
57 template <class T>
58 struct dynamic_id_generator
59   : mpl::if_<
60         boost::python::detail::is_polymorphic<T>
61         , boost::python::objects::polymorphic_id_generator<T>
62         , boost::python::objects::non_polymorphic_id_generator<T>
63     >
64 {};
65 
66 // Register the dynamic id function for T with the type-conversion
67 // system.
68 template <class T>
register_dynamic_id(T * =0)69 void register_dynamic_id(T* = 0)
70 {
71     typedef typename dynamic_id_generator<T>::type generator;
72     register_dynamic_id_aux(
73         python::type_id<T>(), &generator::execute);
74 }
75 
76 //
77 // a generator with an execute() function which, given a void*
78 // pointing to an object of type Source will attempt to convert it to
79 // an object of type Target.
80 //
81 
82 template <class Source, class Target>
83 struct dynamic_cast_generator
84 {
executeboost::python::objects::dynamic_cast_generator85     static void* execute(void* source)
86     {
87         return dynamic_cast<Target*>(
88             static_cast<Source*>(source));
89     }
90 
91 };
92 
93 template <class Source, class Target>
94 struct implicit_cast_generator
95 {
executeboost::python::objects::implicit_cast_generator96     static void* execute(void* source)
97     {
98         Target* result = static_cast<Source*>(source);
99         return result;
100     }
101 };
102 
103 template <class Source, class Target>
104 struct cast_generator
105   : mpl::if_<
106         boost::python::detail::is_base_and_derived<Target,Source>
107       , implicit_cast_generator<Source,Target>
108       , dynamic_cast_generator<Source,Target>
109     >
110 {
111 };
112 
113 template <class Source, class Target>
114 inline void register_conversion(
115     bool is_downcast = ::boost::is_base_and_derived<Source,Target>::value
116     // These parameters shouldn't be used; they're an MSVC bug workaround
117     , Source* = 0, Target* = 0)
118 {
119     typedef typename cast_generator<Source,Target>::type generator;
120 
121     add_cast(
122         python::type_id<Source>()
123       , python::type_id<Target>()
124       , &generator::execute
125       , is_downcast
126     );
127 }
128 
129 }}} // namespace boost::python::object
130 
131 #endif // INHERITANCE_DWA200216_HPP
132