]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/detail/lcast_precision.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / detail / lcast_precision.hpp
1 // Copyright Alexander Nasonov & Paul A. Bristow 2006.
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
9 #define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
10
11 #include <climits>
12 #include <ios>
13 #include <limits>
14
15 #include <boost/config.hpp>
16 #include <boost/integer_traits.hpp>
17
18 #ifndef BOOST_NO_IS_ABSTRACT
19 // Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL
20 #include <boost/mpl/if.hpp>
21 #include <boost/type_traits/is_abstract.hpp>
22 #endif
23
24 #if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \
25 (defined(BOOST_MSVC) && (BOOST_MSVC<1310))
26
27 #define BOOST_LCAST_NO_COMPILE_TIME_PRECISION
28 #endif
29
30 #ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
31 #include <boost/assert.hpp>
32 #else
33 #include <boost/static_assert.hpp>
34 #endif
35
36 namespace boost { namespace detail {
37
38 class lcast_abstract_stub {};
39
40 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
41 // Calculate an argument to pass to std::ios_base::precision from
42 // lexical_cast. See alternative implementation for broken standard
43 // libraries in lcast_get_precision below. Keep them in sync, please.
44 template<class T>
45 struct lcast_precision
46 {
47 #ifdef BOOST_NO_IS_ABSTRACT
48 typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
49 #else
50 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
51 boost::is_abstract<T>
52 , std::numeric_limits<lcast_abstract_stub>
53 , std::numeric_limits<T>
54 >::type limits;
55 #endif
56
57 BOOST_STATIC_CONSTANT(bool, use_default_precision =
58 !limits::is_specialized || limits::is_exact
59 );
60
61 BOOST_STATIC_CONSTANT(bool, is_specialized_bin =
62 !use_default_precision &&
63 limits::radix == 2 && limits::digits > 0
64 );
65
66 BOOST_STATIC_CONSTANT(bool, is_specialized_dec =
67 !use_default_precision &&
68 limits::radix == 10 && limits::digits10 > 0
69 );
70
71 BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
72 boost::integer_traits<std::streamsize>::const_max
73 );
74
75 BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
76
77 BOOST_STATIC_ASSERT(!is_specialized_dec ||
78 precision_dec <= streamsize_max + 0UL
79 );
80
81 BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
82 2UL + limits::digits * 30103UL / 100000UL
83 );
84
85 BOOST_STATIC_ASSERT(!is_specialized_bin ||
86 (limits::digits + 0UL < ULONG_MAX / 30103UL &&
87 precision_bin > limits::digits10 + 0UL &&
88 precision_bin <= streamsize_max + 0UL)
89 );
90
91 BOOST_STATIC_CONSTANT(std::streamsize, value =
92 is_specialized_bin ? precision_bin
93 : is_specialized_dec ? precision_dec : 6
94 );
95 };
96 #endif
97
98 template<class T>
99 inline std::streamsize lcast_get_precision(T* = 0)
100 {
101 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
102 return lcast_precision<T>::value;
103 #else // Follow lcast_precision algorithm at run-time:
104
105 #ifdef BOOST_NO_IS_ABSTRACT
106 typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
107 #else
108 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
109 boost::is_abstract<T>
110 , std::numeric_limits<lcast_abstract_stub>
111 , std::numeric_limits<T>
112 >::type limits;
113 #endif
114
115 bool const use_default_precision =
116 !limits::is_specialized || limits::is_exact;
117
118 if(!use_default_precision)
119 { // Includes all built-in floating-point types, float, double ...
120 // and UDT types for which digits (significand bits) is defined (not zero)
121
122 bool const is_specialized_bin =
123 limits::radix == 2 && limits::digits > 0;
124 bool const is_specialized_dec =
125 limits::radix == 10 && limits::digits10 > 0;
126 std::streamsize const streamsize_max =
127 (boost::integer_traits<std::streamsize>::max)();
128 (void)streamsize_max;
129
130 if(is_specialized_bin)
131 { // Floating-point types with
132 // limits::digits defined by the specialization.
133
134 unsigned long const digits = limits::digits;
135 unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
136 // unsigned long is selected because it is at least 32-bits
137 // and thus ULONG_MAX / 30103UL is big enough for all types.
138 BOOST_ASSERT(
139 digits < ULONG_MAX / 30103UL &&
140 precision > limits::digits10 + 0UL &&
141 precision <= streamsize_max + 0UL
142 );
143 return precision;
144 }
145 else if(is_specialized_dec)
146 { // Decimal Floating-point type, most likely a User Defined Type
147 // rather than a real floating-point hardware type.
148 unsigned int const precision = limits::digits10 + 1U;
149 BOOST_ASSERT(precision <= streamsize_max + 0UL);
150 return precision;
151 }
152 }
153
154 // Integral type (for which precision has no effect)
155 // or type T for which limits is NOT specialized,
156 // so assume stream precision remains the default 6 decimal digits.
157 // Warning: if your User-defined Floating-point type T is NOT specialized,
158 // then you may lose accuracy by only using 6 decimal digits.
159 // To avoid this, you need to specialize T with either
160 // radix == 2 and digits == the number of significand bits,
161 // OR
162 // radix = 10 and digits10 == the number of decimal digits.
163
164 return 6;
165 #endif
166 }
167
168 template<class T>
169 inline void lcast_set_precision(std::ios_base& stream, T*)
170 {
171 stream.precision(lcast_get_precision<T>());
172 }
173
174 template<class Source, class Target>
175 inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
176 {
177 std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
178 std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
179 stream.precision(s > t ? s : t);
180 }
181
182 }}
183
184 #endif // BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
185