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
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>
14 namespace boost { namespace python { namespace objects {
16 typedef type_info class_id;
17 using python::type_id;
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*);
23 BOOST_PYTHON_DECL void register_dynamic_id_aux(
24 class_id static_id, dynamic_id_function get_dynamic_id);
26 BOOST_PYTHON_DECL void add_cast(
27 class_id src_t, class_id dst_t, void* (*cast)(void*), bool is_downcast);
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.
35 // first, the case where T has virtual functions
37 struct polymorphic_id_generator
39 static dynamic_id_t execute(void* p_)
41 T* p = static_cast<T*>(p_);
42 return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
46 // now, the non-polymorphic case.
48 struct non_polymorphic_id_generator
50 static dynamic_id_t execute(void* p_)
52 return std::make_pair(p_, python::type_id<T>());
56 // Now the generalized selector
58 struct dynamic_id_generator
60 boost::python::detail::is_polymorphic<T>
61 , boost::python::objects::polymorphic_id_generator<T>
62 , boost::python::objects::non_polymorphic_id_generator<T>
66 // Register the dynamic id function for T with the type-conversion
69 void register_dynamic_id(T* = 0)
71 typedef typename dynamic_id_generator<T>::type generator;
72 register_dynamic_id_aux(
73 python::type_id<T>(), &generator::execute);
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.
82 template <class Source, class Target>
83 struct dynamic_cast_generator
85 static void* execute(void* source)
87 return dynamic_cast<Target*>(
88 static_cast<Source*>(source));
93 template <class Source, class Target>
94 struct implicit_cast_generator
96 static void* execute(void* source)
98 Target* result = static_cast<Source*>(source);
103 template <class Source, class Target>
104 struct cast_generator
106 boost::python::detail::is_base_and_derived<Target,Source>
107 , implicit_cast_generator<Source,Target>
108 , dynamic_cast_generator<Source,Target>
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)
119 typedef typename cast_generator<Source,Target>::type generator;
122 python::type_id<Source>()
123 , python::type_id<Target>()
124 , &generator::execute
129 }}} // namespace boost::python::object
131 #endif // INHERITANCE_DWA200216_HPP