ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
bytesize.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_format of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace format {
9
10/// Enumeration of byte-size units to output with types #"ByteSizeIEC" and
11/// #"ByteSizeSI".<br>
12/// The unit symbols are read from the #"alib_mod_resources;resources" of module class
13/// #"Basecamp". The list is given with resource name <b>BS</b>.
14enum class ByteSizeUnits : uint8_t
15{
16 IEC= 0, ///< Begin of SI units.
17 B= 0, ///< 2^ 0, Bbyte, factor 1.
18 KiB= 1, ///< 2^ 10, Kibibyte, factor 1024.
19 MiB= 2, ///< 2^ 20, Mebibyte, factor 1048576.
20 GiB= 3, ///< 2^ 30, Gibibyte, factor 1073741824.
21 TiB= 4, ///< 2^ 40, Tebibyte, factor 1099511627776.
22 PiB= 5, ///< 2^ 50, Pebibyte, factor 1125899906842624.
23 EiB= 6, ///< 2^ 60, Exbibyte, factor 1152921504606846976.
24 ZiB= 7, ///< 2^ 70, Zebibyte, factor 1180591620717411303424.
25 YiB= 8, ///< 2^ 80, Yobibyte, factor 1208925819614629174706176.
26 RiB= 9, ///< 2^ 90, Robibyte, factor 1237940039285380274899124224.
27 QiB= 10, ///< 2^100, Quebibyte, factor 1267650600228229401496703205376.
28 IEC_END= 11, ///< End of SI units
29
30 SI= 11, ///< Begin of IEC units.
31 B_SI= 11, ///< 2^ 0, Byte, factor 1.
32 kB= 12, ///< 10^ 3, Kilobyte, factor 100.
33 MB= 13, ///< 10^ 6, Megabyte, factor 100000.
34 GB= 14, ///< 10^ 9, Gigabyte, factor 100000000.
35 TB= 15, ///< 10^ 12, Terabyte, factor 100000000000.
36 PB= 16, ///< 10^ 15, Petabyte, factor 100000000000000.
37 EB= 17, ///< 10^ 18, Exabyte, factor 100000000000000000.
38 ZB= 18, ///< 10^ 21, Zettabyte, factor 100000000000000000000.
39 YB= 19, ///< 10^ 24, Yottabyte, factor 100000000000000000000000.
40 RB= 20, ///< 10^ 27, Ronnabyte, factor 100000000000000000000000000.
41 QB= 21, ///< 10^ 30, Quettabyte, factor 100000000000000000000000000000.
42 SI_END= 22, ///< END of IEC units.
43};
44
45/// The \b NumberFormat singleton used with #"alib_strings_assembly_ttostring;appending"
46/// instances of structs #"ByteSizeIEC" and #"ByteSizeSI" to
47/// \b %AString objects. The object is allocated in the #"GLOBAL_ALLOCATOR" during
48/// bootstrapping.
49///
50/// Fields of interest here, are
51/// #"TNumberFormat::DecimalPointChar", which is copied during bootstrap from
52/// #"TNumberFormat::Global", and
53/// #"TNumberFormat::FractionalPartWidth" which is set to \c 1 during bootstrap.
55
56/// This namespace function searches the next "fitting" magnitude of a given \p{byteSize}, so that
57/// it can be expressed as a floating point between \c 0 and \c 999, hence with three digits.
58/// The function is used for #"alib_strings_assembly_ttostring;appending" byte sizes to
59/// class \b %AString. Helper-types #"ByteSizeIEC" and #"ByteSizeSI"
60/// allow the convenient use of this method in combination with \b AString objects and with parameter
61/// lists of \alib formatter functions. When using these helpers, singleton
62/// #"BYTESIZE_NUMBER_FORMAT" is used.
63///
64/// \note In the seldom case, that software has different threads, and more than one of
65/// those needs to format byte sizes in different number formats, the singleton approach is
66/// not feasible. This is the reason, why this method was exposed publicly instead of using
67/// an implementation in an anonymous namespace.
68///
69/// @param target The target string.
70/// @param byteSize The value to print.
71/// @param magnitudeThreshold The lowest value to use with the next lower possible
72/// magnitude. If, for example, to \c 900, then <em>0.9 GiB</em>
73/// is preferred over <em>900.0 MiB</em>.
74/// @param unitSeparator If not <c>'\0'</c>, this character is printed after the number and
75/// before the unit.
76/// @param unit The unit system to convert to. This parameter must be set to either
77/// #"ByteSizeUnits;IEC" or
78/// #"ByteSizeUnits;SI".
79/// Other values are undefined behavior.
80/// @param nf The number format object to use.
82void FormatByteSize(AString& target, uinteger byteSize, uint16_t magnitudeThreshold,
83 char unitSeparator, ByteSizeUnits unit, NumberFormat& nf);
84
85//==================================================================================================
86/// This struct is used to format sizes of byte arrays (streams, files, etc), in accordance
87/// with IEC units which are based on 1024 bytes (\e KiB, \e MiB, \e GiB, etc).
88/// Integral values embedded in this struct are #"alib_strings_assembly_ttostring;appendable"
89/// to class \b %AString.
90///
91/// @see Sibling struct #"ByteSizeSI"
92//==================================================================================================
94{
95 uinteger Value; ///< The encapsulated value to print.
96 uint16_t MagnitudeThreshold; ///< The lowest value to use with the next lower possible
97 ///< magnitude. If, for example, to \c 900, then <em>0.9 GiB</em>
98 ///< is preferred over <em>900.0 MiB</em>.
99 char UnitSeparator; ///< An optional character to separate the number from the unit.
100
101 /// Constructor.
102 /// @param value The value to append/format.
103 /// @param magnitudeThreshold Stored in #".MagnitudeThreshold". Defaults to <c>8*1024/10</c>.
104 /// @param unitSeparator Separation character between printed number and unit.
105 /// Defaults to <c>0</c>.
106 constexpr ByteSizeIEC( uinteger value, uint16_t magnitudeThreshold = 8*1024/10,
107 char unitSeparator= '\0' ) noexcept
108 : Value { value }
109 , MagnitudeThreshold{ magnitudeThreshold }
110 , UnitSeparator { unitSeparator } {}
111
112 /// Evaluates the magnitude of the value and returns the converted \c double value between
113 /// \c 0.0 and \p{threshold}.
114 /// @return The #"Value" converted to \c double together with the magnitude flag.
116 std::pair<double,ByteSizeUnits> GetMagnitude();
117
118 /// Returns a double value converted to the given unit.
119 /// Note that while this class otherwise is responsible for IEC units, conversions
120 /// to SI-units may be requested.
121 /// @param unit The unit to convert to.
122 /// @return Field #"Value" as a \c double converted to \p{unit}.
125
126}; //struct ByteSizeIEC
127
128//==================================================================================================
129/// This struct is used to format sizes of byte arrays (streams, files, etc), in accordance
130/// with SI units which are based on 1000 bytes (\e kB, \e MB, \e GB, etc).
131/// Integral values embedded in this struct are #"alib_strings_assembly_ttostring;appendable" to class \b %AString.
132///
133/// @see Sibling struct #"ByteSizeIEC"
134//==================================================================================================
136{
137 uinteger Value; ///< The encapsulated value to print.
138 uint16_t MagnitudeThreshold; ///< The lowest value to use with the next lower possible
139 ///< magnitude. If, for example, to \c 900, then <em>0.9 GiB</em>
140 ///< is preferred over <em>900.0 MiB</em>.
141 char UnitSeparator; ///< An optional character to separate the number from the unit.
142
143 /// Constructor.
144 /// @param value The value to append/format.
145 /// @param magnitudeThreshold Stored in #".MagnitudeThreshold". Defaults to <c>800</c>.
146 /// @param unitSeparator Separation character between printed number and unit.
147 /// Defaults to <c>0</c>.
148 constexpr ByteSizeSI( uinteger value, uint16_t magnitudeThreshold = 800,
149 char unitSeparator= '\0' ) noexcept
150 : Value { value }
151 , MagnitudeThreshold{ magnitudeThreshold }
152 , UnitSeparator { unitSeparator } {}
153
154 /// Evaluates the magnitude of the value and returns the converted \c double value between
155 /// \c 0.0 and \p{threshold}.
156 /// @return The #"Value" converted to \c double together with the magnitude flag.
158 std::pair<double,ByteSizeUnits> GetMagnitude();
159
160 /// Returns a double value converted to the given unit.
161 /// Note that while this class otherwise is responsible for SI units, conversions
162 /// to IEC-units may be requested.
163 /// @param unit The unit to convert to.
164 /// @return Field #"Value" as a \c double converted to \p{unit}.
167}; //struct ByteSizeSI
168
169} namespace strings {
170
171#if DOXYGEN
172namespace APPENDABLES { // Documentation fake namespace
173#endif
174
175/// Specialization of functor #"AppendableTraits" for type #"ByteSizeIEC".
176/// \note This specialization is available only if the module \alib_format is included in the
177/// \alibbuild.
179{
180 /// Appends the formatted \b ByteSizeIEC to the \p{target}.
181 /// @param target The \b AString that \b Append was invoked on.
182 /// @param size The size to append.
184 void operator()( AString& target, const format::ByteSizeIEC size );
185};
186
187/// Specialization of functor #"AppendableTraits" for type #"ByteSizeSI".
188/// \note This specialization is available only if the module \alib_format is included in the
189/// \alibbuild.
191{
192 /// Appends the formatted \b ByteSizeSI to the \p{target}.
193 /// @param target The \b AString that \b Append was invoked on.
194 /// @param size The size to append.
196 void operator()( AString& target, const format::ByteSizeSI size );
197};
198
199
200#if DOXYGEN
201} // APPENDABLES documentation fake namespace
202#endif
203
204
205} // namespace alib[::strings]
206
207/// Type alias in namespace \b alib.
209
210/// Type alias in namespace \b alib.
212
213/// Type alias in namespace \b alib.
215
216/// Type alias in namespace \b alib.
218
219
220} // namespace [alib]
221
223ALIB_ENUMS_MAKE_ITERABLE( alib::format::ByteSizeUnits, alib::format::ByteSizeUnits::SI_END)
225
226ALIB_BOXING_VTABLE_DECLARE( alib::format::ByteSizeIEC , vt_lang_format_bytesize_iec )
227ALIB_BOXING_VTABLE_DECLARE( alib::format::ByteSizeSI , vt_lang_format_bytesize_si )
228ALIB_BOXING_VTABLE_DECLARE( alib::format::ByteSizeUnits , vt_lang_format_bytesize_units )
#define ALIB_DLL
Definition alib.inl:573
#define ALIB_EXPORT
Definition alib.inl:562
#define ALIB_BOXING_VTABLE_DECLARE(TMapped, Identifier)
#define ALIB_ENUMS_MAKE_ITERABLE(TEnum, StopElement)
#define ALIB_ENUMS_MAKE_ARITHMETICAL(TEnum)
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
@ GiB
2^ 30, Gibibyte, factor 1073741824.
Definition bytesize.inl:20
@ RiB
2^ 90, Robibyte, factor 1237940039285380274899124224.
Definition bytesize.inl:26
@ EB
10^ 18, Exabyte, factor 100000000000000000.
Definition bytesize.inl:37
@ kB
10^ 3, Kilobyte, factor 100.
Definition bytesize.inl:32
@ QiB
2^100, Quebibyte, factor 1267650600228229401496703205376.
Definition bytesize.inl:27
@ ZiB
2^ 70, Zebibyte, factor 1180591620717411303424.
Definition bytesize.inl:24
@ SI_END
END of IEC units.
Definition bytesize.inl:42
@ GB
10^ 9, Gigabyte, factor 100000000.
Definition bytesize.inl:34
@ MB
10^ 6, Megabyte, factor 100000.
Definition bytesize.inl:33
@ B
2^ 0, Bbyte, factor 1.
Definition bytesize.inl:17
@ QB
10^ 30, Quettabyte, factor 100000000000000000000000000000.
Definition bytesize.inl:41
@ ZB
10^ 21, Zettabyte, factor 100000000000000000000.
Definition bytesize.inl:38
@ B_SI
2^ 0, Byte, factor 1.
Definition bytesize.inl:31
@ TiB
2^ 40, Tebibyte, factor 1099511627776.
Definition bytesize.inl:21
@ YB
10^ 24, Yottabyte, factor 100000000000000000000000.
Definition bytesize.inl:39
@ IEC
Begin of SI units.
Definition bytesize.inl:16
@ RB
10^ 27, Ronnabyte, factor 100000000000000000000000000.
Definition bytesize.inl:40
@ PB
10^ 15, Petabyte, factor 100000000000000.
Definition bytesize.inl:36
@ SI
Begin of IEC units.
Definition bytesize.inl:30
@ PiB
2^ 50, Pebibyte, factor 1125899906842624.
Definition bytesize.inl:22
@ MiB
2^ 20, Mebibyte, factor 1048576.
Definition bytesize.inl:19
@ KiB
2^ 10, Kibibyte, factor 1024.
Definition bytesize.inl:18
@ IEC_END
End of SI units.
Definition bytesize.inl:28
@ YiB
2^ 80, Yobibyte, factor 1208925819614629174706176.
Definition bytesize.inl:25
@ EiB
2^ 60, Exbibyte, factor 1152921504606846976.
Definition bytesize.inl:23
@ TB
10^ 12, Terabyte, factor 100000000000.
Definition bytesize.inl:35
NumberFormat * BYTESIZE_NUMBER_FORMAT
void FormatByteSize(AString &target, uinteger byteSize, uint16_t magnitudeThreshold, char unitSeparator, ByteSizeUnits unit, NumberFormat &nf)
strings::TNumberFormat< character > NumberFormat
Type alias in namespace alib.
format::ByteSizeUnits ByteSizeUnits
Type alias in namespace alib.
Definition bytesize.inl:208
format::ByteSizeSI ByteSizeSI
Type alias in namespace alib.
Definition bytesize.inl:217
format::ByteSizeIEC ByteSizeIEC
Type alias in namespace alib.
Definition bytesize.inl:214
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
Definition integers.inl:152
format::ByteSizeIEC ByteSize
Type alias in namespace alib.
Definition bytesize.inl:211
double ConvertTo(ByteSizeUnits unit)
std::pair< double, ByteSizeUnits > GetMagnitude()
char UnitSeparator
An optional character to separate the number from the unit.
Definition bytesize.inl:99
constexpr ByteSizeIEC(uinteger value, uint16_t magnitudeThreshold=8 *1024/10, char unitSeparator='\0') noexcept
Definition bytesize.inl:106
uinteger Value
The encapsulated value to print.
Definition bytesize.inl:95
std::pair< double, ByteSizeUnits > GetMagnitude()
char UnitSeparator
An optional character to separate the number from the unit.
Definition bytesize.inl:141
double ConvertTo(ByteSizeUnits unit)
uinteger Value
The encapsulated value to print.
Definition bytesize.inl:137
constexpr ByteSizeSI(uinteger value, uint16_t magnitudeThreshold=800, char unitSeparator='\0') noexcept
Definition bytesize.inl:148