Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 1 | #ifndef BOOST_SERIALIZATION_VOID_CAST_HPP |
| 2 | #define BOOST_SERIALIZATION_VOID_CAST_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 | // void_cast.hpp: interface for run-time casting of void pointers. |
| 11 | |
| 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 |
| 17 | |
| 18 | // See http://www.boost.org for updates, documentation, and revision history. |
| 19 | |
| 20 | #include <cstddef> // for ptrdiff_t |
| 21 | #include <boost/config.hpp> |
| 22 | #include <boost/noncopyable.hpp> |
| 23 | |
| 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> |
| 31 | |
| 32 | #include <boost/serialization/config.hpp> |
| 33 | #include <boost/config/abi_prefix.hpp> // must be the last header |
| 34 | |
| 35 | #ifdef BOOST_MSVC |
| 36 | # pragma warning(push) |
| 37 | # pragma warning(disable : 4251 4231 4660 4275) |
| 38 | #endif |
| 39 | |
| 40 | namespace boost { |
| 41 | namespace serialization { |
| 42 | |
| 43 | class extended_type_info; |
| 44 | |
| 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. |
| 49 | |
| 50 | BOOST_SERIALIZATION_DECL void const * |
| 51 | void_upcast( |
| 52 | extended_type_info const & derived, |
| 53 | extended_type_info const & base, |
| 54 | void const * const t |
| 55 | ); |
| 56 | |
| 57 | inline void * |
| 58 | void_upcast( |
| 59 | extended_type_info const & derived, |
| 60 | extended_type_info const & base, |
| 61 | void * const t |
| 62 | ){ |
| 63 | return const_cast<void*>(void_upcast( |
| 64 | derived, |
| 65 | base, |
| 66 | const_cast<void const *>(t) |
| 67 | )); |
| 68 | } |
| 69 | |
| 70 | BOOST_SERIALIZATION_DECL void const * |
| 71 | void_downcast( |
| 72 | extended_type_info const & derived, |
| 73 | extended_type_info const & base, |
| 74 | void const * const t |
| 75 | ); |
| 76 | |
| 77 | inline void * |
| 78 | void_downcast( |
| 79 | extended_type_info const & derived, |
| 80 | extended_type_info const & base, |
| 81 | void * const t |
| 82 | ){ |
| 83 | return const_cast<void*>(void_downcast( |
| 84 | derived, |
| 85 | base, |
| 86 | const_cast<void const *>(t) |
| 87 | )); |
| 88 | } |
| 89 | |
| 90 | namespace void_cast_detail { |
| 91 | |
| 92 | class BOOST_SYMBOL_VISIBLE void_caster : |
| 93 | private boost::noncopyable |
| 94 | { |
| 95 | friend |
| 96 | BOOST_SERIALIZATION_DECL void const * |
| 97 | boost::serialization::void_upcast( |
| 98 | extended_type_info const & derived, |
| 99 | extended_type_info const & base, |
| 100 | void const * const |
| 101 | ); |
| 102 | friend |
| 103 | BOOST_SERIALIZATION_DECL void const * |
| 104 | boost::serialization::void_downcast( |
| 105 | extended_type_info const & derived, |
| 106 | extended_type_info const & base, |
| 107 | void const * const |
| 108 | ); |
| 109 | protected: |
| 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; |
| 113 | public: |
| 114 | // Data members |
| 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; |
| 119 | |
| 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 |
| 124 | // module boundries. |
| 125 | bool operator<(const void_caster & rhs) const; |
| 126 | |
| 127 | const void_caster & operator*(){ |
| 128 | return *this; |
| 129 | } |
| 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; |
| 133 | // Constructor |
| 134 | void_caster( |
| 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 |
| 139 | ) : |
| 140 | m_derived(derived), |
| 141 | m_base(base), |
| 142 | m_difference(difference), |
| 143 | m_parent(parent) |
| 144 | {} |
| 145 | virtual ~void_caster(){} |
| 146 | }; |
| 147 | |
| 148 | #ifdef BOOST_MSVC |
| 149 | # pragma warning(push) |
| 150 | # pragma warning(disable : 4251 4231 4660 4275 4511 4512) |
| 151 | #endif |
| 152 | |
| 153 | template <class Derived, class Base> |
| 154 | class BOOST_SYMBOL_VISIBLE void_caster_primitive : |
| 155 | public void_caster |
| 156 | { |
| 157 | virtual void const * downcast(void const * const t) const { |
| 158 | const Derived * d = |
| 159 | boost::serialization::smart_cast<const Derived *, const Base *>( |
| 160 | static_cast<const Base *>(t) |
| 161 | ); |
| 162 | return d; |
| 163 | } |
| 164 | virtual void const * upcast(void const * const t) const { |
| 165 | const Base * b = |
| 166 | boost::serialization::smart_cast<const Base *, const Derived *>( |
| 167 | static_cast<const Derived *>(t) |
| 168 | ); |
| 169 | return b; |
| 170 | } |
| 171 | virtual bool has_virtual_base() const { |
| 172 | return false; |
| 173 | } |
| 174 | public: |
| 175 | void_caster_primitive(); |
| 176 | virtual ~void_caster_primitive(); |
| 177 | }; |
| 178 | |
| 179 | template <class Derived, class Base> |
| 180 | void_caster_primitive<Derived, Base>::void_caster_primitive() : |
| 181 | void_caster( |
| 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) |
| 190 | ) |
| 191 | ) - (1 << 20) |
| 192 | ) |
| 193 | { |
| 194 | recursive_register(); |
| 195 | } |
| 196 | |
| 197 | template <class Derived, class Base> |
| 198 | void_caster_primitive<Derived, Base>::~void_caster_primitive(){ |
| 199 | recursive_unregister(); |
| 200 | } |
| 201 | |
| 202 | template <class Derived, class Base> |
| 203 | class BOOST_SYMBOL_VISIBLE void_caster_virtual_base : |
| 204 | public void_caster |
| 205 | { |
| 206 | virtual bool has_virtual_base() const { |
| 207 | return true; |
| 208 | } |
| 209 | public: |
| 210 | virtual void const * downcast(void const * const t) const { |
| 211 | const Derived * d = |
| 212 | dynamic_cast<const Derived *>( |
| 213 | static_cast<const Base *>(t) |
| 214 | ); |
| 215 | return d; |
| 216 | } |
| 217 | virtual void const * upcast(void const * const t) const { |
| 218 | const Base * b = |
| 219 | dynamic_cast<const Base *>( |
| 220 | static_cast<const Derived *>(t) |
| 221 | ); |
| 222 | return b; |
| 223 | } |
| 224 | void_caster_virtual_base(); |
| 225 | virtual ~void_caster_virtual_base(); |
| 226 | }; |
| 227 | |
| 228 | #ifdef BOOST_MSVC |
| 229 | #pragma warning(pop) |
| 230 | #endif |
| 231 | |
| 232 | template <class Derived, class Base> |
| 233 | void_caster_virtual_base<Derived,Base>::void_caster_virtual_base() : |
| 234 | void_caster( |
| 235 | & (type_info_implementation<Derived>::type::get_const_instance()), |
| 236 | & (type_info_implementation<Base>::type::get_const_instance()) |
| 237 | ) |
| 238 | { |
| 239 | recursive_register(true); |
| 240 | } |
| 241 | |
| 242 | template <class Derived, class Base> |
| 243 | void_caster_virtual_base<Derived,Base>::~void_caster_virtual_base(){ |
| 244 | recursive_unregister(); |
| 245 | } |
| 246 | |
| 247 | template <class Derived, class Base> |
| 248 | struct BOOST_SYMBOL_VISIBLE void_caster_base : |
| 249 | public void_caster |
| 250 | { |
| 251 | typedef |
| 252 | typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>, |
| 253 | mpl::identity< |
| 254 | void_cast_detail::void_caster_virtual_base<Derived, Base> |
| 255 | > |
| 256 | ,// else |
| 257 | mpl::identity< |
| 258 | void_cast_detail::void_caster_primitive<Derived, Base> |
| 259 | > |
| 260 | >::type type; |
| 261 | }; |
| 262 | |
| 263 | } // void_cast_detail |
| 264 | |
| 265 | template<class Derived, class Base> |
| 266 | BOOST_DLLEXPORT |
| 267 | inline const void_cast_detail::void_caster & void_cast_register( |
| 268 | Derived const * /* dnull = NULL */, |
| 269 | Base const * /* bnull = NULL */ |
| 270 | ){ |
| 271 | typedef |
| 272 | typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>, |
| 273 | mpl::identity< |
| 274 | void_cast_detail::void_caster_virtual_base<Derived, Base> |
| 275 | > |
| 276 | ,// else |
| 277 | mpl::identity< |
| 278 | void_cast_detail::void_caster_primitive<Derived, Base> |
| 279 | > |
| 280 | >::type typex; |
| 281 | return singleton<typex>::get_const_instance(); |
| 282 | } |
| 283 | |
| 284 | template<class Derived, class Base> |
| 285 | class BOOST_SYMBOL_VISIBLE void_caster : |
| 286 | public void_cast_detail::void_caster_base<Derived, Base>::type |
| 287 | { |
| 288 | }; |
| 289 | |
| 290 | } // namespace serialization |
| 291 | } // namespace boost |
| 292 | |
| 293 | #ifdef BOOST_MSVC |
| 294 | # pragma warning(pop) |
| 295 | #endif |
| 296 | |
| 297 | #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas |
| 298 | |
| 299 | #endif // BOOST_SERIALIZATION_VOID_CAST_HPP |