os/ossrv/ossrv_pub/boost_apis/boost/serialization/base_object.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
#ifndef BOOST_SERIALIZATION_BASE_OBJECT_HPP
sl@0
     2
#define BOOST_SERIALIZATION_BASE_OBJECT_HPP
sl@0
     3
sl@0
     4
// MS compatible compilers support #pragma once
sl@0
     5
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
sl@0
     6
# pragma once
sl@0
     7
#endif
sl@0
     8
sl@0
     9
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
sl@0
    10
// base_object.hpp:
sl@0
    11
sl@0
    12
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
sl@0
    13
// Use, modification and distribution is subject to the Boost Software
sl@0
    14
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
    15
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
    16
sl@0
    17
//  See http://www.boost.org for updates, documentation, and revision history.
sl@0
    18
sl@0
    19
// if no archive headers have been included this is a no op
sl@0
    20
// this is to permit BOOST_EXPORT etc to be included in a 
sl@0
    21
// file declaration header
sl@0
    22
sl@0
    23
#include <boost/config.hpp>
sl@0
    24
#include <boost/detail/workaround.hpp>
sl@0
    25
sl@0
    26
#include <boost/mpl/eval_if.hpp>
sl@0
    27
#include <boost/mpl/int.hpp>
sl@0
    28
#include <boost/type_traits/is_base_and_derived.hpp>
sl@0
    29
#include <boost/type_traits/is_pointer.hpp>
sl@0
    30
#include <boost/type_traits/is_const.hpp>
sl@0
    31
sl@0
    32
#include <boost/static_assert.hpp>
sl@0
    33
#include <boost/serialization/type_info_implementation.hpp>
sl@0
    34
#include <boost/serialization/force_include.hpp>
sl@0
    35
#include <boost/serialization/void_cast_fwd.hpp>
sl@0
    36
sl@0
    37
namespace boost {
sl@0
    38
namespace serialization {
sl@0
    39
sl@0
    40
namespace detail {
sl@0
    41
    // metrowerks CodeWarrior
sl@0
    42
    #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) 
sl@0
    43
        // only register void casts if the types are polymorphic
sl@0
    44
        template<class Base, class Derived>
sl@0
    45
        struct base_register{
sl@0
    46
            struct nothing {
sl@0
    47
                static const void_cast_detail::void_caster & invoke(){
sl@0
    48
                    return static_cast<const void_cast_detail::void_caster &>(
sl@0
    49
                        * static_cast<const void_cast_detail::void_caster *>(NULL)
sl@0
    50
                    );
sl@0
    51
                }
sl@0
    52
            };
sl@0
    53
sl@0
    54
            // hold a reference to the void_cast_register and void_caster in the hope of 
sl@0
    55
            // ensuring code instantiation for some compilers with over-zealous link time 
sl@0
    56
            // optimiser. The compiler that demanded this was CW
sl@0
    57
            struct reg{
sl@0
    58
                typedef const void_cast_detail::void_caster & (* t_vcr)(
sl@0
    59
                    const Derived *,
sl@0
    60
                    const Base *
sl@0
    61
                );
sl@0
    62
                t_vcr m_vcr;
sl@0
    63
                static const void_cast_detail::void_caster & invoke(){
sl@0
    64
                    return  void_cast_register<const Derived, const Base>(
sl@0
    65
                        static_cast<const Derived *>(NULL),
sl@0
    66
                        static_cast<const Base *>(NULL)
sl@0
    67
                    );
sl@0
    68
                }
sl@0
    69
                reg() :
sl@0
    70
                    m_vcr(static_cast<t_vcr>(void_cast_register))
sl@0
    71
                {
sl@0
    72
                }
sl@0
    73
            } m_reg;
sl@0
    74
sl@0
    75
            static const void_cast_detail::void_caster & invoke(){
sl@0
    76
                typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
sl@0
    77
                    BOOST_DEDUCED_TYPENAME type_info_implementation<Base>::type::is_polymorphic,
sl@0
    78
                    mpl::identity<reg>,
sl@0
    79
                    mpl::identity<nothing>
sl@0
    80
                >::type typex;
sl@0
    81
                return typex::invoke();
sl@0
    82
            }
sl@0
    83
sl@0
    84
            const void_cast_detail::void_caster & m_vc;
sl@0
    85
            Derived & m_d;
sl@0
    86
sl@0
    87
            base_register(Derived & d) :
sl@0
    88
                m_vc(invoke()),
sl@0
    89
                m_d(d)
sl@0
    90
            {}
sl@0
    91
            Base & get_base() const {
sl@0
    92
                return m_d;
sl@0
    93
            }
sl@0
    94
        };
sl@0
    95
    #else
sl@0
    96
        // only register void casts if the types are polymorphic
sl@0
    97
        template<class Base, class Derived>
sl@0
    98
        struct base_register{
sl@0
    99
            struct nothing {
sl@0
   100
                static void invoke(){}
sl@0
   101
            };
sl@0
   102
            struct reg{
sl@0
   103
                static void invoke(){
sl@0
   104
                    void_cast_register<const Derived, const Base>(
sl@0
   105
                        static_cast<const Derived *>(NULL),
sl@0
   106
                        static_cast<const Base *>(NULL)
sl@0
   107
                    );
sl@0
   108
                }
sl@0
   109
            };
sl@0
   110
            static void invoke(){
sl@0
   111
                typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
sl@0
   112
                    BOOST_DEDUCED_TYPENAME type_info_implementation<Base>::type::is_polymorphic,
sl@0
   113
                    mpl::identity<reg>,
sl@0
   114
                    mpl::identity<nothing>
sl@0
   115
                >::type typex;
sl@0
   116
                typex::invoke();
sl@0
   117
            }
sl@0
   118
        };
sl@0
   119
    #endif
sl@0
   120
    // get the base type for a given derived type
sl@0
   121
    // preserving the const-ness
sl@0
   122
    template<class B, class D>
sl@0
   123
    struct base_cast
sl@0
   124
    {
sl@0
   125
        typedef BOOST_DEDUCED_TYPENAME
sl@0
   126
            mpl::if_<
sl@0
   127
                is_const<D>,
sl@0
   128
                const B,
sl@0
   129
                B
sl@0
   130
        >::type type;
sl@0
   131
        BOOST_STATIC_ASSERT(is_const<type>::value == is_const<D>::value);
sl@0
   132
    };
sl@0
   133
} // namespace detail
sl@0
   134
sl@0
   135
// metrowerks CodeWarrior
sl@0
   136
#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) 
sl@0
   137
template<class Base, class Derived>
sl@0
   138
BOOST_DEDUCED_TYPENAME detail::base_cast<Base, Derived>::type & 
sl@0
   139
base_object(Derived &d)
sl@0
   140
{
sl@0
   141
    BOOST_STATIC_ASSERT(( is_base_and_derived<Base,Derived>::value));
sl@0
   142
    BOOST_STATIC_ASSERT(! is_pointer<Derived>::value);
sl@0
   143
    typedef BOOST_DEDUCED_TYPENAME detail::base_cast<Base, Derived>::type type;
sl@0
   144
    return detail::base_register<type, Derived>(d).get_base();
sl@0
   145
}
sl@0
   146
// BORLAND
sl@0
   147
#elif BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x560))
sl@0
   148
template<class Base, class Derived>
sl@0
   149
const Base & 
sl@0
   150
base_object(const Derived & d)
sl@0
   151
{
sl@0
   152
    BOOST_STATIC_ASSERT(! is_pointer<Derived>::value);
sl@0
   153
    detail::base_register<Base, Derived>::invoke();
sl@0
   154
    return static_cast<const Base &>(d);
sl@0
   155
}
sl@0
   156
#else
sl@0
   157
template<class Base, class Derived>
sl@0
   158
BOOST_DEDUCED_TYPENAME detail::base_cast<Base, Derived>::type & 
sl@0
   159
base_object(Derived &d)
sl@0
   160
{
sl@0
   161
    BOOST_STATIC_ASSERT(( is_base_and_derived<Base,Derived>::value));
sl@0
   162
    BOOST_STATIC_ASSERT(! is_pointer<Derived>::value);
sl@0
   163
    typedef BOOST_DEDUCED_TYPENAME detail::base_cast<Base, Derived>::type type;
sl@0
   164
    detail::base_register<type, Derived>::invoke();
sl@0
   165
    return static_cast<type &>(d);
sl@0
   166
}
sl@0
   167
#endif
sl@0
   168
sl@0
   169
} // namespace serialization
sl@0
   170
} // namespace boost
sl@0
   171
sl@0
   172
#endif // BOOST_SERIALIZATION_BASE_OBJECT_HPP