Update contrib.
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/type_traits/is_polymorphic.hpp>
12 # include <boost/type_traits/is_base_and_derived.hpp>
13 # include <boost/detail/workaround.hpp>
15 namespace boost { namespace python { namespace objects {
17 typedef type_info class_id;
18 using python::type_id;
20 // Types used to get address and id of most derived type
21 typedef std::pair<void*,class_id> dynamic_id_t;
22 typedef dynamic_id_t (*dynamic_id_function)(void*);
24 BOOST_PYTHON_DECL void register_dynamic_id_aux(
25 class_id static_id, dynamic_id_function get_dynamic_id);
27 BOOST_PYTHON_DECL void add_cast(
28 class_id src_t, class_id dst_t, void* (*cast)(void*), bool is_downcast);
31 // a generator with an execute() function which, given a source type
32 // and a pointer to an object of that type, returns its most-derived
33 // /reachable/ type identifier and object pointer.
36 // first, the case where T has virtual functions
38 struct polymorphic_id_generator
40 static dynamic_id_t execute(void* p_)
42 T* p = static_cast<T*>(p_);
43 return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
47 // now, the non-polymorphic case.
49 struct non_polymorphic_id_generator
51 static dynamic_id_t execute(void* p_)
53 return std::make_pair(p_, python::type_id<T>());
57 // Now the generalized selector
59 struct dynamic_id_generator
61 boost::is_polymorphic<T>
62 , boost::python::objects::polymorphic_id_generator<T>
63 , boost::python::objects::non_polymorphic_id_generator<T>
67 // Register the dynamic id function for T with the type-conversion
70 void register_dynamic_id(T* = 0)
72 typedef typename dynamic_id_generator<T>::type generator;
73 register_dynamic_id_aux(
74 python::type_id<T>(), &generator::execute);
78 // a generator with an execute() function which, given a void*
79 // pointing to an object of type Source will attempt to convert it to
80 // an object of type Target.
83 template <class Source, class Target>
84 struct dynamic_cast_generator
86 static void* execute(void* source)
88 return dynamic_cast<Target*>(
89 static_cast<Source*>(source));
94 template <class Source, class Target>
95 struct implicit_cast_generator
97 static void* execute(void* source)
99 Target* result = static_cast<Source*>(source);
104 template <class Source, class Target>
105 struct cast_generator
107 is_base_and_derived<Target,Source>
108 , implicit_cast_generator<Source,Target>
109 , dynamic_cast_generator<Source,Target>
114 template <class Source, class Target>
115 inline void register_conversion(
116 bool is_downcast = ::boost::is_base_and_derived<Source,Target>::value
117 // These parameters shouldn't be used; they're an MSVC bug workaround
118 , Source* = 0, Target* = 0)
120 typedef typename cast_generator<Source,Target>::type generator;
123 python::type_id<Source>()
124 , python::type_id<Target>()
125 , &generator::execute
130 }}} // namespace boost::python::object
132 #endif // INHERITANCE_DWA200216_HPP