lcast_precision.hpp
Go to the documentation of this file.
00001 // Copyright Alexander Nasonov & Paul A. Bristow 2006.
00002 
00003 // Use, modification and distribution are subject to the
00004 // Boost Software License, Version 1.0.
00005 // (See accompanying file LICENSE_1_0.txt
00006 // or copy at http://www.boost.org/LICENSE_1_0.txt)
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 // Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL
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 // Calculate an argument to pass to std::ios_base::precision from
00042 // lexical_cast. See alternative implementation for broken standard
00043 // libraries in lcast_get_precision below. Keep them in sync, please.
00044 template<class T>
00045 struct lcast_precision
00046 {
00047 #ifdef BOOST_NO_IS_ABSTRACT
00048     typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
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; // No fix for SF:1358600.
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     { // Includes all built-in floating-point types, float, double ...
00120       // and UDT types for which digits (significand bits) is defined (not zero)
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         { // Floating-point types with
00131           // limits::digits defined by the specialization.
00132 
00133             unsigned long const digits = limits::digits;
00134             unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
00135             // unsigned long is selected because it is at least 32-bits
00136             // and thus ULONG_MAX / 30103UL is big enough for all types.
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         {   // Decimal Floating-point type, most likely a User Defined Type
00146             // rather than a real floating-point hardware type.
00147             unsigned int const precision = limits::digits10 + 1U;
00148             BOOST_ASSERT(precision <= streamsize_max + 0UL);
00149             return precision;
00150         }
00151     }
00152 
00153     // Integral type (for which precision has no effect)
00154     // or type T for which limits is NOT specialized,
00155     // so assume stream precision remains the default 6 decimal digits.
00156     // Warning: if your User-defined Floating-point type T is NOT specialized,
00157     // then you may lose accuracy by only using 6 decimal digits.
00158     // To avoid this, you need to specialize T with either
00159     // radix == 2 and digits == the number of significand bits,
00160     // OR
00161     // radix = 10 and digits10 == the number of decimal digits.
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 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29