1 #ifndef BOOST_SERIALIZATION_VOID_CAST_HPP
2 #define BOOST_SERIALIZATION_VOID_CAST_HPP
4 // MS compatible compilers support #pragma once
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-2009 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/config.hpp>
22 #include <boost/noncopyable.hpp>
24 #include <boost/serialization/smart_cast.hpp>
25 #include <boost/serialization/singleton.hpp>
26 #include <boost/serialization/force_include.hpp>
27 #include <boost/serialization/type_info_implementation.hpp>
28 #include <boost/serialization/extended_type_info.hpp>
29 #include <boost/type_traits/is_virtual_base_of.hpp>
30 #include <boost/serialization/void_cast_fwd.hpp>
32 #include <boost/serialization/config.hpp>
33 #include <boost/config/abi_prefix.hpp> // must be the last header
36 # pragma warning(push)
37 # pragma warning(disable : 4251 4231 4660 4275)
41 namespace serialization {
43 class extended_type_info;
45 // Given a void *, assume that it really points to an instance of one type
46 // and alter it so that it would point to an instance of a related type.
47 // Return the altered pointer. If there exists no sequence of casts that
48 // can transform from_type to to_type, return a NULL.
50 BOOST_SERIALIZATION_DECL void const *
52 extended_type_info const & derived,
53 extended_type_info const & base,
59 extended_type_info const & derived,
60 extended_type_info const & base,
63 return const_cast<void*>(void_upcast(
66 const_cast<void const *>(t)
70 BOOST_SERIALIZATION_DECL void const *
72 extended_type_info const & derived,
73 extended_type_info const & base,
79 extended_type_info const & derived,
80 extended_type_info const & base,
83 return const_cast<void*>(void_downcast(
86 const_cast<void const *>(t)
90 namespace void_cast_detail {
92 class BOOST_SYMBOL_VISIBLE void_caster :
93 private boost::noncopyable
96 BOOST_SERIALIZATION_DECL void const *
97 boost::serialization::void_upcast(
98 extended_type_info const & derived,
99 extended_type_info const & base,
103 BOOST_SERIALIZATION_DECL void const *
104 boost::serialization::void_downcast(
105 extended_type_info const & derived,
106 extended_type_info const & base,
110 BOOST_SERIALIZATION_DECL void recursive_register(bool includes_virtual_base = false) const;
111 BOOST_SERIALIZATION_DECL void recursive_unregister() const;
112 virtual bool has_virtual_base() const = 0;
115 const extended_type_info * m_derived;
116 const extended_type_info * m_base;
117 /*const*/ std::ptrdiff_t m_difference;
118 void_caster const * const m_parent;
120 // note that void_casters are keyed on value of
121 // member extended type info records - NOT their
122 // addresses. This is necessary in order for the
123 // void cast operations to work across dll and exe
125 bool operator<(const void_caster & rhs) const;
127 const void_caster & operator*(){
130 // each derived class must re-implement these;
131 virtual void const * upcast(void const * const t) const = 0;
132 virtual void const * downcast(void const * const t) const = 0;
135 extended_type_info const * derived,
136 extended_type_info const * base,
137 std::ptrdiff_t difference = 0,
138 void_caster const * const parent = 0
142 m_difference(difference),
145 virtual ~void_caster(){}
149 # pragma warning(push)
150 # pragma warning(disable : 4251 4231 4660 4275 4511 4512)
153 template <class Derived, class Base>
154 class BOOST_SYMBOL_VISIBLE void_caster_primitive :
157 virtual void const * downcast(void const * const t) const {
159 boost::serialization::smart_cast<const Derived *, const Base *>(
160 static_cast<const Base *>(t)
164 virtual void const * upcast(void const * const t) const {
166 boost::serialization::smart_cast<const Base *, const Derived *>(
167 static_cast<const Derived *>(t)
171 virtual bool has_virtual_base() const {
175 void_caster_primitive();
176 virtual ~void_caster_primitive();
179 template <class Derived, class Base>
180 void_caster_primitive<Derived, Base>::void_caster_primitive() :
182 & type_info_implementation<Derived>::type::get_const_instance(),
183 & type_info_implementation<Base>::type::get_const_instance(),
184 /* note about displacement:
185 * displace 0: at least one compiler treated 0 by not shifting it at all
186 * displace by small value (8): caused ICE on certain mingw gcc versions */
187 reinterpret_cast<std::ptrdiff_t>(
188 static_cast<Derived *>(
189 reinterpret_cast<Base *>(1 << 20)
194 recursive_register();
197 template <class Derived, class Base>
198 void_caster_primitive<Derived, Base>::~void_caster_primitive(){
199 recursive_unregister();
202 template <class Derived, class Base>
203 class BOOST_SYMBOL_VISIBLE void_caster_virtual_base :
206 virtual bool has_virtual_base() const {
210 virtual void const * downcast(void const * const t) const {
212 dynamic_cast<const Derived *>(
213 static_cast<const Base *>(t)
217 virtual void const * upcast(void const * const t) const {
219 dynamic_cast<const Base *>(
220 static_cast<const Derived *>(t)
224 void_caster_virtual_base();
225 virtual ~void_caster_virtual_base();
232 template <class Derived, class Base>
233 void_caster_virtual_base<Derived,Base>::void_caster_virtual_base() :
235 & (type_info_implementation<Derived>::type::get_const_instance()),
236 & (type_info_implementation<Base>::type::get_const_instance())
239 recursive_register(true);
242 template <class Derived, class Base>
243 void_caster_virtual_base<Derived,Base>::~void_caster_virtual_base(){
244 recursive_unregister();
247 template <class Derived, class Base>
248 struct BOOST_SYMBOL_VISIBLE void_caster_base :
252 typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
254 void_cast_detail::void_caster_virtual_base<Derived, Base>
258 void_cast_detail::void_caster_primitive<Derived, Base>
263 } // void_cast_detail
265 template<class Derived, class Base>
267 inline const void_cast_detail::void_caster & void_cast_register(
268 Derived const * /* dnull = NULL */,
269 Base const * /* bnull = NULL */
272 typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
274 void_cast_detail::void_caster_virtual_base<Derived, Base>
278 void_cast_detail::void_caster_primitive<Derived, Base>
281 return singleton<typex>::get_const_instance();
284 template<class Derived, class Base>
285 class BOOST_SYMBOL_VISIBLE void_caster :
286 public void_cast_detail::void_caster_base<Derived, Base>::type
290 } // namespace serialization
294 # pragma warning(pop)
297 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
299 #endif // BOOST_SERIALIZATION_VOID_CAST_HPP