1 | #ifndef BOOST_SERIALIZATION_BASE_OBJECT_HPP |
2 | #define BOOST_SERIALIZATION_BASE_OBJECT_HPP |
3 | |
4 | // MS compatible compilers support #pragma once |
5 | #if defined(_MSC_VER) |
6 | # pragma once |
7 | #endif |
8 | |
9 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
10 | // base_object.hpp: |
11 | |
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) |
16 | |
17 | // See http://www.boost.org for updates, documentation, and revision history. |
18 | |
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 |
22 | |
23 | #include <boost/config.hpp> |
24 | #include <boost/detail/workaround.hpp> |
25 | |
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> |
30 | |
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> |
35 | |
36 | #include <boost/static_assert.hpp> |
37 | #include <boost/serialization/access.hpp> |
38 | #include <boost/serialization/force_include.hpp> |
39 | #include <boost/serialization/void_cast_fwd.hpp> |
40 | |
41 | namespace boost { |
42 | namespace serialization { |
43 | |
44 | namespace detail |
45 | { |
46 | // get the base type for a given derived type |
47 | // preserving the const-ness |
48 | template<class B, class D> |
49 | struct base_cast |
50 | { |
51 | typedef typename |
52 | mpl::if_< |
53 | is_const<D>, |
54 | const B, |
55 | B |
56 | >::type type; |
57 | BOOST_STATIC_ASSERT(is_const<type>::value == is_const<D>::value); |
58 | }; |
59 | |
60 | // only register void casts if the types are polymorphic |
61 | template<class Base, class Derived> |
62 | struct base_register |
63 | { |
64 | struct polymorphic { |
65 | static void const * invoke(){ |
66 | Base const * const b = 0; |
67 | Derived const * const d = 0; |
68 | return & void_cast_register(d, b); |
69 | } |
70 | }; |
71 | struct non_polymorphic { |
72 | static void const * invoke(){ |
73 | return 0; |
74 | } |
75 | }; |
76 | static void const * invoke(){ |
77 | typedef typename mpl::eval_if< |
78 | is_polymorphic<Base>, |
79 | mpl::identity<polymorphic>, |
80 | mpl::identity<non_polymorphic> |
81 | >::type type; |
82 | return type::invoke(); |
83 | } |
84 | }; |
85 | |
86 | } // namespace detail |
87 | template<class Base, class Derived> |
88 | typename detail::base_cast<Base, Derived>::type & |
89 | base_object(Derived &d) |
90 | { |
91 | BOOST_STATIC_ASSERT(( is_base_and_derived<Base,Derived>::value)); |
92 | BOOST_STATIC_ASSERT(! is_pointer<Derived>::value); |
93 | typedef typename detail::base_cast<Base, Derived>::type type; |
94 | detail::base_register<type, Derived>::invoke(); |
95 | return access::cast_reference<type, Derived>(d); |
96 | } |
97 | |
98 | } // namespace serialization |
99 | } // namespace boost |
100 | |
101 | #endif // BOOST_SERIALIZATION_BASE_OBJECT_HPP |
102 | |