os/ossrv/ossrv_pub/boost_apis/boost/python/object/inheritance.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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
     7 
     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>
    14 
    15 namespace boost { namespace python { namespace objects {
    16 
    17 typedef type_info class_id;
    18 using python::type_id;
    19 
    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*);
    23 
    24 BOOST_PYTHON_DECL void register_dynamic_id_aux(
    25     class_id static_id, dynamic_id_function get_dynamic_id);
    26 
    27 BOOST_PYTHON_DECL void add_cast(
    28     class_id src_t, class_id dst_t, void* (*cast)(void*), bool is_downcast);
    29 
    30 //
    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.
    34 //
    35 
    36 // first, the case where T has virtual functions
    37 template <class T>
    38 struct polymorphic_id_generator
    39 {
    40     static dynamic_id_t execute(void* p_)
    41     {
    42         T* p = static_cast<T*>(p_);
    43         return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
    44     }
    45 };
    46 
    47 // now, the non-polymorphic case.
    48 template <class T>
    49 struct non_polymorphic_id_generator
    50 {
    51     static dynamic_id_t execute(void* p_)
    52     {
    53         return std::make_pair(p_, python::type_id<T>());
    54     }
    55 };
    56 
    57 // Now the generalized selector
    58 template <class T>
    59 struct dynamic_id_generator
    60   : mpl::if_<
    61         boost::is_polymorphic<T>
    62         , boost::python::objects::polymorphic_id_generator<T>
    63         , boost::python::objects::non_polymorphic_id_generator<T>
    64     >
    65 {};
    66 
    67 // Register the dynamic id function for T with the type-conversion
    68 // system.
    69 template <class T>
    70 void register_dynamic_id(T* = 0)
    71 {
    72     typedef typename dynamic_id_generator<T>::type generator;
    73     register_dynamic_id_aux(
    74         python::type_id<T>(), &generator::execute);
    75 }
    76 
    77 //
    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.
    81 //
    82 
    83 template <class Source, class Target>
    84 struct dynamic_cast_generator
    85 {
    86     static void* execute(void* source)
    87     {
    88         return dynamic_cast<Target*>(
    89             static_cast<Source*>(source));
    90     }
    91         
    92 };
    93 
    94 template <class Source, class Target>
    95 struct implicit_cast_generator
    96 {
    97     static void* execute(void* source)
    98     {
    99         Target* result = static_cast<Source*>(source);
   100         return result;
   101     }
   102 };
   103 
   104 template <class Source, class Target>
   105 struct cast_generator
   106   : mpl::if_<
   107         is_base_and_derived<Target,Source>
   108       , implicit_cast_generator<Source,Target>
   109       , dynamic_cast_generator<Source,Target>
   110     >
   111 {
   112 };
   113 
   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)
   119 {
   120     typedef typename cast_generator<Source,Target>::type generator;
   121 
   122     add_cast(
   123         python::type_id<Source>()
   124       , python::type_id<Target>()
   125       , &generator::execute
   126       , is_downcast
   127     );
   128 }
   129 
   130 }}} // namespace boost::python::object
   131 
   132 #endif // INHERITANCE_DWA200216_HPP