Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 8 | #ifndef NDNBOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED |
| 9 | #define NDNBOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 10 | |
| 11 | #include <climits> |
| 12 | #include <ios> |
| 13 | #include <limits> |
| 14 | |
| 15 | #include <ndnboost/config.hpp> |
| 16 | #include <ndnboost/integer_traits.hpp> |
| 17 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 18 | #ifndef NDNBOOST_NO_IS_ABSTRACT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 19 | // 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 24 | #if defined(NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \ |
| 25 | (defined(NDNBOOST_MSVC) && (NDNBOOST_MSVC<1310)) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 26 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 27 | #define NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 28 | #endif |
| 29 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 30 | #ifdef NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 31 | #include <ndnboost/assert.hpp> |
| 32 | #else |
| 33 | #include <ndnboost/static_assert.hpp> |
| 34 | #endif |
| 35 | |
| 36 | namespace ndnboost { namespace detail { |
| 37 | |
| 38 | class lcast_abstract_stub {}; |
| 39 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 40 | #ifndef NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 47 | #ifdef NDNBOOST_NO_IS_ABSTRACT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 48 | typedef std::numeric_limits<T> limits; // No fix for SF:1358600. |
| 49 | #else |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 50 | typedef NDNBOOST_DEDUCED_TYPENAME ndnboost::mpl::if_< |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 51 | ndnboost::is_abstract<T> |
| 52 | , std::numeric_limits<lcast_abstract_stub> |
| 53 | , std::numeric_limits<T> |
| 54 | >::type limits; |
| 55 | #endif |
| 56 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 57 | NDNBOOST_STATIC_CONSTANT(bool, use_default_precision = |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 58 | !limits::is_specialized || limits::is_exact |
| 59 | ); |
| 60 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 61 | NDNBOOST_STATIC_CONSTANT(bool, is_specialized_bin = |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 62 | !use_default_precision && |
| 63 | limits::radix == 2 && limits::digits > 0 |
| 64 | ); |
| 65 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 66 | NDNBOOST_STATIC_CONSTANT(bool, is_specialized_dec = |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 67 | !use_default_precision && |
| 68 | limits::radix == 10 && limits::digits10 > 0 |
| 69 | ); |
| 70 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 71 | NDNBOOST_STATIC_CONSTANT(std::streamsize, streamsize_max = |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 72 | ndnboost::integer_traits<std::streamsize>::const_max |
| 73 | ); |
| 74 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 75 | NDNBOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 76 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 77 | NDNBOOST_STATIC_ASSERT(!is_specialized_dec || |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 78 | precision_dec <= streamsize_max + 0UL |
| 79 | ); |
| 80 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 81 | NDNBOOST_STATIC_CONSTANT(unsigned long, precision_bin = |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 82 | 2UL + limits::digits * 30103UL / 100000UL |
| 83 | ); |
| 84 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 85 | NDNBOOST_STATIC_ASSERT(!is_specialized_bin || |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 86 | (limits::digits + 0UL < ULONG_MAX / 30103UL && |
| 87 | precision_bin > limits::digits10 + 0UL && |
| 88 | precision_bin <= streamsize_max + 0UL) |
| 89 | ); |
| 90 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 91 | NDNBOOST_STATIC_CONSTANT(std::streamsize, value = |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 101 | #ifndef NDNBOOST_LCAST_NO_COMPILE_TIME_PRECISION |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 102 | return lcast_precision<T>::value; |
| 103 | #else // Follow lcast_precision algorithm at run-time: |
| 104 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 105 | #ifdef NDNBOOST_NO_IS_ABSTRACT |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 106 | typedef std::numeric_limits<T> limits; // No fix for SF:1358600. |
| 107 | #else |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 108 | typedef NDNBOOST_DEDUCED_TYPENAME ndnboost::mpl::if_< |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 109 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 137 | NDNBOOST_ASSERT( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 138 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 148 | NDNBOOST_ASSERT(precision <= streamsize_max + 0UL); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 149 | 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 | |
| 167 | template<class T> |
| 168 | inline void lcast_set_precision(std::ios_base& stream, T*) |
| 169 | { |
| 170 | stream.precision(lcast_get_precision<T>()); |
| 171 | } |
| 172 | |
| 173 | template<class Source, class Target> |
| 174 | inline 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 183 | #endif // NDNBOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 184 | |