blob: 2b0b2aa05a639ef2ce2262229c2545397aed34e9 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -07008#ifndef NDNBOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
9#define NDNBOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070010
11#include <climits>
12#include <ios>
13#include <limits>
14
15#include <ndnboost/config.hpp>
16#include <ndnboost/integer_traits.hpp>
17
Jeff Thompson3d613fd2013-10-15 15:39:04 -070018#ifndef NDNBOOST_NO_IS_ABSTRACT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070019// Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL
20#include <ndnboost/mpl/if.hpp>
21#include <ndnboost/type_traits/is_abstract.hpp>
22#endif
23
Jeff Thompson3d613fd2013-10-15 15:39:04 -070024#if defined(NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \
25 (defined(NDNBOOST_MSVC) && (NDNBOOST_MSVC<1310))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070026
Jeff Thompson3d613fd2013-10-15 15:39:04 -070027#define NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070028#endif
29
Jeff Thompson3d613fd2013-10-15 15:39:04 -070030#ifdef NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070031#include <ndnboost/assert.hpp>
32#else
33#include <ndnboost/static_assert.hpp>
34#endif
35
36namespace ndnboost { namespace detail {
37
38class lcast_abstract_stub {};
39
Jeff Thompson3d613fd2013-10-15 15:39:04 -070040#ifndef NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070041// 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.
44template<class T>
45struct lcast_precision
46{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070047#ifdef NDNBOOST_NO_IS_ABSTRACT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070048 typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
49#else
Jeff Thompson3d613fd2013-10-15 15:39:04 -070050 typedef NDNBOOST_DEDUCED_TYPENAME ndnboost::mpl::if_<
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070051 ndnboost::is_abstract<T>
52 , std::numeric_limits<lcast_abstract_stub>
53 , std::numeric_limits<T>
54 >::type limits;
55#endif
56
Jeff Thompson3d613fd2013-10-15 15:39:04 -070057 NDNBOOST_STATIC_CONSTANT(bool, use_default_precision =
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070058 !limits::is_specialized || limits::is_exact
59 );
60
Jeff Thompson3d613fd2013-10-15 15:39:04 -070061 NDNBOOST_STATIC_CONSTANT(bool, is_specialized_bin =
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070062 !use_default_precision &&
63 limits::radix == 2 && limits::digits > 0
64 );
65
Jeff Thompson3d613fd2013-10-15 15:39:04 -070066 NDNBOOST_STATIC_CONSTANT(bool, is_specialized_dec =
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070067 !use_default_precision &&
68 limits::radix == 10 && limits::digits10 > 0
69 );
70
Jeff Thompson3d613fd2013-10-15 15:39:04 -070071 NDNBOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070072 ndnboost::integer_traits<std::streamsize>::const_max
73 );
74
Jeff Thompson3d613fd2013-10-15 15:39:04 -070075 NDNBOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070076
Jeff Thompson3d613fd2013-10-15 15:39:04 -070077 NDNBOOST_STATIC_ASSERT(!is_specialized_dec ||
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070078 precision_dec <= streamsize_max + 0UL
79 );
80
Jeff Thompson3d613fd2013-10-15 15:39:04 -070081 NDNBOOST_STATIC_CONSTANT(unsigned long, precision_bin =
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070082 2UL + limits::digits * 30103UL / 100000UL
83 );
84
Jeff Thompson3d613fd2013-10-15 15:39:04 -070085 NDNBOOST_STATIC_ASSERT(!is_specialized_bin ||
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070086 (limits::digits + 0UL < ULONG_MAX / 30103UL &&
87 precision_bin > limits::digits10 + 0UL &&
88 precision_bin <= streamsize_max + 0UL)
89 );
90
Jeff Thompson3d613fd2013-10-15 15:39:04 -070091 NDNBOOST_STATIC_CONSTANT(std::streamsize, value =
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070092 is_specialized_bin ? precision_bin
93 : is_specialized_dec ? precision_dec : 6
94 );
95};
96#endif
97
98template<class T>
99inline std::streamsize lcast_get_precision(T* = 0)
100{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700101#ifndef NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700102 return lcast_precision<T>::value;
103#else // Follow lcast_precision algorithm at run-time:
104
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700105#ifdef NDNBOOST_NO_IS_ABSTRACT
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700106 typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
107#else
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700108 typedef NDNBOOST_DEDUCED_TYPENAME ndnboost::mpl::if_<
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700109 ndnboost::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 (ndnboost::integer_traits<std::streamsize>::max)();
128
129 if(is_specialized_bin)
130 { // Floating-point types with
131 // limits::digits defined by the specialization.
132
133 unsigned long const digits = limits::digits;
134 unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
135 // unsigned long is selected because it is at least 32-bits
136 // and thus ULONG_MAX / 30103UL is big enough for all types.
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700137 NDNBOOST_ASSERT(
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700138 digits < ULONG_MAX / 30103UL &&
139 precision > limits::digits10 + 0UL &&
140 precision <= streamsize_max + 0UL
141 );
142 return precision;
143 }
144 else if(is_specialized_dec)
145 { // Decimal Floating-point type, most likely a User Defined Type
146 // rather than a real floating-point hardware type.
147 unsigned int const precision = limits::digits10 + 1U;
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700148 NDNBOOST_ASSERT(precision <= streamsize_max + 0UL);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700149 return precision;
150 }
151 }
152
153 // Integral type (for which precision has no effect)
154 // or type T for which limits is NOT specialized,
155 // so assume stream precision remains the default 6 decimal digits.
156 // Warning: if your User-defined Floating-point type T is NOT specialized,
157 // then you may lose accuracy by only using 6 decimal digits.
158 // To avoid this, you need to specialize T with either
159 // radix == 2 and digits == the number of significand bits,
160 // OR
161 // radix = 10 and digits10 == the number of decimal digits.
162
163 return 6;
164#endif
165}
166
167template<class T>
168inline void lcast_set_precision(std::ios_base& stream, T*)
169{
170 stream.precision(lcast_get_precision<T>());
171}
172
173template<class Source, class Target>
174inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
175{
176 std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
177 std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
178 stream.precision(s > t ? s : t);
179}
180
181}}
182
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700183#endif // NDNBOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700184