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