ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
bytesize.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of sub-namespace #alib::lang::format of module \alib_basecamp of
4 * the \aliblong.
5 *
6 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
7 * Published under \ref mainpage_license "Boost Software License".
8 **************************************************************************************************/
9#ifndef HPP_ALIB_LANG_FORMAT_BYTESIZE
10#define HPP_ALIB_LANG_FORMAT_BYTESIZE 1
11
12#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
13# include "alib/alib.hpp"
14#endif
15
17
18#if !defined (HPP_ALIB_LANG_FORMAT_EXCEPTIONS)
20#endif
21
22#if !defined (HPP_ALIB_ENUMS_ARITHMETICAL)
24#endif
25#if !defined (HPP_ALIB_ENUMS_BITWISE)
26 #include "alib/enums/bitwise.hpp"
27#endif
28#if !defined (HPP_ALIB_ENUMS_ITERABLE)
30#endif
31#if !defined (HPP_ALIB_ENUMS_RECORDS)
32# include "alib/enums/records.hpp"
33#endif
34#if !defined(HPP_ALIB_ENUMS_SERIALIZATION)
36#endif
37
38namespace alib { namespace lang::format {
39
40/**
41 * Enumeration of byte-size units to output with types \alib{lang::format;ByteSizeIEC} and
42 * \alib{lang::format;ByteSizeSI}.<br>
43 * The unit symbols are read from the \ref alib_basecamp_resources "resources" of module class
44 * \alib{lang::basecamp;BaseCamp}. The list is given with resource name <b>BS</b>.
45*/
46enum ByteSizeUnits : uint8_t
47{
48 IEC= 0, ///< Begin of SI units.
49 B= 0, ///< 2^ 0, Bbyte , factor 1.
50 KiB= 1, ///< 2^ 10, Kibibyte , factor 1024.
51 MiB= 2, ///< 2^ 20, Mebibyte , factor 1048576.
52 GiB= 3, ///< 2^ 30, Gibibyte , factor 1073741824.
53 TiB= 4, ///< 2^ 40, Tebibyte , factor 1099511627776.
54 PiB= 5, ///< 2^ 50, Pebibyte , factor 1125899906842624.
55 EiB= 6, ///< 2^ 60, Exbibyte , factor 1152921504606846976.
56 ZiB= 7, ///< 2^ 70, Zebibyte , factor 1180591620717411303424.
57 YiB= 8, ///< 2^ 80, Yobibyte , factor 1208925819614629174706176.
58 RiB= 9, ///< 2^ 90, Robibyte , factor 1237940039285380274899124224.
59 QiB= 10, ///< 2^100, Quebibyte , factor 1267650600228229401496703205376.
60 IEC_END= 11, ///< End of SI units
61
62 SI= 11, ///< Begin of IEC units.
63 B_SI= 11, ///< 2^ 0, Byte , factor 1.
64 kB= 12, ///< 10^ 3, Kilobyte , factor 100.
65 MB= 13, ///< 10^ 6, Megabyte , factor 100000.
66 GB= 14, ///< 10^ 9, Gigabyte , factor 100000000.
67 TB= 15, ///< 10^ 12, Terabyte , factor 100000000000.
68 PB= 16, ///< 10^ 15, Petabyte , factor 100000000000000.
69 EB= 17, ///< 10^ 18, Exabyte , factor 100000000000000000.
70 ZB= 18, ///< 10^ 21, Zettabyte , factor 100000000000000000000.
71 YB= 19, ///< 10^ 24, Yottabyte , factor 100000000000000000000000.
72 RB= 20, ///< 10^ 27, Ronnabyte , factor 100000000000000000000000000.
73 QB= 21, ///< 10^ 30, Quettabyte, factor 100000000000000000000000000000.
74 SI_END= 22, ///< END of IEC units.
75};
76
77/** The \b NumberFormat singleton used with \ref alib_strings_assembly_ttostring "appending"
78 * instances of structs \alib{lang::format;ByteSizeIEC} and \alib{lang::format;ByteSizeSI} to
79 * \b %AString objects. The object is allocated in the \alib{monomem;GlobalAllocator} during
80 * bootstrapping of module \alib_basecamp_nl.
81 *
82 * Fields of interest here, are
83 * \alib{strings::TNumberFormat;DecimalPointChar}, which is copied during bootstrap from
84 * \alib{strings::TNumberFormat;Global;NumberFormat::Global}, and
85 * \alib{strings::TNumberFormat;FractionalPartWidth} which is set to \c 1 during bootstrap.
86 */
88
89/**
90 * This namespace function searches the next "fitting" magnitude of a given \p{byteSize}, so that
91 * it can be expressed as a floating point between \c 0 and \c 999, hence with three digits.
92 * The function is used for \ref alib_strings_assembly_ttostring "appending" byte sizes to
93 * to class \b %AString. Helper types \alib{lang::format;ByteSizeIEC} and \alib{lang::format;ByteSizeSI}
94 * allow the convient use of this method in combination with \b AString objects and with parameter
95 * lists of \alib formatter functions. When using these helpers, singleton
96 * \alib{lang::format;ByteSizeNumberFormat} is used.
97 *
98 * \note In the very, very seldom case, that a software has different threads, and more than one of
99 * those needs to format byte sizes in different (!) number formats, the singleton approach is
100 * not feasable. This is the reason, why this method was exposed publicly instead of using
101 * an implementation in an anonymous namesapce.
102 *
103 * @param target The target string.
104 * @param byteSize The value to print.
105 * @param magnitudeThreshold The lowest value to use with the next lower possible
106 * magnitude. If for example to \c 900, then <em>0.9 GiB</em>
107 * is preferred over <em>900.0 MiB</em>.
108 * @param unitSeparator If not <c>'\0'</c>, this character is printed after the number and
109 * before the unit.
110 * @param unit The unit system to convert to. This parameter must be set to either
111 * \alib{lang::format;ByteSizeUnits;IEC} or \alib{lang::format;ByteSizeUnits;SI}.
112 * Other values are undefined behaviour.
113 * @param nf The number format object to use.
114 */
116void FormatByteSize(AString& target, uinteger byteSize, uint16_t magnitudeThreshold,
117 char unitSeparator, ByteSizeUnits unit, NumberFormat& nf);
118
119/** ************************************************************************************************
120 * This struct is used to format sizes of byte arrays (streams, files, etc), in accordance
121 * with IEC units (KiB, MiB, GiB, etc). Integral values embedded in this struct are
122 * \ref alib_strings_assembly_ttostring "appendable" to class \b %AString.
123 *
124 * @see Sibling struct \alib{lang::format;ByteSizeSI}
125 **************************************************************************************************/
127{
128 uinteger Value; ///< The encapsulated value to print.
129 uint16_t MagnitudeThreshold; ///< The lowest value to use with the next lower possible
130 ///< magnitude. If for example to \c 900, then <em>0.9 GiB</em>
131 ///< is preferred over <em>900.0 MiB</em>.
132 char UnitSeparator; ///< An optional character to separate the number from the unit.
133
134 /** Constructor.
135 * @param value The value to append/format.
136 * @param magnitudeThreshold Stored in #MagnitudeThreshold. Defaults to <c>8*1024/10</c>.
137 * @param unitSeparator Separation character between printed number and unit.
138 * Defaults to <c>0</c>. */
139 constexpr ByteSizeIEC( uinteger value, uint16_t magnitudeThreshold = 8*1024/10,
140 char unitSeparator= '\0' ) noexcept
141 : Value { value }
142 , MagnitudeThreshold{ magnitudeThreshold }
143 , UnitSeparator { unitSeparator } {}
144
145 /**
146 * Evaluates the magnitude of the value and returns the converted \c double value between
147 * \c 0.0 and \p{threshold}.
148 * @return The #Value converted to \c double together with the magnitude flag.
149 */
151 std::pair<double,ByteSizeUnits> GetMagnitude();
152
153 /**
154 * Returns a double value converted to the given unit.
155 * Note that even this class otherwise is responsible for IEC-units, conversions
156 * to SI-units may be requested.
157 * @param unit The unit to convert to.
158 * @return Field #Value as a \c double converted to \p{unit}.
159 */
162
163}; //struct ByteSizeIEC
164
165/** ************************************************************************************************
166 * This struct is used to format sizes of byte arrays (streams, files, etc), in accordance
167 * with SI units (kB, MB, GB, etc). Integral values embedded in this struct are
168 * \ref alib_strings_assembly_ttostring "appendable" to class \b %AString.
169 *
170 * @see Sibling struct \alib{lang::format;ByteSizeIEC}
171 **************************************************************************************************/
173{
174 uinteger Value; ///< The encapsulated value to print.
175 uint16_t MagnitudeThreshold; ///< The lowest value to use with the next lower possible
176 ///< magnitude. If for example to \c 900, then <em>0.9 GiB</em>
177 ///< is preferred over <em>900.0 MiB</em>.
178 char UnitSeparator; ///< An optional character to separate the number from the unit.
179
180 /** Constructor.
181 * @param value The value to append/format.
182 * @param magnitudeThreshold Stored in #MagnitudeThreshold. Defaults to <c>800</c>.
183 * @param unitSeparator Separation character between printed number and unit.
184 * Defaults to <c>0</c>. */
185 constexpr ByteSizeSI( uinteger value, uint16_t magnitudeThreshold = 800,
186 char unitSeparator= '\0' ) noexcept
187 : Value { value }
188 , MagnitudeThreshold{ magnitudeThreshold }
189 , UnitSeparator { unitSeparator } {}
190
191 /**
192 * Evaluates the magnitude of the value and returns the converted \c double value between
193 * \c 0.0 and \p{threshold}.
194 * @return The #Value converted to \c double together with the magnitude flag.
195 */
197 std::pair<double,ByteSizeUnits> GetMagnitude();
198
199 /**
200 * Returns a double value converted to the given unit.
201 * Note that even this class otherwise is responsible for SI-units, conversions
202 * to IEC-units may be requested.
203 * @param unit The unit to convert to.
204 * @return Field #Value as a \c double converted to \p{unit}.
205 */
208}; //struct ByteSizeSI
209
210} namespace strings {
211
212#if defined(ALIB_DOX)
213namespace APPENDABLES { // Documentation fake namespace
214#endif
215
216/** Specialization of functor \alib{strings;T_Append} for type \alib{lang::format;ByteSizeIEC}.
217 * \note This specialization is available only if module \alib_basecamp is included in the
218 * \alibdist and a using source file includes header \alibheader{lang/format/bytesize.hpp}. */
219template<> struct T_Append< lang::format::ByteSizeIEC ,character>
220{
221 /** Appends the formatted \b ByteSizeIEC to the \p{target}.
222 * @param target The \b AString that \b Append was invoked on.
223 * @param size The size to append. */
225 void operator()( AString& target, const lang::format::ByteSizeIEC size );
226};
227
228/** Specialization of functor \alib{strings;T_Append} for type \alib{lang::format;ByteSizeSI}.
229 * \note This specialization is available only if module \alib_basecamp is included in the
230 * \alibdist and a using source file includes header \alibheader{lang/format/bytesize.hpp}. */
231template<> struct T_Append< lang::format::ByteSizeSI ,character>
232{
233 /** Appends the formatted \b ByteSizeSI to the \p{target}.
234 * @param target The \b AString that \b Append was invoked on.
235 * @param size The size to append. */
237 void operator()( AString& target, const lang::format::ByteSizeSI size );
238};
239
240
241#if defined(ALIB_DOX)
242} // APPENDABLES documentation fake namespace
243#endif
244
245
246} // namespace alib[::strings]
247
248/// Type alias in namespace \b alib.
250
251/// Type alias in namespace \b alib.
253
254/// Type alias in namespace \b alib.
256
257/// Type alias in namespace \b alib.
259
260
261} // namespace [alib]
262
264ALIB_ENUMS_MAKE_ITERABLE( alib::lang::format::ByteSizeUnits, alib::lang::format::ByteSizeUnits::SI_END)
267
268#endif // HPP_ALIB_LANG_FORMAT_BYTESIZE
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
Definition records.hpp:752
#define ALIB_API
Definition alib.hpp:538
#define ALIB_ENUMS_MAKE_ITERABLE(TEnum, StopElement)
Definition iterable.hpp:114
#define ALIB_ENUMS_MAKE_ARITHMETICAL(TEnum)
#define ALIB_RESOURCED_IN_MODULE(T, Camp, ResName)
NumberFormat * ByteSizeNumberFormat
Definition basecamp.cpp:139
ALIB_API void FormatByteSize(AString &target, uinteger byteSize, uint16_t magnitudeThreshold, char unitSeparator, ByteSizeUnits unit, NumberFormat &nf)
@ B_SI
2^ 0, Byte , factor 1.
Definition bytesize.hpp:63
@ GiB
2^ 30, Gibibyte , factor 1073741824.
Definition bytesize.hpp:52
@ TiB
2^ 40, Tebibyte , factor 1099511627776.
Definition bytesize.hpp:53
@ KiB
2^ 10, Kibibyte , factor 1024.
Definition bytesize.hpp:50
@ ZiB
2^ 70, Zebibyte , factor 1180591620717411303424.
Definition bytesize.hpp:56
@ B
2^ 0, Bbyte , factor 1.
Definition bytesize.hpp:49
@ GB
10^ 9, Gigabyte , factor 100000000.
Definition bytesize.hpp:66
@ QiB
2^100, Quebibyte , factor 1267650600228229401496703205376.
Definition bytesize.hpp:59
@ kB
10^ 3, Kilobyte , factor 100.
Definition bytesize.hpp:64
@ IEC
Begin of SI units.
Definition bytesize.hpp:48
@ RB
10^ 27, Ronnabyte , factor 100000000000000000000000000.
Definition bytesize.hpp:72
@ SI
Begin of IEC units.
Definition bytesize.hpp:62
@ EiB
2^ 60, Exbibyte , factor 1152921504606846976.
Definition bytesize.hpp:55
@ YiB
2^ 80, Yobibyte , factor 1208925819614629174706176.
Definition bytesize.hpp:57
@ PB
10^ 15, Petabyte , factor 100000000000000.
Definition bytesize.hpp:68
@ QB
10^ 30, Quettabyte, factor 100000000000000000000000000000.
Definition bytesize.hpp:73
@ YB
10^ 24, Yottabyte , factor 100000000000000000000000.
Definition bytesize.hpp:71
@ RiB
2^ 90, Robibyte , factor 1237940039285380274899124224.
Definition bytesize.hpp:58
@ EB
10^ 18, Exabyte , factor 100000000000000000.
Definition bytesize.hpp:69
@ TB
10^ 12, Terabyte , factor 100000000000.
Definition bytesize.hpp:67
@ PiB
2^ 50, Pebibyte , factor 1125899906842624.
Definition bytesize.hpp:54
@ MiB
2^ 20, Mebibyte , factor 1048576.
Definition bytesize.hpp:51
@ ZB
10^ 21, Zettabyte , factor 100000000000000000000.
Definition bytesize.hpp:70
@ MB
10^ 6, Megabyte , factor 100000.
Definition bytesize.hpp:65
@ IEC_END
End of SI units.
Definition bytesize.hpp:60
@ SI_END
END of IEC units.
Definition bytesize.hpp:74
platform_specific uinteger
Definition integers.hpp:56
Definition alib.cpp:57
lang::basecamp::BaseCamp BASECAMP
Definition basecamp.cpp:136
characters::character character
Type alias in namespace alib.
char UnitSeparator
An optional character to separate the number from the unit.
Definition bytesize.hpp:132
ALIB_API std::pair< double, ByteSizeUnits > GetMagnitude()
ALIB_API double ConvertTo(ByteSizeUnits unit)
uinteger Value
The encapsulated value to print.
Definition bytesize.hpp:128
constexpr ByteSizeIEC(uinteger value, uint16_t magnitudeThreshold=8 *1024/10, char unitSeparator='\0') noexcept
Definition bytesize.hpp:139
char UnitSeparator
An optional character to separate the number from the unit.
Definition bytesize.hpp:178
ALIB_API std::pair< double, ByteSizeUnits > GetMagnitude()
constexpr ByteSizeSI(uinteger value, uint16_t magnitudeThreshold=800, char unitSeparator='\0') noexcept
Definition bytesize.hpp:185
ALIB_API double ConvertTo(ByteSizeUnits unit)
uinteger Value
The encapsulated value to print.
Definition bytesize.hpp:174
ALIB_API void operator()(AString &target, const lang::format::ByteSizeIEC size)
ALIB_API void operator()(AString &target, const lang::format::ByteSizeSI size)