os/ossrv/ossrv_pub/boost_apis/boost/python/object/inheritance.hpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/python/object/inheritance.hpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,132 @@
     1.4 +// Copyright David Abrahams 2002.
     1.5 +// Distributed under the Boost Software License, Version 1.0. (See
     1.6 +// accompanying file LICENSE_1_0.txt or copy at
     1.7 +// http://www.boost.org/LICENSE_1_0.txt)
     1.8 +#ifndef INHERITANCE_DWA200216_HPP
     1.9 +# define INHERITANCE_DWA200216_HPP
    1.10 +
    1.11 +# include <boost/python/type_id.hpp>
    1.12 +# include <boost/shared_ptr.hpp>
    1.13 +# include <boost/mpl/if.hpp>
    1.14 +# include <boost/type_traits/is_polymorphic.hpp>
    1.15 +# include <boost/type_traits/is_base_and_derived.hpp>
    1.16 +# include <boost/detail/workaround.hpp>
    1.17 +
    1.18 +namespace boost { namespace python { namespace objects {
    1.19 +
    1.20 +typedef type_info class_id;
    1.21 +using python::type_id;
    1.22 +
    1.23 +// Types used to get address and id of most derived type
    1.24 +typedef std::pair<void*,class_id> dynamic_id_t;
    1.25 +typedef dynamic_id_t (*dynamic_id_function)(void*);
    1.26 +
    1.27 +BOOST_PYTHON_DECL void register_dynamic_id_aux(
    1.28 +    class_id static_id, dynamic_id_function get_dynamic_id);
    1.29 +
    1.30 +BOOST_PYTHON_DECL void add_cast(
    1.31 +    class_id src_t, class_id dst_t, void* (*cast)(void*), bool is_downcast);
    1.32 +
    1.33 +//
    1.34 +// a generator with an execute() function which, given a source type
    1.35 +// and a pointer to an object of that type, returns its most-derived
    1.36 +// /reachable/ type identifier and object pointer.
    1.37 +//
    1.38 +
    1.39 +// first, the case where T has virtual functions
    1.40 +template <class T>
    1.41 +struct polymorphic_id_generator
    1.42 +{
    1.43 +    static dynamic_id_t execute(void* p_)
    1.44 +    {
    1.45 +        T* p = static_cast<T*>(p_);
    1.46 +        return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
    1.47 +    }
    1.48 +};
    1.49 +
    1.50 +// now, the non-polymorphic case.
    1.51 +template <class T>
    1.52 +struct non_polymorphic_id_generator
    1.53 +{
    1.54 +    static dynamic_id_t execute(void* p_)
    1.55 +    {
    1.56 +        return std::make_pair(p_, python::type_id<T>());
    1.57 +    }
    1.58 +};
    1.59 +
    1.60 +// Now the generalized selector
    1.61 +template <class T>
    1.62 +struct dynamic_id_generator
    1.63 +  : mpl::if_<
    1.64 +        boost::is_polymorphic<T>
    1.65 +        , boost::python::objects::polymorphic_id_generator<T>
    1.66 +        , boost::python::objects::non_polymorphic_id_generator<T>
    1.67 +    >
    1.68 +{};
    1.69 +
    1.70 +// Register the dynamic id function for T with the type-conversion
    1.71 +// system.
    1.72 +template <class T>
    1.73 +void register_dynamic_id(T* = 0)
    1.74 +{
    1.75 +    typedef typename dynamic_id_generator<T>::type generator;
    1.76 +    register_dynamic_id_aux(
    1.77 +        python::type_id<T>(), &generator::execute);
    1.78 +}
    1.79 +
    1.80 +//
    1.81 +// a generator with an execute() function which, given a void*
    1.82 +// pointing to an object of type Source will attempt to convert it to
    1.83 +// an object of type Target.
    1.84 +//
    1.85 +
    1.86 +template <class Source, class Target>
    1.87 +struct dynamic_cast_generator
    1.88 +{
    1.89 +    static void* execute(void* source)
    1.90 +    {
    1.91 +        return dynamic_cast<Target*>(
    1.92 +            static_cast<Source*>(source));
    1.93 +    }
    1.94 +        
    1.95 +};
    1.96 +
    1.97 +template <class Source, class Target>
    1.98 +struct implicit_cast_generator
    1.99 +{
   1.100 +    static void* execute(void* source)
   1.101 +    {
   1.102 +        Target* result = static_cast<Source*>(source);
   1.103 +        return result;
   1.104 +    }
   1.105 +};
   1.106 +
   1.107 +template <class Source, class Target>
   1.108 +struct cast_generator
   1.109 +  : mpl::if_<
   1.110 +        is_base_and_derived<Target,Source>
   1.111 +      , implicit_cast_generator<Source,Target>
   1.112 +      , dynamic_cast_generator<Source,Target>
   1.113 +    >
   1.114 +{
   1.115 +};
   1.116 +
   1.117 +template <class Source, class Target>
   1.118 +inline void register_conversion(
   1.119 +    bool is_downcast = ::boost::is_base_and_derived<Source,Target>::value
   1.120 +    // These parameters shouldn't be used; they're an MSVC bug workaround
   1.121 +    , Source* = 0, Target* = 0)
   1.122 +{
   1.123 +    typedef typename cast_generator<Source,Target>::type generator;
   1.124 +
   1.125 +    add_cast(
   1.126 +        python::type_id<Source>()
   1.127 +      , python::type_id<Target>()
   1.128 +      , &generator::execute
   1.129 +      , is_downcast
   1.130 +    );
   1.131 +}
   1.132 +
   1.133 +}}} // namespace boost::python::object
   1.134 +
   1.135 +#endif // INHERITANCE_DWA200216_HPP