1 #ifndef BOOST_SERIALIZATION_VOID_CAST_HPP
2 #define BOOST_SERIALIZATION_VOID_CAST_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // void_cast.hpp: interface for run-time casting of void pointers.
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 // gennadiy.rozental@tfn.com
18 // See http://www.boost.org for updates, documentation, and revision history.
20 #include <cstddef> // for ptrdiff_t
21 #include <boost/serialization/config.hpp>
22 #include <boost/serialization/smart_cast.hpp>
23 #include <boost/serialization/singleton.hpp>
24 #include <boost/serialization/force_include.hpp>
25 #include <boost/serialization/type_info_implementation.hpp>
26 #include <boost/type_traits/is_virtual_base_of.hpp>
28 #include <boost/config/abi_prefix.hpp> // must be the last header
31 # pragma warning(push)
32 # pragma warning(disable : 4251 4231 4660 4275)
36 namespace serialization
{
38 class extended_type_info
;
40 // Given a void *, assume that it really points to an instance of one type
41 // and alter it so that it would point to an instance of a related type.
42 // Return the altered pointer. If there exists no sequence of casts that
43 // can transform from_type to to_type, return a NULL.
45 BOOST_SERIALIZATION_DECL(void const *)
47 extended_type_info
const & derived
,
48 extended_type_info
const & base
,
54 extended_type_info
const & derived
,
55 extended_type_info
const & base
,
58 return const_cast<void*>(void_upcast(
61 const_cast<void const *>(t
)
65 BOOST_SERIALIZATION_DECL(void const *)
67 extended_type_info
const & derived
,
68 extended_type_info
const & base
,
74 extended_type_info
const & derived
,
75 extended_type_info
const & base
,
78 return const_cast<void*>(void_downcast(
81 const_cast<void const *>(t
)
85 namespace void_cast_detail
{
87 class BOOST_SERIALIZATION_DECL(BOOST_PP_EMPTY()) void_caster
90 BOOST_SERIALIZATION_DECL(void const *)
91 boost::serialization::void_upcast(
92 extended_type_info
const & derived
,
93 extended_type_info
const & base
,
97 BOOST_SERIALIZATION_DECL(void const *)
98 boost::serialization::void_downcast(
99 extended_type_info
const & derived
,
100 extended_type_info
const & base
,
103 // cw 8.3 requires this!!
104 void_caster
& operator=(void_caster
const &);
106 void recursive_register(bool includes_virtual_base
= false) const;
107 void recursive_unregister() const;
110 const extended_type_info
* m_derived
;
111 const extended_type_info
* m_base
;
112 const std::ptrdiff_t m_difference
;
113 virtual bool is_shortcut() const {
116 // note that void_casters are keyed on value of
117 // addresses to member extended type info records
118 bool operator<(const void_caster
& lhs
) const {
119 if(m_derived
< lhs
.m_derived
)
121 if(m_derived
== lhs
.m_derived
)
122 if(m_base
< lhs
.m_base
)
126 // each derived class must re-implement these;
127 virtual void const * upcast(void const * const t
) const = 0;
128 virtual void const * downcast(void const * const t
) const = 0;
131 extended_type_info
const * derived
,
132 extended_type_info
const * base
,
133 std::ptrdiff_t difference
= 0
137 m_difference(difference
)
139 virtual ~void_caster(){}
142 template <class Derived
, class Base
>
143 class void_caster_primitive
:
146 virtual void const * downcast(void const * const t
) const {
148 boost::serialization::smart_cast
<const Derived
*, const Base
*>(
149 static_cast<const Base
*>(t
)
153 virtual void const * upcast(void const * const t
) const {
155 boost::serialization::smart_cast
<const Base
*, const Derived
*>(
156 static_cast<const Derived
*>(t
)
161 void_caster_primitive();
162 ~void_caster_primitive();
165 template <class Derived
, class Base
>
166 void_caster_primitive
<Derived
, Base
>::void_caster_primitive() :
168 & type_info_implementation
<Derived
>::type::get_const_instance(),
169 & type_info_implementation
<Base
>::type::get_const_instance(),
170 // note:I wanted to display from 0 here, but at least one compiler
171 // treated 0 by not shifting it at all.
172 reinterpret_cast<std::ptrdiff_t>(
173 static_cast<Derived
*>(
174 reinterpret_cast<Base
*>(1)
179 recursive_register();
182 template <class Derived
, class Base
>
183 void_caster_primitive
<Derived
, Base
>::~void_caster_primitive(){
184 recursive_unregister();
187 template <class Derived
, class Base
>
188 class void_caster_virtual_base
:
192 virtual void const * downcast(void const * const t
) const {
194 dynamic_cast<const Derived
*>(
195 static_cast<const Base
*>(t
)
199 virtual void const * upcast(void const * const t
) const {
201 dynamic_cast<const Base
*>(
202 static_cast<const Derived
*>(t
)
206 void_caster_virtual_base();
207 ~void_caster_virtual_base();
210 template <class Derived
, class Base
>
211 void_caster_virtual_base
<Derived
,Base
>::void_caster_virtual_base() :
213 & type_info_implementation
<Derived
>::type::get_const_instance(),
214 & type_info_implementation
<Base
>::type::get_const_instance()
217 recursive_register(true);
220 template <class Derived
, class Base
>
221 void_caster_virtual_base
<Derived
,Base
>::~void_caster_virtual_base(){
222 recursive_unregister();
225 } // void_cast_detail
227 // Register a base/derived pair. This indicates that it is possible
228 // to upcast a void pointer from Derived to Base and downcast a
229 // void pointer from Base to Derived. Note bogus arguments to workaround
231 template<class Derived
, class Base
>
233 inline const void_cast_detail::void_caster
& void_cast_register(
234 const Derived
* dnull
,
238 template<class Derived
, class Base
>
240 inline const void_cast_detail::void_caster
& void_cast_register(
241 Derived
const * /* dnull = NULL */,
242 Base
const * /* bnull = NULL */
245 BOOST_DEDUCED_TYPENAME
mpl::eval_if
<boost::is_virtual_base_of
<Base
,Derived
>,
247 void_cast_detail::void_caster_virtual_base
<Derived
, Base
>
251 void_cast_detail::void_caster_primitive
<Derived
, Base
>
254 return singleton
<typex
>::get_const_instance();
257 } // namespace serialization
261 # pragma warning(pop)
264 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
266 #endif // BOOST_SERIALIZATION_VOID_CAST_HPP