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