Chris@102: // Copyright Kevlin Henney, 2000-2005. Chris@102: // Copyright Alexander Nasonov, 2006-2010. Chris@102: // Copyright Antony Polukhin, 2011-2014. Chris@102: // Chris@102: // Distributed under the Boost Software License, Version 1.0. (See Chris@102: // accompanying file LICENSE_1_0.txt or copy at Chris@102: // http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: // what: lexical_cast custom keyword cast Chris@102: // who: contributed by Kevlin Henney, Chris@102: // enhanced with contributions from Terje Slettebo, Chris@102: // with additional fixes and suggestions from Gennaro Prota, Chris@102: // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, Chris@102: // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, Chris@102: // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters Chris@102: // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014 Chris@102: Chris@102: #ifndef BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP Chris@102: #define BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP Chris@102: Chris@102: #include Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: #include Chris@102: Chris@102: namespace boost { namespace detail { Chris@102: Chris@102: template Chris@102: struct detect_precision_loss Chris@102: { Chris@102: typedef Source source_type; Chris@102: typedef boost::numeric::Trunc Rounder; Chris@102: typedef BOOST_DEDUCED_TYPENAME mpl::if_< Chris@102: boost::is_arithmetic, Source, Source const& Chris@102: >::type argument_type ; Chris@102: Chris@102: static inline source_type nearbyint(argument_type s, bool& is_ok) BOOST_NOEXCEPT { Chris@102: const source_type near_int = Rounder::nearbyint(s); Chris@102: if (near_int && is_ok) { Chris@102: const source_type orig_div_round = s / near_int; Chris@102: const source_type eps = std::numeric_limits::epsilon(); Chris@102: Chris@102: is_ok = !((orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > eps); Chris@102: } Chris@102: Chris@102: return s; Chris@102: } Chris@102: Chris@102: typedef typename Rounder::round_style round_style; Chris@102: }; Chris@102: Chris@102: template Chris@102: struct fake_precision_loss: public Base Chris@102: { Chris@102: typedef Source source_type ; Chris@102: typedef BOOST_DEDUCED_TYPENAME mpl::if_< Chris@102: boost::is_arithmetic, Source, Source const& Chris@102: >::type argument_type ; Chris@102: Chris@102: static inline source_type nearbyint(argument_type s, bool& /*is_ok*/) BOOST_NOEXCEPT { Chris@102: return s; Chris@102: } Chris@102: }; Chris@102: Chris@102: struct nothrow_overflow_handler Chris@102: { Chris@102: inline bool operator() ( boost::numeric::range_check_result r ) const BOOST_NOEXCEPT { Chris@102: return (r == boost::numeric::cInRange); Chris@102: } Chris@102: }; Chris@102: Chris@102: template Chris@102: inline bool noexcept_numeric_convert(const Source& arg, Target& result) BOOST_NOEXCEPT { Chris@102: typedef boost::numeric::converter< Chris@102: Target, Chris@102: Source, Chris@102: boost::numeric::conversion_traits, Chris@102: nothrow_overflow_handler, Chris@102: detect_precision_loss Chris@102: > converter_orig_t; Chris@102: Chris@102: typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c< Chris@102: boost::is_base_of< detect_precision_loss, converter_orig_t >::value, Chris@102: converter_orig_t, Chris@102: fake_precision_loss Chris@102: >::type converter_t; Chris@102: Chris@102: bool res = nothrow_overflow_handler()(converter_t::out_of_range(arg)); Chris@102: result = converter_t::low_level_convert(converter_t::nearbyint(arg, res)); Chris@102: return res; Chris@102: } Chris@102: Chris@102: template Chris@102: struct lexical_cast_dynamic_num_not_ignoring_minus Chris@102: { Chris@102: static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT { Chris@102: return noexcept_numeric_convert(arg, result); Chris@102: } Chris@102: }; Chris@102: Chris@102: template Chris@102: struct lexical_cast_dynamic_num_ignoring_minus Chris@102: { Chris@102: static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT { Chris@102: typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if_c< Chris@102: boost::is_float::value, Chris@102: boost::mpl::identity, Chris@102: boost::make_unsigned Chris@102: >::type usource_t; Chris@102: Chris@102: if (arg < 0) { Chris@102: const bool res = noexcept_numeric_convert(0u - arg, result); Chris@102: result = static_cast(0u - result); Chris@102: return res; Chris@102: } else { Chris@102: return noexcept_numeric_convert(arg, result); Chris@102: } Chris@102: } Chris@102: }; Chris@102: Chris@102: /* Chris@102: * lexical_cast_dynamic_num follows the rules: Chris@102: * 1) If Source can be converted to Target without precision loss and Chris@102: * without overflows, then assign Source to Target and return Chris@102: * Chris@102: * 2) If Source is less than 0 and Target is an unsigned integer, Chris@102: * then negate Source, check the requirements of rule 1) and if Chris@102: * successful, assign static_casted Source to Target and return Chris@102: * Chris@102: * 3) Otherwise throw a bad_lexical_cast exception Chris@102: * Chris@102: * Chris@102: * Rule 2) required because boost::lexical_cast has the behavior of Chris@102: * stringstream, which uses the rules of scanf for conversions. And Chris@102: * in the C99 standard for unsigned input value minus sign is Chris@102: * optional, so if a negative number is read, no errors will arise Chris@102: * and the result will be the two's complement. Chris@102: */ Chris@102: template Chris@102: struct dynamic_num_converter_impl Chris@102: { Chris@102: static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT { Chris@102: typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c< Chris@102: boost::type_traits::ice_and< Chris@102: boost::is_unsigned::value, Chris@102: boost::type_traits::ice_or< Chris@102: boost::is_signed::value, Chris@102: boost::is_float::value Chris@102: >::value, Chris@102: boost::type_traits::ice_not< Chris@102: boost::is_same::value Chris@102: >::value, Chris@102: boost::type_traits::ice_not< Chris@102: boost::is_same::value Chris@102: >::value Chris@102: >::value, Chris@102: lexical_cast_dynamic_num_ignoring_minus, Chris@102: lexical_cast_dynamic_num_not_ignoring_minus Chris@102: >::type caster_type; Chris@102: Chris@102: return caster_type::try_convert(arg, result); Chris@102: } Chris@102: }; Chris@102: Chris@102: }} // namespace boost::detail Chris@102: Chris@102: #endif // BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP Chris@102: