Chris@16: // boost cast.hpp header file ----------------------------------------------// Chris@16: Chris@16: // (C) Copyright Kevlin Henney and Dave Abrahams 1999. Chris@16: // Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/conversion for Documentation. Chris@16: Chris@16: // Revision History Chris@16: // 23 JUN 05 Code extracted from /boost/cast.hpp into this new header. Chris@16: // Keeps this legacy version of numeric_cast<> for old compilers Chris@16: // wich can't compile the new version in /boost/numeric/conversion/cast.hpp Chris@16: // (Fernando Cacciola) Chris@16: // 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included Chris@16: // instead (the workaround did not Chris@16: // actually compile when BOOST_NO_LIMITS was defined in Chris@16: // any case, so we loose nothing). (John Maddock) Chris@16: // 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never Chris@16: // worked with stock GCC; trying to get it to do that broke Chris@16: // vc-stlport. Chris@16: // 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp. Chris@16: // Removed unused BOOST_EXPLICIT_TARGET macro. Moved Chris@16: // boost::detail::type to boost/type.hpp. Made it compile with Chris@16: // stock gcc again (Dave Abrahams) Chris@16: // 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal Chris@16: // Review (Beman Dawes) Chris@16: // 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams) Chris@16: // 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC Chris@16: // (Dave Abrahams) Chris@16: // 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams) Chris@16: // 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes) Chris@16: // 27 Jun 00 More MSVC6 workarounds Chris@16: // 15 Jun 00 Add workarounds for MSVC6 Chris@16: // 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov) Chris@16: // 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar) Chris@16: // 29 Dec 99 Change using declarations so usages in other namespaces work Chris@16: // correctly (Dave Abrahams) Chris@16: // 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors Chris@16: // as suggested Darin Adler and improved by Valentin Bonnard. Chris@16: // 2 Sep 99 Remove controversial asserts, simplify, rename. Chris@16: // 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast, Chris@16: // place in nested namespace. Chris@16: // 3 Aug 99 Initial version Chris@16: Chris@16: #ifndef BOOST_OLD_NUMERIC_CAST_HPP Chris@16: #define BOOST_OLD_NUMERIC_CAST_HPP Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: // It has been demonstrated numerous times that MSVC 6.0 fails silently at link Chris@16: // time if you use a template function which has template parameters that don't Chris@16: // appear in the function's argument list. Chris@16: // Chris@16: // TODO: Add this to config.hpp? Chris@16: // FLC: This macro is repeated in boost/cast.hpp but only locally (is undefined at the bottom) Chris@16: // so is OK to reproduce it here. Chris@16: # if defined(BOOST_MSVC) && BOOST_MSVC < 1300 Chris@16: # define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type* = 0 Chris@16: # else Chris@16: # define BOOST_EXPLICIT_DEFAULT_TARGET Chris@16: # endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: using numeric::bad_numeric_cast; Chris@16: Chris@16: // LEGACY numeric_cast [only for some old broken compilers] --------------------------------------// Chris@16: Chris@16: // Contributed by Kevlin Henney Chris@16: Chris@16: // numeric_cast ------------------------------------------------------------// Chris@16: Chris@16: #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_SGI_CPP_LIMITS) Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct signed_numeric_limits : std::numeric_limits Chris@16: { Chris@16: static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { Chris@16: return (std::numeric_limits::min)() >= 0 Chris@16: // unary minus causes integral promotion, thus the static_cast<> Chris@16: ? static_cast(-(std::numeric_limits::max)()) Chris@16: : (std::numeric_limits::min)(); Chris@16: }; Chris@16: }; Chris@16: Chris@16: // Move to namespace boost in utility.hpp? Chris@16: template Chris@16: struct fixed_numeric_limits_base Chris@16: : public if_true< std::numeric_limits::is_signed > Chris@16: ::BOOST_NESTED_TEMPLATE then< signed_numeric_limits, Chris@16: std::numeric_limits Chris@16: >::type Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct fixed_numeric_limits Chris@16: : fixed_numeric_limits_base::is_specialized)> Chris@16: {}; Chris@16: Chris@16: # ifdef BOOST_HAS_LONG_LONG Chris@16: // cover implementations which supply no specialization for long Chris@16: // long / unsigned long long. Not intended to be full Chris@16: // numeric_limits replacements, but good enough for numeric_cast<> Chris@16: template <> Chris@16: struct fixed_numeric_limits_base< ::boost::long_long_type, false> Chris@16: { Chris@16: BOOST_STATIC_CONSTANT(bool, is_specialized = true); Chris@16: BOOST_STATIC_CONSTANT(bool, is_signed = true); Chris@16: static ::boost::long_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { Chris@16: # ifdef LONGLONG_MAX Chris@16: return LONGLONG_MAX; Chris@16: # else Chris@16: return 9223372036854775807LL; // hope this is portable Chris@16: # endif Chris@16: } Chris@16: Chris@16: static ::boost::long_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { Chris@16: # ifdef LONGLONG_MIN Chris@16: return LONGLONG_MIN; Chris@16: # else Chris@16: return -( 9223372036854775807LL )-1; // hope this is portable Chris@16: # endif Chris@16: } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct fixed_numeric_limits_base< ::boost::ulong_long_type, false> Chris@16: { Chris@16: BOOST_STATIC_CONSTANT(bool, is_specialized = true); Chris@16: BOOST_STATIC_CONSTANT(bool, is_signed = false); Chris@16: static ::boost::ulong_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { Chris@16: # ifdef ULONGLONG_MAX Chris@16: return ULONGLONG_MAX; Chris@16: # else Chris@16: return 0xffffffffffffffffULL; // hope this is portable Chris@16: # endif Chris@16: } Chris@16: Chris@16: static ::boost::ulong_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; } Chris@16: }; Chris@16: # endif Chris@16: } // namespace detail Chris@16: Chris@16: // less_than_type_min - Chris@16: // x_is_signed should be numeric_limits::is_signed Chris@16: // y_is_signed should be numeric_limits::is_signed Chris@16: // y_min should be numeric_limits::min() Chris@16: // Chris@16: // check(x, y_min) returns true iff x < y_min without invoking comparisons Chris@16: // between signed and unsigned values. Chris@16: // Chris@16: // "poor man's partial specialization" is in use here. Chris@16: template Chris@16: struct less_than_type_min Chris@16: { Chris@16: template Chris@16: static bool check(X x, Y y_min) Chris@16: { return x < y_min; } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct less_than_type_min Chris@16: { Chris@16: template Chris@16: static bool check(X, Y) Chris@16: { return false; } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct less_than_type_min Chris@16: { Chris@16: template Chris@16: static bool check(X x, Y) Chris@16: { return x < 0; } Chris@16: }; Chris@16: Chris@16: // greater_than_type_max - Chris@16: // same_sign should be: Chris@16: // numeric_limits::is_signed == numeric_limits::is_signed Chris@16: // y_max should be numeric_limits::max() Chris@16: // Chris@16: // check(x, y_max) returns true iff x > y_max without invoking comparisons Chris@16: // between signed and unsigned values. Chris@16: // Chris@16: // "poor man's partial specialization" is in use here. Chris@16: template Chris@16: struct greater_than_type_max; Chris@16: Chris@16: template<> Chris@16: struct greater_than_type_max Chris@16: { Chris@16: template Chris@16: static inline bool check(X x, Y y_max) Chris@16: { return x > y_max; } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct greater_than_type_max Chris@16: { Chris@16: // What does the standard say about this? I think it's right, and it Chris@16: // will work with every compiler I know of. Chris@16: template Chris@16: static inline bool check(X x, Y) Chris@16: { return x >= 0 && static_cast(static_cast(x)) != x; } Chris@16: Chris@16: # if defined(BOOST_MSVC) && BOOST_MSVC < 1300 Chris@16: // MSVC6 can't static_cast unsigned __int64 -> floating types Chris@16: # define BOOST_UINT64_CAST(src_type) \ Chris@16: static inline bool check(src_type x, unsigned __int64) \ Chris@16: { \ Chris@16: if (x < 0) return false; \ Chris@16: unsigned __int64 y = static_cast(x); \ Chris@16: bool odd = y & 0x1; \ Chris@16: __int64 div2 = static_cast<__int64>(y >> 1); \ Chris@16: return ((static_cast(div2) * 2.0) + odd) != x; \ Chris@16: } Chris@16: Chris@16: BOOST_UINT64_CAST(long double); Chris@16: BOOST_UINT64_CAST(double); Chris@16: BOOST_UINT64_CAST(float); Chris@16: # undef BOOST_UINT64_CAST Chris@16: # endif Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct greater_than_type_max Chris@16: { Chris@16: template Chris@16: static inline bool check(X x, Y y_max) Chris@16: { return x > y_max; } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct greater_than_type_max Chris@16: { Chris@16: // What does the standard say about this? I think it's right, and it Chris@16: // will work with every compiler I know of. Chris@16: template Chris@16: static inline bool check(X x, Y) Chris@16: { return static_cast(static_cast(x)) != x; } Chris@16: }; Chris@16: Chris@16: #else // use #pragma hacks if available Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: # if BOOST_MSVC Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable : 4018) Chris@16: # pragma warning(disable : 4146) Chris@16: #elif defined(__BORLANDC__) Chris@16: # pragma option push -w-8041 Chris@16: # endif Chris@16: Chris@16: // Move to namespace boost in utility.hpp? Chris@16: template Chris@16: struct fixed_numeric_limits : public std::numeric_limits Chris@16: { Chris@16: static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { Chris@16: return std::numeric_limits::is_signed && (std::numeric_limits::min)() >= 0 Chris@16: ? T(-(std::numeric_limits::max)()) : (std::numeric_limits::min)(); Chris@16: } Chris@16: }; Chris@16: Chris@16: # if BOOST_MSVC Chris@16: # pragma warning(pop) Chris@16: #elif defined(__BORLANDC__) Chris@16: # pragma option pop Chris@16: # endif Chris@16: } // namespace detail Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: inline Target numeric_cast(Source arg BOOST_EXPLICIT_DEFAULT_TARGET) Chris@16: { Chris@16: // typedefs abbreviating respective trait classes Chris@16: typedef detail::fixed_numeric_limits arg_traits; Chris@16: typedef detail::fixed_numeric_limits result_traits; Chris@16: Chris@16: #if defined(BOOST_STRICT_CONFIG) \ Chris@16: || (!defined(__HP_aCC) || __HP_aCC > 33900) \ Chris@16: && (!defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) \ Chris@16: || defined(BOOST_SGI_CPP_LIMITS)) Chris@16: // typedefs that act as compile time assertions Chris@16: // (to be replaced by boost compile time assertions Chris@16: // as and when they become available and are stable) Chris@16: typedef bool argument_must_be_numeric[arg_traits::is_specialized]; Chris@16: typedef bool result_must_be_numeric[result_traits::is_specialized]; Chris@16: Chris@16: const bool arg_is_signed = arg_traits::is_signed; Chris@16: const bool result_is_signed = result_traits::is_signed; Chris@16: const bool same_sign = arg_is_signed == result_is_signed; Chris@16: Chris@16: if (less_than_type_min::check(arg, (result_traits::min)()) Chris@16: || greater_than_type_max::check(arg, (result_traits::max)()) Chris@16: ) Chris@16: Chris@16: #else // We need to use #pragma hacks if available Chris@16: Chris@16: # if BOOST_MSVC Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable : 4018) Chris@16: #elif defined(__BORLANDC__) Chris@16: #pragma option push -w-8012 Chris@16: # endif Chris@16: if ((arg < 0 && !result_traits::is_signed) // loss of negative range Chris@16: || (arg_traits::is_signed && arg < (result_traits::min)()) // underflow Chris@16: || arg > (result_traits::max)()) // overflow Chris@16: # if BOOST_MSVC Chris@16: # pragma warning(pop) Chris@16: #elif defined(__BORLANDC__) Chris@16: #pragma option pop Chris@16: # endif Chris@16: #endif Chris@16: { Chris@16: throw bad_numeric_cast(); Chris@16: } Chris@16: return static_cast(arg); Chris@16: } // numeric_cast Chris@16: Chris@16: # undef BOOST_EXPLICIT_DEFAULT_TARGET Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_OLD_NUMERIC_CAST_HPP