Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:09:52

0001 // Copyright Alexander Nasonov & Paul A. Bristow 2006.
0002 
0003 // Use, modification and distribution are subject to the
0004 // Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE_1_0.txt
0006 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
0009 #define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
0010 
0011 #include <climits>
0012 #include <ios>
0013 #include <limits>
0014 
0015 #include <boost/config.hpp>
0016 #include <boost/integer_traits.hpp>
0017 
0018 namespace boost { namespace detail {
0019 
0020 class lcast_abstract_stub {};
0021 
0022 // Calculate an argument to pass to std::ios_base::precision from
0023 // lexical_cast. See alternative implementation for broken standard
0024 // libraries in lcast_get_precision below. Keep them in sync, please.
0025 template<class T>
0026 struct lcast_precision
0027 {
0028     using limits = std::numeric_limits<T>;
0029 
0030     BOOST_STATIC_CONSTANT(bool, use_default_precision =
0031             !limits::is_specialized || limits::is_exact
0032         );
0033 
0034     BOOST_STATIC_CONSTANT(bool, is_specialized_bin =
0035             !use_default_precision &&
0036             limits::radix == 2 && limits::digits > 0
0037         );
0038 
0039     BOOST_STATIC_CONSTANT(bool, is_specialized_dec =
0040             !use_default_precision &&
0041             limits::radix == 10 && limits::digits10 > 0
0042         );
0043 
0044     BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
0045             boost::integer_traits<std::streamsize>::const_max
0046         );
0047 
0048     BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
0049 
0050     static_assert(!is_specialized_dec ||
0051             precision_dec <= streamsize_max + 0UL
0052         , "");
0053 
0054     BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
0055             2UL + limits::digits * 30103UL / 100000UL
0056         );
0057 
0058     static_assert(!is_specialized_bin ||
0059             (limits::digits + 0UL < ULONG_MAX / 30103UL &&
0060             precision_bin > limits::digits10 + 0UL &&
0061             precision_bin <= streamsize_max + 0UL)
0062         , "");
0063 
0064     BOOST_STATIC_CONSTANT(std::streamsize, value =
0065             is_specialized_bin ? precision_bin
0066                                : is_specialized_dec ? precision_dec : 6
0067         );
0068 };
0069 
0070 
0071 template<class T>
0072 inline std::streamsize lcast_get_precision(T* = 0)
0073 {
0074     return lcast_precision<T>::value;
0075 }
0076 
0077 template<class T>
0078 inline void lcast_set_precision(std::ios_base& stream, T*)
0079 {
0080     stream.precision(lcast_get_precision<T>());
0081 }
0082 
0083 template<class Source, class Target>
0084 inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
0085 {
0086     std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
0087     std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
0088     stream.precision(s > t ? s : t);
0089 }
0090 
0091 }}
0092 
0093 #endif //  BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
0094