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