converter_numeric.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2016.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // what: lexical_cast custom keyword cast
  10. // who: contributed by Kevlin Henney,
  11. // enhanced with contributions from Terje Slettebo,
  12. // with additional fixes and suggestions from Gennaro Prota,
  13. // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
  14. // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
  15. // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters
  16. // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2016
  17. #ifndef BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP
  18. #define BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #include <boost/limits.hpp>
  24. #include <boost/mpl/eval_if.hpp>
  25. #include <boost/mpl/identity.hpp>
  26. #include <boost/mpl/if.hpp>
  27. #include <boost/type_traits/make_unsigned.hpp>
  28. #include <boost/type_traits/is_signed.hpp>
  29. #include <boost/type_traits/is_integral.hpp>
  30. #include <boost/type_traits/is_arithmetic.hpp>
  31. #include <boost/type_traits/is_base_of.hpp>
  32. #include <boost/type_traits/is_float.hpp>
  33. #include <boost/numeric/conversion/cast.hpp>
  34. namespace boost { namespace detail {
  35. template <class Source >
  36. struct detect_precision_loss
  37. {
  38. typedef Source source_type;
  39. typedef boost::numeric::Trunc<Source> Rounder;
  40. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  41. boost::is_arithmetic<Source>, Source, Source const&
  42. >::type argument_type ;
  43. static inline source_type nearbyint(argument_type s, bool& is_ok) BOOST_NOEXCEPT {
  44. const source_type near_int = Rounder::nearbyint(s);
  45. if (near_int && is_ok) {
  46. const source_type orig_div_round = s / near_int;
  47. const source_type eps = std::numeric_limits<source_type>::epsilon();
  48. is_ok = !((orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > eps);
  49. }
  50. return s;
  51. }
  52. typedef typename Rounder::round_style round_style;
  53. };
  54. template <typename Base, class Source>
  55. struct fake_precision_loss: public Base
  56. {
  57. typedef Source source_type ;
  58. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  59. boost::is_arithmetic<Source>, Source, Source const&
  60. >::type argument_type ;
  61. static inline source_type nearbyint(argument_type s, bool& /*is_ok*/) BOOST_NOEXCEPT {
  62. return s;
  63. }
  64. };
  65. struct nothrow_overflow_handler
  66. {
  67. inline bool operator() ( boost::numeric::range_check_result r ) const BOOST_NOEXCEPT {
  68. return (r == boost::numeric::cInRange);
  69. }
  70. };
  71. template <typename Target, typename Source>
  72. inline bool noexcept_numeric_convert(const Source& arg, Target& result) BOOST_NOEXCEPT {
  73. typedef boost::numeric::converter<
  74. Target,
  75. Source,
  76. boost::numeric::conversion_traits<Target, Source >,
  77. nothrow_overflow_handler,
  78. detect_precision_loss<Source >
  79. > converter_orig_t;
  80. typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
  81. boost::is_base_of< detect_precision_loss<Source >, converter_orig_t >::value,
  82. converter_orig_t,
  83. fake_precision_loss<converter_orig_t, Source>
  84. >::type converter_t;
  85. bool res = nothrow_overflow_handler()(converter_t::out_of_range(arg));
  86. result = converter_t::low_level_convert(converter_t::nearbyint(arg, res));
  87. return res;
  88. }
  89. template <typename Target, typename Source>
  90. struct lexical_cast_dynamic_num_not_ignoring_minus
  91. {
  92. static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT {
  93. return noexcept_numeric_convert<Target, Source >(arg, result);
  94. }
  95. };
  96. template <typename Target, typename Source>
  97. struct lexical_cast_dynamic_num_ignoring_minus
  98. {
  99. static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT {
  100. typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if_c<
  101. boost::is_float<Source>::value,
  102. boost::mpl::identity<Source>,
  103. boost::make_unsigned<Source>
  104. >::type usource_t;
  105. if (arg < 0) {
  106. const bool res = noexcept_numeric_convert<Target, usource_t>(0u - arg, result);
  107. result = static_cast<Target>(0u - result);
  108. return res;
  109. } else {
  110. return noexcept_numeric_convert<Target, usource_t>(arg, result);
  111. }
  112. }
  113. };
  114. /*
  115. * lexical_cast_dynamic_num follows the rules:
  116. * 1) If Source can be converted to Target without precision loss and
  117. * without overflows, then assign Source to Target and return
  118. *
  119. * 2) If Source is less than 0 and Target is an unsigned integer,
  120. * then negate Source, check the requirements of rule 1) and if
  121. * successful, assign static_casted Source to Target and return
  122. *
  123. * 3) Otherwise throw a bad_lexical_cast exception
  124. *
  125. *
  126. * Rule 2) required because boost::lexical_cast has the behavior of
  127. * stringstream, which uses the rules of scanf for conversions. And
  128. * in the C99 standard for unsigned input value minus sign is
  129. * optional, so if a negative number is read, no errors will arise
  130. * and the result will be the two's complement.
  131. */
  132. template <typename Target, typename Source>
  133. struct dynamic_num_converter_impl
  134. {
  135. static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT {
  136. typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
  137. boost::is_unsigned<Target>::value &&
  138. (boost::is_signed<Source>::value || boost::is_float<Source>::value) &&
  139. !(boost::is_same<Source, bool>::value) &&
  140. !(boost::is_same<Target, bool>::value),
  141. lexical_cast_dynamic_num_ignoring_minus<Target, Source>,
  142. lexical_cast_dynamic_num_not_ignoring_minus<Target, Source>
  143. >::type caster_type;
  144. return caster_type::try_convert(arg, result);
  145. }
  146. };
  147. }} // namespace boost::detail
  148. #endif // BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP