annotate DEPENDENCIES/generic/include/boost/numeric/conversion/detail/old_numeric_cast.hpp @ 133:4acb5d8d80b6 tip

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