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/type_traits/aligned_storage.hpp>
31 #include <boost/serialization/void_cast_fwd.hpp>
33 #include <boost/serialization/config.hpp>
34 #include <boost/config/abi_prefix.hpp> // must be the last header
37 # pragma warning(push)
38 # pragma warning(disable : 4251 4231 4660 4275)
42 namespace serialization {
44 class extended_type_info;
46 // Given a void *, assume that it really points to an instance of one type
47 // and alter it so that it would point to an instance of a related type.
48 // Return the altered pointer. If there exists no sequence of casts that
49 // can transform from_type to to_type, return a NULL.
51 BOOST_SERIALIZATION_DECL void const *
53 extended_type_info const & derived,
54 extended_type_info const & base,
60 extended_type_info const & derived,
61 extended_type_info const & base,
64 return const_cast<void*>(void_upcast(
67 const_cast<void const *>(t)
71 BOOST_SERIALIZATION_DECL void const *
73 extended_type_info const & derived,
74 extended_type_info const & base,
80 extended_type_info const & derived,
81 extended_type_info const & base,
84 return const_cast<void*>(void_downcast(
87 const_cast<void const *>(t)
91 namespace void_cast_detail {
93 class BOOST_SYMBOL_VISIBLE void_caster :
94 private boost::noncopyable
97 BOOST_SERIALIZATION_DECL void const *
98 boost::serialization::void_upcast(
99 extended_type_info const & derived,
100 extended_type_info const & base,
104 BOOST_SERIALIZATION_DECL void const *
105 boost::serialization::void_downcast(
106 extended_type_info const & derived,
107 extended_type_info const & base,
111 BOOST_SERIALIZATION_DECL void recursive_register(bool includes_virtual_base = false) const;
112 BOOST_SERIALIZATION_DECL void recursive_unregister() const;
113 virtual bool has_virtual_base() const = 0;
116 const extended_type_info * m_derived;
117 const extended_type_info * m_base;
118 /*const*/ std::ptrdiff_t m_difference;
119 void_caster const * const m_parent;
121 // note that void_casters are keyed on value of
122 // member extended type info records - NOT their
123 // addresses. This is necessary in order for the
124 // void cast operations to work across dll and exe
125 // module boundaries.
126 bool operator<(const void_caster & rhs) const;
128 const void_caster & operator*(){
131 // each derived class must re-implement these;
132 virtual void const * upcast(void const * const t) const = 0;
133 virtual void const * downcast(void const * const t) const = 0;
136 extended_type_info const * derived,
137 extended_type_info const * base,
138 std::ptrdiff_t difference = 0,
139 void_caster const * const parent = 0
143 m_difference(difference),
146 virtual ~void_caster(){}
150 # pragma warning(push)
151 # pragma warning(disable : 4251 4231 4660 4275 4511 4512)
154 template <class Derived, class Base>
155 class BOOST_SYMBOL_VISIBLE void_caster_primitive :
158 void const * downcast(void const * const t) const BOOST_OVERRIDE {
160 boost::serialization::smart_cast<const Derived *, const Base *>(
161 static_cast<const Base *>(t)
165 void const * upcast(void const * const t) const BOOST_OVERRIDE {
167 boost::serialization::smart_cast<const Base *, const Derived *>(
168 static_cast<const Derived *>(t)
172 bool has_virtual_base() const BOOST_OVERRIDE {
176 void_caster_primitive();
177 ~void_caster_primitive() BOOST_OVERRIDE;
180 static std::ptrdiff_t base_offset() {
181 typename boost::aligned_storage<sizeof(Derived)>::type data;
182 return reinterpret_cast<char*>(&data)
183 - reinterpret_cast<char*>(
185 reinterpret_cast<Derived*>(&data)));
189 template <class Derived, class Base>
190 void_caster_primitive<Derived, Base>::void_caster_primitive() :
192 & type_info_implementation<Derived>::type::get_const_instance(),
193 & type_info_implementation<Base>::type::get_const_instance(),
197 recursive_register();
200 template <class Derived, class Base>
201 void_caster_primitive<Derived, Base>::~void_caster_primitive(){
202 recursive_unregister();
205 template <class Derived, class Base>
206 class BOOST_SYMBOL_VISIBLE void_caster_virtual_base :
209 bool has_virtual_base() const BOOST_OVERRIDE {
213 void const * downcast(void const * const t) const BOOST_OVERRIDE {
215 dynamic_cast<const Derived *>(
216 static_cast<const Base *>(t)
220 void const * upcast(void const * const t) const BOOST_OVERRIDE {
222 dynamic_cast<const Base *>(
223 static_cast<const Derived *>(t)
227 void_caster_virtual_base();
228 ~void_caster_virtual_base() BOOST_OVERRIDE;
235 template <class Derived, class Base>
236 void_caster_virtual_base<Derived,Base>::void_caster_virtual_base() :
238 & (type_info_implementation<Derived>::type::get_const_instance()),
239 & (type_info_implementation<Base>::type::get_const_instance())
242 recursive_register(true);
245 template <class Derived, class Base>
246 void_caster_virtual_base<Derived,Base>::~void_caster_virtual_base(){
247 recursive_unregister();
250 template <class Derived, class Base>
251 struct BOOST_SYMBOL_VISIBLE void_caster_base :
255 typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
257 void_cast_detail::void_caster_virtual_base<Derived, Base>
261 void_cast_detail::void_caster_primitive<Derived, Base>
266 } // void_cast_detail
268 template<class Derived, class Base>
270 inline const void_cast_detail::void_caster & void_cast_register(
271 Derived const * /* dnull = NULL */,
272 Base const * /* bnull = NULL */
275 typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
277 void_cast_detail::void_caster_virtual_base<Derived, Base>
281 void_cast_detail::void_caster_primitive<Derived, Base>
284 return singleton<typex>::get_const_instance();
287 template<class Derived, class Base>
288 class BOOST_SYMBOL_VISIBLE void_caster :
289 public void_cast_detail::void_caster_base<Derived, Base>::type
293 } // namespace serialization
297 # pragma warning(pop)
300 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
302 #endif // BOOST_SERIALIZATION_VOID_CAST_HPP