old_numeric_cast.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // boost cast.hpp header file ----------------------------------------------//
  2. // (C) Copyright Kevlin Henney and Dave Abrahams 1999.
  3. // Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/conversion for Documentation.
  7. // Revision History
  8. // 02 Jun 14 Remove VC6 workarounds.
  9. // 16 Jul 11 Bugfixes for VC6.
  10. // 23 JUN 05 Code extracted from /boost/cast.hpp into this new header.
  11. // Keeps this legacy version of numeric_cast<> for old compilers
  12. // wich can't compile the new version in /boost/numeric/conversion/cast.hpp
  13. // (Fernando Cacciola)
  14. // 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
  15. // <boost/limits.hpp> instead (the workaround did not
  16. // actually compile when BOOST_NO_LIMITS was defined in
  17. // any case, so we loose nothing). (John Maddock)
  18. // 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
  19. // worked with stock GCC; trying to get it to do that broke
  20. // vc-stlport.
  21. // 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
  22. // Removed unused BOOST_EXPLICIT_TARGET macro. Moved
  23. // boost::detail::type to boost/type.hpp. Made it compile with
  24. // stock gcc again (Dave Abrahams)
  25. // 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
  26. // Review (Beman Dawes)
  27. // 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
  28. // 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
  29. // (Dave Abrahams)
  30. // 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
  31. // 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
  32. // 27 Jun 00 More MSVC6 workarounds
  33. // 15 Jun 00 Add workarounds for MSVC6
  34. // 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
  35. // 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
  36. // 29 Dec 99 Change using declarations so usages in other namespaces work
  37. // correctly (Dave Abrahams)
  38. // 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
  39. // as suggested Darin Adler and improved by Valentin Bonnard.
  40. // 2 Sep 99 Remove controversial asserts, simplify, rename.
  41. // 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
  42. // place in nested namespace.
  43. // 3 Aug 99 Initial version
  44. #ifndef BOOST_OLD_NUMERIC_CAST_HPP
  45. #define BOOST_OLD_NUMERIC_CAST_HPP
  46. # include <boost/config.hpp>
  47. # include <cassert>
  48. # include <typeinfo>
  49. # include <boost/type.hpp>
  50. # include <boost/limits.hpp>
  51. # include <boost/numeric/conversion/converter_policies.hpp>
  52. namespace boost
  53. {
  54. using numeric::bad_numeric_cast;
  55. // LEGACY numeric_cast [only for some old broken compilers] --------------------------------------//
  56. // Contributed by Kevlin Henney
  57. // numeric_cast ------------------------------------------------------------//
  58. #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_SGI_CPP_LIMITS)
  59. namespace detail
  60. {
  61. template <class T>
  62. struct signed_numeric_limits : std::numeric_limits<T>
  63. {
  64. static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  65. {
  66. return (std::numeric_limits<T>::min)() >= 0
  67. // unary minus causes integral promotion, thus the static_cast<>
  68. ? static_cast<T>(-(std::numeric_limits<T>::max)())
  69. : (std::numeric_limits<T>::min)();
  70. };
  71. };
  72. // Move to namespace boost in utility.hpp?
  73. template <class T, bool specialized>
  74. struct fixed_numeric_limits_base
  75. : public if_true< std::numeric_limits<T>::is_signed >
  76. ::BOOST_NESTED_TEMPLATE then< signed_numeric_limits<T>,
  77. std::numeric_limits<T>
  78. >::type
  79. {};
  80. template <class T>
  81. struct fixed_numeric_limits
  82. : fixed_numeric_limits_base<T,(std::numeric_limits<T>::is_specialized)>
  83. {};
  84. # ifdef BOOST_HAS_LONG_LONG
  85. // cover implementations which supply no specialization for long
  86. // long / unsigned long long. Not intended to be full
  87. // numeric_limits replacements, but good enough for numeric_cast<>
  88. template <>
  89. struct fixed_numeric_limits_base< ::boost::long_long_type, false>
  90. {
  91. BOOST_STATIC_CONSTANT(bool, is_specialized = true);
  92. BOOST_STATIC_CONSTANT(bool, is_signed = true);
  93. static ::boost::long_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  94. {
  95. # ifdef LONGLONG_MAX
  96. return LONGLONG_MAX;
  97. # else
  98. return 9223372036854775807LL; // hope this is portable
  99. # endif
  100. }
  101. static ::boost::long_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  102. {
  103. # ifdef LONGLONG_MIN
  104. return LONGLONG_MIN;
  105. # else
  106. return -( 9223372036854775807LL )-1; // hope this is portable
  107. # endif
  108. }
  109. };
  110. template <>
  111. struct fixed_numeric_limits_base< ::boost::ulong_long_type, false>
  112. {
  113. BOOST_STATIC_CONSTANT(bool, is_specialized = true);
  114. BOOST_STATIC_CONSTANT(bool, is_signed = false);
  115. static ::boost::ulong_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  116. {
  117. # ifdef ULONGLONG_MAX
  118. return ULONGLONG_MAX;
  119. # else
  120. return 0xffffffffffffffffULL; // hope this is portable
  121. # endif
  122. }
  123. static ::boost::ulong_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
  124. };
  125. # endif
  126. } // namespace detail
  127. // less_than_type_min -
  128. // x_is_signed should be numeric_limits<X>::is_signed
  129. // y_is_signed should be numeric_limits<Y>::is_signed
  130. // y_min should be numeric_limits<Y>::min()
  131. //
  132. // check(x, y_min) returns true iff x < y_min without invoking comparisons
  133. // between signed and unsigned values.
  134. //
  135. // "poor man's partial specialization" is in use here.
  136. template <bool x_is_signed, bool y_is_signed>
  137. struct less_than_type_min
  138. {
  139. template <class X, class Y>
  140. static bool check(X x, Y y_min)
  141. { return x < y_min; }
  142. };
  143. template <>
  144. struct less_than_type_min<false, true>
  145. {
  146. template <class X, class Y>
  147. static bool check(X, Y)
  148. { return false; }
  149. };
  150. template <>
  151. struct less_than_type_min<true, false>
  152. {
  153. template <class X, class Y>
  154. static bool check(X x, Y)
  155. { return x < 0; }
  156. };
  157. // greater_than_type_max -
  158. // same_sign should be:
  159. // numeric_limits<X>::is_signed == numeric_limits<Y>::is_signed
  160. // y_max should be numeric_limits<Y>::max()
  161. //
  162. // check(x, y_max) returns true iff x > y_max without invoking comparisons
  163. // between signed and unsigned values.
  164. //
  165. // "poor man's partial specialization" is in use here.
  166. template <bool same_sign, bool x_is_signed>
  167. struct greater_than_type_max;
  168. template<>
  169. struct greater_than_type_max<true, true>
  170. {
  171. template <class X, class Y>
  172. static inline bool check(X x, Y y_max)
  173. { return x > y_max; }
  174. };
  175. template <>
  176. struct greater_than_type_max<false, true>
  177. {
  178. // What does the standard say about this? I think it's right, and it
  179. // will work with every compiler I know of.
  180. template <class X, class Y>
  181. static inline bool check(X x, Y)
  182. { return x >= 0 && static_cast<X>(static_cast<Y>(x)) != x; }
  183. };
  184. template<>
  185. struct greater_than_type_max<true, false>
  186. {
  187. template <class X, class Y>
  188. static inline bool check(X x, Y y_max)
  189. { return x > y_max; }
  190. };
  191. template <>
  192. struct greater_than_type_max<false, false>
  193. {
  194. // What does the standard say about this? I think it's right, and it
  195. // will work with every compiler I know of.
  196. template <class X, class Y>
  197. static inline bool check(X x, Y)
  198. { return static_cast<X>(static_cast<Y>(x)) != x; }
  199. };
  200. #else // use #pragma hacks if available
  201. namespace detail
  202. {
  203. # if BOOST_MSVC
  204. # pragma warning(push)
  205. # pragma warning(disable : 4018)
  206. # pragma warning(disable : 4146)
  207. #elif defined(__BORLANDC__)
  208. # pragma option push -w-8041
  209. # endif
  210. // Move to namespace boost in utility.hpp?
  211. template <class T>
  212. struct fixed_numeric_limits : public std::numeric_limits<T>
  213. {
  214. static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  215. {
  216. return std::numeric_limits<T>::is_signed && (std::numeric_limits<T>::min)() >= 0
  217. ? T(-(std::numeric_limits<T>::max)()) : (std::numeric_limits<T>::min)();
  218. }
  219. };
  220. # if BOOST_MSVC
  221. # pragma warning(pop)
  222. #elif defined(__BORLANDC__)
  223. # pragma option pop
  224. # endif
  225. } // namespace detail
  226. #endif
  227. template<typename Target, typename Source>
  228. inline Target numeric_cast(Source arg)
  229. {
  230. // typedefs abbreviating respective trait classes
  231. typedef detail::fixed_numeric_limits<Source> arg_traits;
  232. typedef detail::fixed_numeric_limits<Target> result_traits;
  233. #if defined(BOOST_STRICT_CONFIG) \
  234. || (!defined(__HP_aCC) || __HP_aCC > 33900) \
  235. && (!defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) \
  236. || defined(BOOST_SGI_CPP_LIMITS))
  237. // typedefs that act as compile time assertions
  238. // (to be replaced by boost compile time assertions
  239. // as and when they become available and are stable)
  240. typedef bool argument_must_be_numeric[arg_traits::is_specialized];
  241. typedef bool result_must_be_numeric[result_traits::is_specialized];
  242. const bool arg_is_signed = arg_traits::is_signed;
  243. const bool result_is_signed = result_traits::is_signed;
  244. const bool same_sign = arg_is_signed == result_is_signed;
  245. if (less_than_type_min<arg_is_signed, result_is_signed>::check(arg, (result_traits::min)())
  246. || greater_than_type_max<same_sign, arg_is_signed>::check(arg, (result_traits::max)())
  247. )
  248. #else // We need to use #pragma hacks if available
  249. # if BOOST_MSVC
  250. # pragma warning(push)
  251. # pragma warning(disable : 4018)
  252. #elif defined(__BORLANDC__)
  253. #pragma option push -w-8012
  254. # endif
  255. if ((arg < 0 && !result_traits::is_signed) // loss of negative range
  256. || (arg_traits::is_signed && arg < (result_traits::min)()) // underflow
  257. || arg > (result_traits::max)()) // overflow
  258. # if BOOST_MSVC
  259. # pragma warning(pop)
  260. #elif defined(__BORLANDC__)
  261. #pragma option pop
  262. # endif
  263. #endif
  264. {
  265. throw bad_numeric_cast();
  266. }
  267. return static_cast<Target>(arg);
  268. } // numeric_cast
  269. } // namespace boost
  270. #endif // BOOST_OLD_NUMERIC_CAST_HPP