ALib C++ Library
Library Version: 2510 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 \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace format {
9
10/// Enumeration of byte-size units to output with types \alib{format;ByteSizeIEC} and
11/// \alib{format;ByteSizeSI}.<br>
12/// The unit symbols are read from the \ref alib_mod_resources "resources" of module class
13/// \alib{camp;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 \ref alib_strings_assembly_ttostring "appending"
46/// instances of structs \alib{format;ByteSizeIEC} and \alib{format;ByteSizeSI} to
47/// \b %AString objects. The object is allocated in the \alib{monomem;GLOBAL_ALLOCATOR} during
48/// bootstrapping.
49///
50/// Fields of interest here, are
51/// \alib{strings::TNumberFormat;DecimalPointChar}, which is copied during bootstrap from
52/// \alib{strings::TNumberFormat;Global;NumberFormat::Global}, and
53/// \alib{strings::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 \ref alib_strings_assembly_ttostring "appending" byte sizes to
59/// to class \b %AString. Helper-types \alib{format;ByteSizeIEC} and \alib{format;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/// \alib{format;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/// \alib{format;ByteSizeUnits;IEC} or
78/// \alib{format;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 (KiB, MiB, GiB, etc). Integral values embedded in this struct are
88/// \ref alib_strings_assembly_ttostring "appendable" to class \b %AString.
89///
90/// @see Sibling struct \alib{format;ByteSizeSI}
91//==================================================================================================
93{
94 uinteger Value; ///< The encapsulated value to print.
95 uint16_t MagnitudeThreshold; ///< The lowest value to use with the next lower possible
96 ///< magnitude. If for example to \c 900, then <em>0.9 GiB</em>
97 ///< is preferred over <em>900.0 MiB</em>.
98 char UnitSeparator; ///< An optional character to separate the number from the unit.
99
100 /// Constructor.
101 /// @param value The value to append/format.
102 /// @param magnitudeThreshold Stored in #MagnitudeThreshold. Defaults to <c>8*1024/10</c>.
103 /// @param unitSeparator Separation character between printed number and unit.
104 /// Defaults to <c>0</c>.
105 constexpr ByteSizeIEC( uinteger value, uint16_t magnitudeThreshold = 8*1024/10,
106 char unitSeparator= '\0' ) noexcept
107 : Value { value }
108 , MagnitudeThreshold{ magnitudeThreshold }
109 , UnitSeparator { unitSeparator } {}
110
111 /// Evaluates the magnitude of the value and returns the converted \c double value between
112 /// \c 0.0 and \p{threshold}.
113 /// @return The #Value converted to \c double together with the magnitude flag.
115 std::pair<double,ByteSizeUnits> GetMagnitude();
116
117 /// Returns a double value converted to the given unit.
118 /// Note that while this class otherwise is responsible for IEC units, conversions
119 /// to SI-units may be requested.
120 /// @param unit The unit to convert to.
121 /// @return Field #Value as a \c double converted to \p{unit}.
124
125}; //struct ByteSizeIEC
126
127//==================================================================================================
128/// This struct is used to format sizes of byte arrays (streams, files, etc), in accordance
129/// with SI units (kB, MB, GB, etc). Integral values embedded in this struct are
130/// \ref alib_strings_assembly_ttostring "appendable" to class \b %AString.
131///
132/// @see Sibling struct \alib{format;ByteSizeIEC}
133//==================================================================================================
135{
136 uinteger Value; ///< The encapsulated value to print.
137 uint16_t MagnitudeThreshold; ///< The lowest value to use with the next lower possible
138 ///< magnitude. If for example to \c 900, then <em>0.9 GiB</em>
139 ///< is preferred over <em>900.0 MiB</em>.
140 char UnitSeparator; ///< An optional character to separate the number from the unit.
141
142 /// Constructor.
143 /// @param value The value to append/format.
144 /// @param magnitudeThreshold Stored in #MagnitudeThreshold. Defaults to <c>800</c>.
145 /// @param unitSeparator Separation character between printed number and unit.
146 /// Defaults to <c>0</c>.
147 constexpr ByteSizeSI( uinteger value, uint16_t magnitudeThreshold = 800,
148 char unitSeparator= '\0' ) noexcept
149 : Value { value }
150 , MagnitudeThreshold{ magnitudeThreshold }
151 , UnitSeparator { unitSeparator } {}
152
153 /// Evaluates the magnitude of the value and returns the converted \c double value between
154 /// \c 0.0 and \p{threshold}.
155 /// @return The #Value converted to \c double together with the magnitude flag.
157 std::pair<double,ByteSizeUnits> GetMagnitude();
158
159 /// Returns a double value converted to the given unit.
160 /// Note that while this class otherwise is responsible for SI units, conversions
161 /// to IEC-units may be requested.
162 /// @param unit The unit to convert to.
163 /// @return Field #Value as a \c double converted to \p{unit}.
166}; //struct ByteSizeSI
167
168} namespace strings {
169
170#if DOXYGEN
171namespace APPENDABLES { // Documentation fake namespace
172#endif
173
174/// Specialization of functor \alib{strings;AppendableTraits} for type \alib{format;ByteSizeIEC}.
175/// \note This specialization is available only if the module \alib_format is included in the
176/// \alibbuild.
178{
179 /// Appends the formatted \b ByteSizeIEC to the \p{target}.
180 /// @param target The \b AString that \b Append was invoked on.
181 /// @param size The size to append.
183 void operator()( AString& target, const format::ByteSizeIEC size );
184};
185
186/// Specialization of functor \alib{strings;AppendableTraits} for type \alib{format;ByteSizeSI}.
187/// \note This specialization is available only if the module \alib_format is included in the
188/// \alibbuild.
190{
191 /// Appends the formatted \b ByteSizeSI to the \p{target}.
192 /// @param target The \b AString that \b Append was invoked on.
193 /// @param size The size to append.
195 void operator()( AString& target, const format::ByteSizeSI size );
196};
197
198
199#if DOXYGEN
200} // APPENDABLES documentation fake namespace
201#endif
202
203
204} // namespace alib[::strings]
205
206/// Type alias in namespace \b alib.
208
209/// Type alias in namespace \b alib.
211
212/// Type alias in namespace \b alib.
214
215/// Type alias in namespace \b alib.
217
218
219} // namespace [alib]
220
222ALIB_ENUMS_MAKE_ITERABLE( alib::format::ByteSizeUnits, alib::format::ByteSizeUnits::SI_END)
224
225ALIB_BOXING_VTABLE_DECLARE( alib::format::ByteSizeIEC , vt_lang_format_bytesize_iec )
226ALIB_BOXING_VTABLE_DECLARE( alib::format::ByteSizeSI , vt_lang_format_bytesize_si )
227ALIB_BOXING_VTABLE_DECLARE( alib::format::ByteSizeUnits , vt_lang_format_bytesize_units )
228
229
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
#define ALIB_ENUMS_MAKE_ITERABLE(TEnum, StopElement)
#define ALIB_BOXING_VTABLE_DECLARE(TMapped, Identifier)
#define ALIB_ENUMS_MAKE_ARITHMETICAL(TEnum)
#define ALIB_EXPORT
Definition alib.inl:488
@ 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
ALIB_DLL void FormatByteSize(AString &target, uinteger byteSize, uint16_t magnitudeThreshold, char unitSeparator, ByteSizeUnits unit, NumberFormat &nf)
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
format::ByteSizeIEC ByteSize
Type alias in namespace alib.
Definition bytesize.inl:210
format::ByteSizeIEC ByteSizeIEC
Type alias in namespace alib.
Definition bytesize.inl:213
format::ByteSizeSI ByteSizeSI
Type alias in namespace alib.
Definition bytesize.inl:216
strings::TNumberFormat< character > NumberFormat
Type alias in namespace alib.
format::ByteSizeUnits ByteSizeUnits
Type alias in namespace alib.
Definition bytesize.inl:207
characters::character character
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
Definition integers.inl:152
ALIB_DLL std::pair< double, ByteSizeUnits > GetMagnitude()
char UnitSeparator
An optional character to separate the number from the unit.
Definition bytesize.inl:98
constexpr ByteSizeIEC(uinteger value, uint16_t magnitudeThreshold=8 *1024/10, char unitSeparator='\0') noexcept
Definition bytesize.inl:105
ALIB_DLL double ConvertTo(ByteSizeUnits unit)
uinteger Value
The encapsulated value to print.
Definition bytesize.inl:94
char UnitSeparator
An optional character to separate the number from the unit.
Definition bytesize.inl:140
ALIB_DLL double ConvertTo(ByteSizeUnits unit)
uinteger Value
The encapsulated value to print.
Definition bytesize.inl:136
constexpr ByteSizeSI(uinteger value, uint16_t magnitudeThreshold=800, char unitSeparator='\0') noexcept
Definition bytesize.inl:147
ALIB_DLL std::pair< double, ByteSizeUnits > GetMagnitude()