File indexing completed on 2025-09-17 08:25:42
0001
0002
0003
0004
0005
0006
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
0017 namespace boost { namespace detail {
0018
0019
0020
0021 template<class T>
0022 struct lcast_precision
0023 {
0024 using limits = std::numeric_limits<T>;
0025
0026 static constexpr bool use_default_precision =
0027 !limits::is_specialized || limits::is_exact
0028 ;
0029
0030 static constexpr bool is_specialized_bin =
0031 !use_default_precision &&
0032 limits::radix == 2 && limits::digits > 0
0033 ;
0034
0035 static constexpr bool is_specialized_dec =
0036 !use_default_precision &&
0037 limits::radix == 10 && limits::digits10 > 0
0038 ;
0039
0040 static constexpr std::streamsize streamsize_max =
0041 (std::numeric_limits<std::streamsize>::max)()
0042 ;
0043
0044 static constexpr unsigned int precision_dec = limits::digits10 + 1U;
0045
0046 static_assert(!is_specialized_dec ||
0047 precision_dec <= streamsize_max + 0UL
0048 , "");
0049
0050 static constexpr unsigned long precision_bin =
0051 2UL + limits::digits * 30103UL / 100000UL
0052 ;
0053
0054 static_assert(!is_specialized_bin ||
0055 (limits::digits + 0UL < ULONG_MAX / 30103UL &&
0056 precision_bin > limits::digits10 + 0UL &&
0057 precision_bin <= streamsize_max + 0UL)
0058 , "");
0059
0060 static constexpr std::streamsize value =
0061 is_specialized_bin ? precision_bin
0062 : is_specialized_dec ? precision_dec : 6
0063 ;
0064 };
0065
0066 template<class T>
0067 inline void lcast_set_precision(std::ios_base& stream, T*)
0068 {
0069 stream.precision(lcast_precision<T>::value);
0070 }
0071
0072 template<class Source, class Target>
0073 inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
0074 {
0075 std::streamsize const s = lcast_precision<Source>::value;
0076 std::streamsize const t = lcast_precision<Target*>::value;
0077 stream.precision(s > t ? s : t);
0078 }
0079
0080 }}
0081
0082 #endif
0083