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:I wanted to displace from 0 here, but at least one compiler
185 // treated 0 by not shifting it at all.
186 reinterpret_cast<std::ptrdiff_t>(
187 static_cast<Derived *>(
188 reinterpret_cast<Base *>(8)
193 recursive_register();
196 template <class Derived, class Base>
197 void_caster_primitive<Derived, Base>::~void_caster_primitive(){
198 recursive_unregister();
201 template <class Derived, class Base>
202 class BOOST_SYMBOL_VISIBLE void_caster_virtual_base :
205 virtual bool has_virtual_base() const {
209 virtual void const * downcast(void const * const t) const {
211 dynamic_cast<const Derived *>(
212 static_cast<const Base *>(t)
216 virtual void const * upcast(void const * const t) const {
218 dynamic_cast<const Base *>(
219 static_cast<const Derived *>(t)
223 void_caster_virtual_base();
224 virtual ~void_caster_virtual_base();
231 template <class Derived, class Base>
232 void_caster_virtual_base<Derived,Base>::void_caster_virtual_base() :
234 & (type_info_implementation<Derived>::type::get_const_instance()),
235 & (type_info_implementation<Base>::type::get_const_instance())
238 recursive_register(true);
241 template <class Derived, class Base>
242 void_caster_virtual_base<Derived,Base>::~void_caster_virtual_base(){
243 recursive_unregister();
246 template <class Derived, class Base>
247 struct BOOST_SYMBOL_VISIBLE void_caster_base :
251 typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
253 void_cast_detail::void_caster_virtual_base<Derived, Base>
257 void_cast_detail::void_caster_primitive<Derived, Base>
262 } // void_cast_detail
264 template<class Derived, class Base>
266 inline const void_cast_detail::void_caster & void_cast_register(
267 Derived const * /* dnull = NULL */,
268 Base const * /* bnull = NULL */
271 typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
273 void_cast_detail::void_caster_virtual_base<Derived, Base>
277 void_cast_detail::void_caster_primitive<Derived, Base>
280 return singleton<typex>::get_const_instance();
283 template<class Derived, class Base>
284 class BOOST_SYMBOL_VISIBLE void_caster :
285 public void_cast_detail::void_caster_base<Derived, Base>::type
289 } // namespace serialization
293 # pragma warning(pop)
296 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
298 #endif // BOOST_SERIALIZATION_VOID_CAST_HPP