Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
00009 #define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
00010
00011 #include <climits>
00012 #include <ios>
00013 #include <limits>
00014
00015 #include <boost/config.hpp>
00016 #include <boost/integer_traits.hpp>
00017
00018 #ifndef BOOST_NO_IS_ABSTRACT
00019
00020 #include <boost/mpl/if.hpp>
00021 #include <boost/type_traits/is_abstract.hpp>
00022 #endif
00023
00024 #if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \
00025 (defined(BOOST_MSVC) && (BOOST_MSVC<1310))
00026
00027 #define BOOST_LCAST_NO_COMPILE_TIME_PRECISION
00028 #endif
00029
00030 #ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
00031 #include <boost/assert.hpp>
00032 #else
00033 #include <boost/static_assert.hpp>
00034 #endif
00035
00036 namespace boost { namespace detail {
00037
00038 class lcast_abstract_stub {};
00039
00040 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
00041
00042
00043
00044 template<class T>
00045 struct lcast_precision
00046 {
00047 #ifdef BOOST_NO_IS_ABSTRACT
00048 typedef std::numeric_limits<T> limits;
00049 #else
00050 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
00051 boost::is_abstract<T>
00052 , std::numeric_limits<lcast_abstract_stub>
00053 , std::numeric_limits<T>
00054 >::type limits;
00055 #endif
00056
00057 BOOST_STATIC_CONSTANT(bool, use_default_precision =
00058 !limits::is_specialized || limits::is_exact
00059 );
00060
00061 BOOST_STATIC_CONSTANT(bool, is_specialized_bin =
00062 !use_default_precision &&
00063 limits::radix == 2 && limits::digits > 0
00064 );
00065
00066 BOOST_STATIC_CONSTANT(bool, is_specialized_dec =
00067 !use_default_precision &&
00068 limits::radix == 10 && limits::digits10 > 0
00069 );
00070
00071 BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
00072 boost::integer_traits<std::streamsize>::const_max
00073 );
00074
00075 BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
00076
00077 BOOST_STATIC_ASSERT(!is_specialized_dec ||
00078 precision_dec <= streamsize_max + 0UL
00079 );
00080
00081 BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
00082 2UL + limits::digits * 30103UL / 100000UL
00083 );
00084
00085 BOOST_STATIC_ASSERT(!is_specialized_bin ||
00086 (limits::digits + 0UL < ULONG_MAX / 30103UL &&
00087 precision_bin > limits::digits10 + 0UL &&
00088 precision_bin <= streamsize_max + 0UL)
00089 );
00090
00091 BOOST_STATIC_CONSTANT(std::streamsize, value =
00092 is_specialized_bin ? precision_bin
00093 : is_specialized_dec ? precision_dec : 6
00094 );
00095 };
00096 #endif
00097
00098 template<class T>
00099 inline std::streamsize lcast_get_precision(T* = 0)
00100 {
00101 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
00102 return lcast_precision<T>::value;
00103 #else // Follow lcast_precision algorithm at run-time:
00104
00105 #ifdef BOOST_NO_IS_ABSTRACT
00106 typedef std::numeric_limits<T> limits;
00107 #else
00108 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
00109 boost::is_abstract<T>
00110 , std::numeric_limits<lcast_abstract_stub>
00111 , std::numeric_limits<T>
00112 >::type limits;
00113 #endif
00114
00115 bool const use_default_precision =
00116 !limits::is_specialized || limits::is_exact;
00117
00118 if(!use_default_precision)
00119 {
00120
00121
00122 bool const is_specialized_bin =
00123 limits::radix == 2 && limits::digits > 0;
00124 bool const is_specialized_dec =
00125 limits::radix == 10 && limits::digits10 > 0;
00126 std::streamsize const streamsize_max =
00127 (boost::integer_traits<std::streamsize>::max)();
00128
00129 if(is_specialized_bin)
00130 {
00131
00132
00133 unsigned long const digits = limits::digits;
00134 unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
00135
00136
00137 BOOST_ASSERT(
00138 digits < ULONG_MAX / 30103UL &&
00139 precision > limits::digits10 + 0UL &&
00140 precision <= streamsize_max + 0UL
00141 );
00142 return precision;
00143 }
00144 else if(is_specialized_dec)
00145 {
00146
00147 unsigned int const precision = limits::digits10 + 1U;
00148 BOOST_ASSERT(precision <= streamsize_max + 0UL);
00149 return precision;
00150 }
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 return 6;
00164 #endif
00165 }
00166
00167 template<class T>
00168 inline void lcast_set_precision(std::ios_base& stream, T*)
00169 {
00170 stream.precision(lcast_get_precision<T>());
00171 }
00172
00173 template<class Source, class Target>
00174 inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
00175 {
00176 std::streamsize const s = lcast_get_precision((Source*)0);
00177 std::streamsize const t = lcast_get_precision((Target*)0);
00178 stream.precision(s > t ? s : t);
00179 }
00180
00181 }}
00182
00183 #endif // BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
00184