smart_cast.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. namespace boost {
  46. namespace serialization {
  47. namespace smart_cast_impl {
  48. template<class T>
  49. struct reference {
  50. struct polymorphic {
  51. struct linear {
  52. template<class U>
  53. static T cast(U & u){
  54. return static_cast<T>(u);
  55. }
  56. };
  57. struct cross {
  58. template<class U>
  59. static T cast(U & u){
  60. return dynamic_cast<T>(u);
  61. }
  62. };
  63. template<class U>
  64. static T cast(U & u){
  65. // if we're in debug mode
  66. #if ! defined(NDEBUG) \
  67. || defined(__BORLANDC__) && (__BORLANDC__ <= 0x560) \
  68. || defined(__MWERKS__)
  69. // do a checked dynamic cast
  70. return cross::cast(u);
  71. #else
  72. // borland 5.51 chokes here so we can't use it
  73. // note: if remove_reference isn't function for these types
  74. // cross casting will be selected this will work but will
  75. // not be the most efficient method. This will conflict with
  76. // the original smart_cast motivation.
  77. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  78. BOOST_DEDUCED_TYPENAME mpl::and_<
  79. mpl::not_<is_base_and_derived<
  80. BOOST_DEDUCED_TYPENAME remove_reference<T>::type,
  81. U
  82. > >,
  83. mpl::not_<is_base_and_derived<
  84. U,
  85. BOOST_DEDUCED_TYPENAME remove_reference<T>::type
  86. > >
  87. >,
  88. // borland chokes w/o full qualification here
  89. mpl::identity<cross>,
  90. mpl::identity<linear>
  91. >::type typex;
  92. // typex works around gcc 2.95 issue
  93. return typex::cast(u);
  94. #endif
  95. }
  96. };
  97. struct non_polymorphic {
  98. template<class U>
  99. static T cast(U & u){
  100. return static_cast<T>(u);
  101. }
  102. };
  103. template<class U>
  104. static T cast(U & u){
  105. #if defined(__BORLANDC__)
  106. return mpl::eval_if<
  107. boost::is_polymorphic<U>,
  108. mpl::identity<polymorphic>,
  109. mpl::identity<non_polymorphic>
  110. >::type::cast(u);
  111. #else
  112. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  113. boost::is_polymorphic<U>,
  114. mpl::identity<polymorphic>,
  115. mpl::identity<non_polymorphic>
  116. >::type typex;
  117. return typex::cast(u);
  118. #endif
  119. }
  120. };
  121. template<class T>
  122. struct pointer {
  123. struct polymorphic {
  124. // unfortunately, this below fails to work for virtual base
  125. // classes. need has_virtual_base to do this.
  126. // Subject for further study
  127. #if 0
  128. struct linear {
  129. template<class U>
  130. static T cast(U * u){
  131. return static_cast<T>(u);
  132. }
  133. };
  134. struct cross {
  135. template<class U>
  136. static T cast(U * u){
  137. T tmp = dynamic_cast<T>(u);
  138. #ifndef NDEBUG
  139. if ( tmp == 0 ) throw std::bad_cast();
  140. #endif
  141. return tmp;
  142. }
  143. };
  144. template<class U>
  145. static T cast(U * u){
  146. // if we're in debug mode
  147. #if ! defined(NDEBUG) || defined(__BORLANDC__) && (__BORLANDC__ <= 0x560)
  148. // do a checked dynamic cast
  149. return cross::cast(u);
  150. #else
  151. // borland 5.51 chokes here so we can't use it
  152. // note: if remove_pointer isn't function for these types
  153. // cross casting will be selected this will work but will
  154. // not be the most efficient method. This will conflict with
  155. // the original smart_cast motivation.
  156. typedef
  157. BOOST_DEDUCED_TYPENAME mpl::eval_if<
  158. BOOST_DEDUCED_TYPENAME mpl::and_<
  159. mpl::not_<is_base_and_derived<
  160. BOOST_DEDUCED_TYPENAME remove_pointer<T>::type,
  161. U
  162. > >,
  163. mpl::not_<is_base_and_derived<
  164. U,
  165. BOOST_DEDUCED_TYPENAME remove_pointer<T>::type
  166. > >
  167. >,
  168. // borland chokes w/o full qualification here
  169. mpl::identity<cross>,
  170. mpl::identity<linear>
  171. >::type typex;
  172. return typex::cast(u);
  173. #endif
  174. }
  175. #else
  176. template<class U>
  177. static T cast(U * u){
  178. T tmp = dynamic_cast<T>(u);
  179. #ifndef NDEBUG
  180. if ( tmp == 0 ) throw std::bad_cast();
  181. #endif
  182. return tmp;
  183. }
  184. #endif
  185. };
  186. struct non_polymorphic {
  187. template<class U>
  188. static T cast(U * u){
  189. return static_cast<T>(u);
  190. }
  191. };
  192. template<class U>
  193. static T cast(U * u){
  194. #if defined(__BORLANDC__)
  195. return mpl::eval_if<
  196. boost::is_polymorphic<U>,
  197. mpl::identity<polymorphic>,
  198. mpl::identity<non_polymorphic>
  199. >::type::cast(u);
  200. #else
  201. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  202. boost::is_polymorphic<U>,
  203. mpl::identity<polymorphic>,
  204. mpl::identity<non_polymorphic>
  205. >::type typex;
  206. return typex::cast(u);
  207. #endif
  208. }
  209. };
  210. template<class TPtr>
  211. struct void_pointer {
  212. template<class UPtr>
  213. static TPtr cast(UPtr uptr){
  214. return static_cast<TPtr>(uptr);
  215. }
  216. };
  217. template<class T>
  218. struct error {
  219. // if we get here, its because we are using one argument in the
  220. // cast on a system which doesn't support partial template
  221. // specialization
  222. template<class U>
  223. static T cast(U u){
  224. BOOST_STATIC_ASSERT(sizeof(T)==0);
  225. return * static_cast<T *>(NULL);
  226. }
  227. };
  228. } // smart_cast_impl
  229. // this implements:
  230. // smart_cast<Target *, Source *>(Source * s)
  231. // smart_cast<Target &, Source &>(s)
  232. // note that it will fail with
  233. // smart_cast<Target &>(s)
  234. template<class T, class U>
  235. T smart_cast(U u) {
  236. typedef
  237. BOOST_DEDUCED_TYPENAME mpl::eval_if<
  238. BOOST_DEDUCED_TYPENAME mpl::or_<
  239. boost::is_same<void *, U>,
  240. boost::is_same<void *, T>,
  241. boost::is_same<const void *, U>,
  242. boost::is_same<const void *, T>
  243. >,
  244. mpl::identity<smart_cast_impl::void_pointer<T> >,
  245. // else
  246. BOOST_DEDUCED_TYPENAME mpl::eval_if<boost::is_pointer<U>,
  247. mpl::identity<smart_cast_impl::pointer<T> >,
  248. // else
  249. BOOST_DEDUCED_TYPENAME mpl::eval_if<boost::is_reference<U>,
  250. mpl::identity<smart_cast_impl::reference<T> >,
  251. // else
  252. mpl::identity<smart_cast_impl::error<T>
  253. >
  254. >
  255. >
  256. >::type typex;
  257. return typex::cast(u);
  258. }
  259. // this implements:
  260. // smart_cast_reference<Target &>(Source & s)
  261. template<class T, class U>
  262. T smart_cast_reference(U & u) {
  263. return smart_cast_impl::reference<T>::cast(u);
  264. }
  265. } // namespace serialization
  266. } // namespace boost
  267. #endif // BOOST_SERIALIZATION_SMART_CAST_HPP