void_cast.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef BOOST_SERIALIZATION_VOID_CAST_HPP
  2. #define BOOST_SERIALIZATION_VOID_CAST_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // void_cast.hpp: interface for run-time casting of void pointers.
  9. // (C) Copyright 2002-2009 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // [email protected]
  14. // See http://www.boost.org for updates, documentation, and revision history.
  15. #include <cstddef> // for ptrdiff_t
  16. #include <boost/config.hpp>
  17. #include <boost/noncopyable.hpp>
  18. #include <boost/serialization/smart_cast.hpp>
  19. #include <boost/serialization/singleton.hpp>
  20. #include <boost/serialization/force_include.hpp>
  21. #include <boost/serialization/type_info_implementation.hpp>
  22. #include <boost/serialization/extended_type_info.hpp>
  23. #include <boost/type_traits/is_virtual_base_of.hpp>
  24. #include <boost/type_traits/aligned_storage.hpp>
  25. #include <boost/serialization/void_cast_fwd.hpp>
  26. #include <boost/serialization/config.hpp>
  27. #include <boost/config/abi_prefix.hpp> // must be the last header
  28. #ifdef BOOST_MSVC
  29. # pragma warning(push)
  30. # pragma warning(disable : 4251 4231 4660 4275)
  31. #endif
  32. namespace boost {
  33. namespace serialization {
  34. class extended_type_info;
  35. // Given a void *, assume that it really points to an instance of one type
  36. // and alter it so that it would point to an instance of a related type.
  37. // Return the altered pointer. If there exists no sequence of casts that
  38. // can transform from_type to to_type, return a NULL.
  39. BOOST_SERIALIZATION_DECL void const *
  40. void_upcast(
  41. extended_type_info const & derived,
  42. extended_type_info const & base,
  43. void const * const t
  44. );
  45. inline void *
  46. void_upcast(
  47. extended_type_info const & derived,
  48. extended_type_info const & base,
  49. void * const t
  50. ){
  51. return const_cast<void*>(void_upcast(
  52. derived,
  53. base,
  54. const_cast<void const *>(t)
  55. ));
  56. }
  57. BOOST_SERIALIZATION_DECL void const *
  58. void_downcast(
  59. extended_type_info const & derived,
  60. extended_type_info const & base,
  61. void const * const t
  62. );
  63. inline void *
  64. void_downcast(
  65. extended_type_info const & derived,
  66. extended_type_info const & base,
  67. void * const t
  68. ){
  69. return const_cast<void*>(void_downcast(
  70. derived,
  71. base,
  72. const_cast<void const *>(t)
  73. ));
  74. }
  75. namespace void_cast_detail {
  76. class BOOST_SYMBOL_VISIBLE void_caster :
  77. private boost::noncopyable
  78. {
  79. friend
  80. BOOST_SERIALIZATION_DECL void const *
  81. boost::serialization::void_upcast(
  82. extended_type_info const & derived,
  83. extended_type_info const & base,
  84. void const * const
  85. );
  86. friend
  87. BOOST_SERIALIZATION_DECL void const *
  88. boost::serialization::void_downcast(
  89. extended_type_info const & derived,
  90. extended_type_info const & base,
  91. void const * const
  92. );
  93. protected:
  94. BOOST_SERIALIZATION_DECL void recursive_register(bool includes_virtual_base = false) const;
  95. BOOST_SERIALIZATION_DECL void recursive_unregister() const;
  96. virtual bool has_virtual_base() const = 0;
  97. public:
  98. // Data members
  99. const extended_type_info * m_derived;
  100. const extended_type_info * m_base;
  101. /*const*/ std::ptrdiff_t m_difference;
  102. void_caster const * const m_parent;
  103. // note that void_casters are keyed on value of
  104. // member extended type info records - NOT their
  105. // addresses. This is necessary in order for the
  106. // void cast operations to work across dll and exe
  107. // module boundaries.
  108. bool operator<(const void_caster & rhs) const;
  109. const void_caster & operator*(){
  110. return *this;
  111. }
  112. // each derived class must re-implement these;
  113. virtual void const * upcast(void const * const t) const = 0;
  114. virtual void const * downcast(void const * const t) const = 0;
  115. // Constructor
  116. void_caster(
  117. extended_type_info const * derived,
  118. extended_type_info const * base,
  119. std::ptrdiff_t difference = 0,
  120. void_caster const * const parent = 0
  121. ) :
  122. m_derived(derived),
  123. m_base(base),
  124. m_difference(difference),
  125. m_parent(parent)
  126. {}
  127. virtual ~void_caster(){}
  128. };
  129. #ifdef BOOST_MSVC
  130. # pragma warning(push)
  131. # pragma warning(disable : 4251 4231 4660 4275 4511 4512)
  132. #endif
  133. template <class Derived, class Base>
  134. class BOOST_SYMBOL_VISIBLE void_caster_primitive :
  135. public void_caster
  136. {
  137. void const * downcast(void const * const t) const BOOST_OVERRIDE {
  138. const Derived * d =
  139. boost::serialization::smart_cast<const Derived *, const Base *>(
  140. static_cast<const Base *>(t)
  141. );
  142. return d;
  143. }
  144. void const * upcast(void const * const t) const BOOST_OVERRIDE {
  145. const Base * b =
  146. boost::serialization::smart_cast<const Base *, const Derived *>(
  147. static_cast<const Derived *>(t)
  148. );
  149. return b;
  150. }
  151. bool has_virtual_base() const BOOST_OVERRIDE {
  152. return false;
  153. }
  154. public:
  155. void_caster_primitive();
  156. ~void_caster_primitive() BOOST_OVERRIDE;
  157. private:
  158. static std::ptrdiff_t base_offset() {
  159. typename boost::aligned_storage<sizeof(Derived)>::type data;
  160. return reinterpret_cast<char*>(&data)
  161. - reinterpret_cast<char*>(
  162. static_cast<Base*>(
  163. reinterpret_cast<Derived*>(&data)));
  164. }
  165. };
  166. template <class Derived, class Base>
  167. void_caster_primitive<Derived, Base>::void_caster_primitive() :
  168. void_caster(
  169. & type_info_implementation<Derived>::type::get_const_instance(),
  170. & type_info_implementation<Base>::type::get_const_instance(),
  171. base_offset()
  172. )
  173. {
  174. recursive_register();
  175. }
  176. template <class Derived, class Base>
  177. void_caster_primitive<Derived, Base>::~void_caster_primitive(){
  178. recursive_unregister();
  179. }
  180. template <class Derived, class Base>
  181. class BOOST_SYMBOL_VISIBLE void_caster_virtual_base :
  182. public void_caster
  183. {
  184. bool has_virtual_base() const BOOST_OVERRIDE {
  185. return true;
  186. }
  187. public:
  188. void const * downcast(void const * const t) const BOOST_OVERRIDE {
  189. const Derived * d =
  190. dynamic_cast<const Derived *>(
  191. static_cast<const Base *>(t)
  192. );
  193. return d;
  194. }
  195. void const * upcast(void const * const t) const BOOST_OVERRIDE {
  196. const Base * b =
  197. dynamic_cast<const Base *>(
  198. static_cast<const Derived *>(t)
  199. );
  200. return b;
  201. }
  202. void_caster_virtual_base();
  203. ~void_caster_virtual_base() BOOST_OVERRIDE;
  204. };
  205. #ifdef BOOST_MSVC
  206. #pragma warning(pop)
  207. #endif
  208. template <class Derived, class Base>
  209. void_caster_virtual_base<Derived,Base>::void_caster_virtual_base() :
  210. void_caster(
  211. & (type_info_implementation<Derived>::type::get_const_instance()),
  212. & (type_info_implementation<Base>::type::get_const_instance())
  213. )
  214. {
  215. recursive_register(true);
  216. }
  217. template <class Derived, class Base>
  218. void_caster_virtual_base<Derived,Base>::~void_caster_virtual_base(){
  219. recursive_unregister();
  220. }
  221. template <class Derived, class Base>
  222. struct BOOST_SYMBOL_VISIBLE void_caster_base :
  223. public void_caster
  224. {
  225. typedef
  226. typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
  227. mpl::identity<
  228. void_cast_detail::void_caster_virtual_base<Derived, Base>
  229. >
  230. ,// else
  231. mpl::identity<
  232. void_cast_detail::void_caster_primitive<Derived, Base>
  233. >
  234. >::type type;
  235. };
  236. } // void_cast_detail
  237. template<class Derived, class Base>
  238. BOOST_DLLEXPORT
  239. inline const void_cast_detail::void_caster & void_cast_register(
  240. Derived const * /* dnull = NULL */,
  241. Base const * /* bnull = NULL */
  242. ){
  243. typedef
  244. typename mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
  245. mpl::identity<
  246. void_cast_detail::void_caster_virtual_base<Derived, Base>
  247. >
  248. ,// else
  249. mpl::identity<
  250. void_cast_detail::void_caster_primitive<Derived, Base>
  251. >
  252. >::type typex;
  253. return singleton<typex>::get_const_instance();
  254. }
  255. template<class Derived, class Base>
  256. class BOOST_SYMBOL_VISIBLE void_caster :
  257. public void_cast_detail::void_caster_base<Derived, Base>::type
  258. {
  259. };
  260. } // namespace serialization
  261. } // namespace boost
  262. #ifdef BOOST_MSVC
  263. # pragma warning(pop)
  264. #endif
  265. #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  266. #endif // BOOST_SERIALIZATION_VOID_CAST_HPP