smart_cast.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #ifndef BOOST_SERIALIZATION_SMART_CAST_HPP
  2. #define BOOST_SERIALIZATION_SMART_CAST_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // smart_cast.hpp:
  9. // (C) Copyright 2002 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. // See http://www.boost.org/libs/serialization for updates, documentation, and revision history.
  14. // casting of pointers and references.
  15. // In casting between different C++ classes, there are a number of
  16. // rules that have to be kept in mind in deciding whether to use
  17. // static_cast or dynamic_cast.
  18. // a) dynamic casting can only be applied when one of the types is polymorphic
  19. // Otherwise static_cast must be used.
  20. // b) only dynamic casting can do runtime error checking
  21. // use of static_cast is generally un checked even when compiled for debug
  22. // c) static_cast would be considered faster than dynamic_cast.
  23. // If casting is applied to a template parameter, there is no apriori way
  24. // to know which of the two casting methods will be permitted or convenient.
  25. // smart_cast uses C++ type_traits, and program debug mode to select the
  26. // most convenient cast to use.
  27. #include <exception>
  28. #include <typeinfo>
  29. #include <cstddef> // NULL
  30. #include <boost/config.hpp>
  31. #include <boost/static_assert.hpp>
  32. #include <boost/type_traits/is_base_and_derived.hpp>
  33. #include <boost/type_traits/is_polymorphic.hpp>
  34. #include <boost/type_traits/is_pointer.hpp>
  35. #include <boost/type_traits/is_reference.hpp>
  36. #include <boost/type_traits/is_same.hpp>
  37. #include <boost/type_traits/remove_pointer.hpp>
  38. #include <boost/type_traits/remove_reference.hpp>
  39. #include <boost/mpl/eval_if.hpp>
  40. #include <boost/mpl/if.hpp>
  41. #include <boost/mpl/or.hpp>
  42. #include <boost/mpl/and.hpp>
  43. #include <boost/mpl/not.hpp>
  44. #include <boost/mpl/identity.hpp>
  45. #include <boost/serialization/throw_exception.hpp>
  46. namespace boost {
  47. namespace serialization {
  48. namespace smart_cast_impl {
  49. template<class T>
  50. struct reference {
  51. struct polymorphic {
  52. struct linear {
  53. template<class U>
  54. static T cast(U & u){
  55. return static_cast< T >(u);
  56. }
  57. };
  58. struct cross {
  59. template<class U>
  60. static T cast(U & u){
  61. return dynamic_cast< T >(u);
  62. }
  63. };
  64. template<class U>
  65. static T cast(U & u){
  66. // if we're in debug mode
  67. #if ! defined(NDEBUG) \
  68. || defined(__BORLANDC__) && (__BORLANDC__ <= 0x560) \
  69. || defined(__MWERKS__)
  70. // do a checked dynamic cast
  71. return cross::cast(u);
  72. #else
  73. // borland 5.51 chokes here so we can't use it
  74. // note: if remove_reference isn't function for these types
  75. // cross casting will be selected this will work but will
  76. // not be the most efficient method. This will conflict with
  77. // the original smart_cast motivation.
  78. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  79. BOOST_DEDUCED_TYPENAME mpl::and_<
  80. mpl::not_<is_base_and_derived<
  81. BOOST_DEDUCED_TYPENAME remove_reference< T >::type,
  82. U
  83. > >,
  84. mpl::not_<is_base_and_derived<
  85. U,
  86. BOOST_DEDUCED_TYPENAME remove_reference< T >::type
  87. > >
  88. >,
  89. // borland chokes w/o full qualification here
  90. mpl::identity<cross>,
  91. mpl::identity<linear>
  92. >::type typex;
  93. // typex works around gcc 2.95 issue
  94. return typex::cast(u);
  95. #endif
  96. }
  97. };
  98. struct non_polymorphic {
  99. template<class U>
  100. static T cast(U & u){
  101. return static_cast< T >(u);
  102. }
  103. };
  104. template<class U>
  105. static T cast(U & u){
  106. #if defined(__BORLANDC__)
  107. return mpl::eval_if<
  108. boost::is_polymorphic<U>,
  109. mpl::identity<polymorphic>,
  110. mpl::identity<non_polymorphic>
  111. >::type::cast(u);
  112. #else
  113. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  114. boost::is_polymorphic<U>,
  115. mpl::identity<polymorphic>,
  116. mpl::identity<non_polymorphic>
  117. >::type typex;
  118. return typex::cast(u);
  119. #endif
  120. }
  121. };
  122. template<class T>
  123. struct pointer {
  124. struct polymorphic {
  125. // unfortunately, this below fails to work for virtual base
  126. // classes. need has_virtual_base to do this.
  127. // Subject for further study
  128. #if 0
  129. struct linear {
  130. template<class U>
  131. static T cast(U * u){
  132. return static_cast< T >(u);
  133. }
  134. };
  135. struct cross {
  136. template<class U>
  137. static T cast(U * u){
  138. T tmp = dynamic_cast< T >(u);
  139. #ifndef NDEBUG
  140. if ( tmp == 0 ) throw_exception(std::bad_cast());
  141. #endif
  142. return tmp;
  143. }
  144. };
  145. template<class U>
  146. static T cast(U * u){
  147. // if we're in debug mode
  148. #if ! defined(NDEBUG) || defined(__BORLANDC__) && (__BORLANDC__ <= 0x560)
  149. // do a checked dynamic cast
  150. return cross::cast(u);
  151. #else
  152. // borland 5.51 chokes here so we can't use it
  153. // note: if remove_pointer isn't function for these types
  154. // cross casting will be selected this will work but will
  155. // not be the most efficient method. This will conflict with
  156. // the original smart_cast motivation.
  157. typedef
  158. BOOST_DEDUCED_TYPENAME mpl::eval_if<
  159. BOOST_DEDUCED_TYPENAME mpl::and_<
  160. mpl::not_<is_base_and_derived<
  161. BOOST_DEDUCED_TYPENAME remove_pointer< T >::type,
  162. U
  163. > >,
  164. mpl::not_<is_base_and_derived<
  165. U,
  166. BOOST_DEDUCED_TYPENAME remove_pointer< T >::type
  167. > >
  168. >,
  169. // borland chokes w/o full qualification here
  170. mpl::identity<cross>,
  171. mpl::identity<linear>
  172. >::type typex;
  173. return typex::cast(u);
  174. #endif
  175. }
  176. #else
  177. template<class U>
  178. static T cast(U * u){
  179. T tmp = dynamic_cast< T >(u);
  180. #ifndef NDEBUG
  181. if ( tmp == 0 ) throw_exception(std::bad_cast());
  182. #endif
  183. return tmp;
  184. }
  185. #endif
  186. };
  187. struct non_polymorphic {
  188. template<class U>
  189. static T cast(U * u){
  190. return static_cast< T >(u);
  191. }
  192. };
  193. template<class U>
  194. static T cast(U * u){
  195. #if defined(__BORLANDC__)
  196. return mpl::eval_if<
  197. boost::is_polymorphic<U>,
  198. mpl::identity<polymorphic>,
  199. mpl::identity<non_polymorphic>
  200. >::type::cast(u);
  201. #else
  202. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  203. boost::is_polymorphic<U>,
  204. mpl::identity<polymorphic>,
  205. mpl::identity<non_polymorphic>
  206. >::type typex;
  207. return typex::cast(u);
  208. #endif
  209. }
  210. };
  211. template<class TPtr>
  212. struct void_pointer {
  213. template<class UPtr>
  214. static TPtr cast(UPtr uptr){
  215. return static_cast<TPtr>(uptr);
  216. }
  217. };
  218. template<class T>
  219. struct error {
  220. // if we get here, its because we are using one argument in the
  221. // cast on a system which doesn't support partial template
  222. // specialization
  223. template<class U>
  224. static T cast(U u){
  225. BOOST_STATIC_ASSERT(sizeof(T)==0);
  226. return * static_cast<T *>(NULL);
  227. }
  228. };
  229. } // smart_cast_impl
  230. // this implements:
  231. // smart_cast<Target *, Source *>(Source * s)
  232. // smart_cast<Target &, Source &>(s)
  233. // note that it will fail with
  234. // smart_cast<Target &>(s)
  235. template<class T, class U>
  236. T smart_cast(U u) {
  237. typedef
  238. BOOST_DEDUCED_TYPENAME mpl::eval_if<
  239. BOOST_DEDUCED_TYPENAME mpl::or_<
  240. boost::is_same<void *, U>,
  241. boost::is_same<void *, T>,
  242. boost::is_same<const void *, U>,
  243. boost::is_same<const void *, T>
  244. >,
  245. mpl::identity<smart_cast_impl::void_pointer< T > >,
  246. // else
  247. BOOST_DEDUCED_TYPENAME mpl::eval_if<boost::is_pointer<U>,
  248. mpl::identity<smart_cast_impl::pointer< T > >,
  249. // else
  250. BOOST_DEDUCED_TYPENAME mpl::eval_if<boost::is_reference<U>,
  251. mpl::identity<smart_cast_impl::reference< T > >,
  252. // else
  253. mpl::identity<smart_cast_impl::error< T >
  254. >
  255. >
  256. >
  257. >::type typex;
  258. return typex::cast(u);
  259. }
  260. // this implements:
  261. // smart_cast_reference<Target &>(Source & s)
  262. template<class T, class U>
  263. T smart_cast_reference(U & u) {
  264. return smart_cast_impl::reference< T >::cast(u);
  265. }
  266. } // namespace serialization
  267. } // namespace boost
  268. #endif // BOOST_SERIALIZATION_SMART_CAST_HPP