fix doc example typo
[boost.git] / boost / serialization / base_object.hpp
blob5d81e90d88d8fea48999711d63297db7383c8ab7
1 #ifndef BOOST_SERIALIZATION_BASE_OBJECT_HPP
2 #define BOOST_SERIALIZATION_BASE_OBJECT_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // base_object.hpp:
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 // See http://www.boost.org for updates, documentation, and revision history.
19 // if no archive headers have been included this is a no op
20 // this is to permit BOOST_EXPORT etc to be included in a
21 // file declaration header
23 #include <boost/config.hpp>
24 #include <boost/detail/workaround.hpp>
26 #include <boost/mpl/eval_if.hpp>
27 #include <boost/mpl/int.hpp>
28 #include <boost/mpl/bool.hpp>
29 #include <boost/mpl/identity.hpp>
31 #include <boost/type_traits/is_base_and_derived.hpp>
32 #include <boost/type_traits/is_pointer.hpp>
33 #include <boost/type_traits/is_const.hpp>
34 #include <boost/type_traits/is_polymorphic.hpp>
36 #include <boost/static_assert.hpp>
37 #include <boost/serialization/force_include.hpp>
38 #include <boost/serialization/void_cast_fwd.hpp>
40 namespace boost {
41 namespace serialization {
43 namespace detail
45 // get the base type for a given derived type
46 // preserving the const-ness
47 template<class B, class D>
48 struct base_cast
50 typedef BOOST_DEDUCED_TYPENAME
51 mpl::if_<
52 is_const<D>,
53 const B,
55 >::type type;
56 BOOST_STATIC_ASSERT(is_const<type>::value == is_const<D>::value);
59 // only register void casts if the types are polymorphic
60 template<class Base, class Derived>
61 struct base_register
63 struct polymorphic {
64 static void const * invoke(){
65 return &void_cast_register((Derived const*)0, (Base const*)0);
68 struct non_polymorphic {
69 static void const * invoke(){
70 return 0;
73 static void const * invoke(){
74 typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
75 is_polymorphic<Base>,
76 mpl::identity<polymorphic>,
77 mpl::identity<non_polymorphic>
78 >::type type;
79 return type::invoke();
83 } // namespace detail
85 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x560))
86 template<class Base, class Derived>
87 const Base &
88 base_object(const Derived & d)
90 BOOST_STATIC_ASSERT(! is_pointer<Derived>::value);
91 detail::base_register<Base, Derived>::invoke();
92 return static_cast<const Base &>(d);
94 #else
95 template<class Base, class Derived>
96 BOOST_DEDUCED_TYPENAME detail::base_cast<Base, Derived>::type &
97 base_object(Derived &d)
99 BOOST_STATIC_ASSERT(( is_base_and_derived<Base,Derived>::value));
100 BOOST_STATIC_ASSERT(! is_pointer<Derived>::value);
101 typedef BOOST_DEDUCED_TYPENAME detail::base_cast<Base, Derived>::type type;
102 detail::base_register<type, Derived>::invoke();
103 return static_cast<type &>(d);
105 #endif
107 } // namespace serialization
108 } // namespace boost
110 #endif // BOOST_SERIALIZATION_BASE_OBJECT_HPP