1 | // Copyright Kevlin Henney, 2000-2005. |
2 | // Copyright Alexander Nasonov, 2006-2010. |
3 | // Copyright Antony Polukhin, 2011-2020. |
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 | |
18 | #ifndef BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP |
19 | #define BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP |
20 | |
21 | #include <boost/config.hpp> |
22 | #ifdef BOOST_HAS_PRAGMA_ONCE |
23 | # pragma once |
24 | #endif |
25 | |
26 | #include <boost/limits.hpp> |
27 | #include <boost/type_traits/type_identity.hpp> |
28 | #include <boost/type_traits/conditional.hpp> |
29 | #include <boost/type_traits/make_unsigned.hpp> |
30 | #include <boost/type_traits/is_signed.hpp> |
31 | #include <boost/type_traits/is_integral.hpp> |
32 | #include <boost/type_traits/is_arithmetic.hpp> |
33 | #include <boost/type_traits/is_base_of.hpp> |
34 | #include <boost/type_traits/is_float.hpp> |
35 | |
36 | #include <boost/numeric/conversion/cast.hpp> |
37 | |
38 | namespace boost { namespace detail { |
39 | |
40 | template <class Source > |
41 | struct detect_precision_loss |
42 | { |
43 | typedef Source source_type; |
44 | typedef boost::numeric::Trunc<Source> Rounder; |
45 | typedef BOOST_DEDUCED_TYPENAME conditional< |
46 | boost::is_arithmetic<Source>::value, Source, Source const& |
47 | >::type argument_type ; |
48 | |
49 | static inline source_type nearbyint(argument_type s, bool& is_ok) BOOST_NOEXCEPT { |
50 | const source_type near_int = Rounder::nearbyint(s); |
51 | if (near_int && is_ok) { |
52 | const source_type orig_div_round = s / near_int; |
53 | const source_type eps = std::numeric_limits<source_type>::epsilon(); |
54 | |
55 | is_ok = !((orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > eps); |
56 | } |
57 | |
58 | return s; |
59 | } |
60 | |
61 | typedef typename Rounder::round_style round_style; |
62 | }; |
63 | |
64 | template <typename Base, class Source> |
65 | struct fake_precision_loss: public Base |
66 | { |
67 | typedef Source source_type ; |
68 | typedef BOOST_DEDUCED_TYPENAME conditional< |
69 | boost::is_arithmetic<Source>::value, Source, Source const& |
70 | >::type argument_type ; |
71 | |
72 | static inline source_type nearbyint(argument_type s, bool& /*is_ok*/) BOOST_NOEXCEPT { |
73 | return s; |
74 | } |
75 | }; |
76 | |
77 | struct nothrow_overflow_handler |
78 | { |
79 | inline bool operator() ( boost::numeric::range_check_result r ) const BOOST_NOEXCEPT { |
80 | return (r == boost::numeric::cInRange); |
81 | } |
82 | }; |
83 | |
84 | template <typename Target, typename Source> |
85 | inline bool noexcept_numeric_convert(const Source& arg, Target& result) BOOST_NOEXCEPT { |
86 | typedef boost::numeric::converter< |
87 | Target, |
88 | Source, |
89 | boost::numeric::conversion_traits<Target, Source >, |
90 | nothrow_overflow_handler, |
91 | detect_precision_loss<Source > |
92 | > converter_orig_t; |
93 | |
94 | typedef BOOST_DEDUCED_TYPENAME boost::conditional< |
95 | boost::is_base_of< detect_precision_loss<Source >, converter_orig_t >::value, |
96 | converter_orig_t, |
97 | fake_precision_loss<converter_orig_t, Source> |
98 | >::type converter_t; |
99 | |
100 | bool res = nothrow_overflow_handler()(converter_t::out_of_range(arg)); |
101 | result = converter_t::low_level_convert(converter_t::nearbyint(arg, res)); |
102 | return res; |
103 | } |
104 | |
105 | template <typename Target, typename Source> |
106 | struct lexical_cast_dynamic_num_not_ignoring_minus |
107 | { |
108 | static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT { |
109 | return noexcept_numeric_convert<Target, Source >(arg, result); |
110 | } |
111 | }; |
112 | |
113 | template <typename Target, typename Source> |
114 | struct lexical_cast_dynamic_num_ignoring_minus |
115 | { |
116 | static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT { |
117 | typedef BOOST_DEDUCED_TYPENAME boost::conditional< |
118 | boost::is_float<Source>::value, |
119 | boost::type_identity<Source>, |
120 | boost::make_unsigned<Source> |
121 | >::type usource_lazy_t; |
122 | typedef BOOST_DEDUCED_TYPENAME usource_lazy_t::type usource_t; |
123 | |
124 | if (arg < 0) { |
125 | const bool res = noexcept_numeric_convert<Target, usource_t>(0u - arg, result); |
126 | result = static_cast<Target>(0u - result); |
127 | return res; |
128 | } else { |
129 | return noexcept_numeric_convert<Target, usource_t>(arg, result); |
130 | } |
131 | } |
132 | }; |
133 | |
134 | /* |
135 | * lexical_cast_dynamic_num follows the rules: |
136 | * 1) If Source can be converted to Target without precision loss and |
137 | * without overflows, then assign Source to Target and return |
138 | * |
139 | * 2) If Source is less than 0 and Target is an unsigned integer, |
140 | * then negate Source, check the requirements of rule 1) and if |
141 | * successful, assign static_casted Source to Target and return |
142 | * |
143 | * 3) Otherwise throw a bad_lexical_cast exception |
144 | * |
145 | * |
146 | * Rule 2) required because boost::lexical_cast has the behavior of |
147 | * stringstream, which uses the rules of scanf for conversions. And |
148 | * in the C99 standard for unsigned input value minus sign is |
149 | * optional, so if a negative number is read, no errors will arise |
150 | * and the result will be the two's complement. |
151 | */ |
152 | template <typename Target, typename Source> |
153 | struct dynamic_num_converter_impl |
154 | { |
155 | static inline bool try_convert(const Source &arg, Target& result) BOOST_NOEXCEPT { |
156 | typedef BOOST_DEDUCED_TYPENAME boost::conditional< |
157 | boost::is_unsigned<Target>::value && |
158 | (boost::is_signed<Source>::value || boost::is_float<Source>::value) && |
159 | !(boost::is_same<Source, bool>::value) && |
160 | !(boost::is_same<Target, bool>::value), |
161 | lexical_cast_dynamic_num_ignoring_minus<Target, Source>, |
162 | lexical_cast_dynamic_num_not_ignoring_minus<Target, Source> |
163 | >::type caster_type; |
164 | |
165 | return caster_type::try_convert(arg, result); |
166 | } |
167 | }; |
168 | |
169 | }} // namespace boost::detail |
170 | |
171 | #endif // BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP |
172 | |
173 | |