qxLib
base.h
Go to the documentation of this file.
1 /**
2 
3  @file base.h
4  @author Khrapov
5  @date 11.08.2025
6  @copyright © Nick Khrapov, 2025. All right reserved.
7 
8 **/
9 #pragma once
10 
13 #include <qx/meta/concepts.h>
14 
15 #include <optional>
16 
17 namespace qx
18 {
19 
20 namespace units
21 {
22 
23 template<enumeration_c unit_t>
24 struct traits;
25 
26 }
27 
28 template<class T>
29 concept unit_enum_c = enumeration_c<T> && requires { units::traits<T>(); };
30 
31 /**
32  @struct unit
33  @code
34  // The things you can do with `qx::unit`:
35  // 1. Convert it using `qx::convert`:
36  qx::unit result = qx::convert(unit).to(qx::units::unit_type::type2);
37  // 2. Format it:
38  qx::string = qx::string::static_format(QX_TEXT("{}"), unit); // -> for ex. 20kB
39  // 3. Create it from a string:
40  std::optional<qx::unit<int, qx::units::data>> optResult =
41  qx::unit_from_string<int, qx::units::data, qx::char_type>(QX_TEXT("20KiB"));
42  // 4. Get the nearest meaningful value (SI units):
43  // 2000B -> 1.95KiB
44  // 3000B -> 2.92KiB
45  qx::unit converted1 = qx::normalize_unit(unit);
46  @endcode
47  @tparam T - floating point or integral type
48  @tparam unit_t - unit type enum
49 **/
50 template<arithmetic_c T, unit_enum_c unit_t>
51 struct unit
52 {
53  T value;
54  unit_t type;
55 
56  constexpr operator T() const noexcept;
57  constexpr bool operator==(const unit<T, unit_t>& other) const noexcept;
58 };
59 
60 /**
61 
62  @class convert
63  @brief A conversion class
64  @code
65  // Usage:
66  float result = qx::convert(value, qx::units::unit_type::type1).to(qx::units::unit_type::type2);
67  // or
68  qx::unit result = qx::convert(value, qx::units::unit_type::type1).to(qx::units::unit_type::type2);
69  @endcode
70  @tparam T - floating point or integral value type
71  @tparam unit_t - unit type enum
72  @author Khrapov
73  @date 13.08.2025
74 
75 **/
76 template<arithmetic_c T, unit_enum_c unit_t>
77 class convert;
78 
79 /**
80  @brief The function returns the closest value greater than one from the SI for the unit of measurement.
81  @tparam T - floating point or integral value type
82  @tparam unit_t - unit type enum
83  @param unit - input unit
84  @retval - the closest value greater than one from the SI for the unit of measurement
85 **/
86 template<arithmetic_c T, unit_enum_c unit_t>
88 
89 /**
90  @brief The function returns the closest value greater than one from the SI for the unit of measurement.
91  @tparam T - floating point or integral value type
92  @tparam unit_t - unit type enum
93  @param value - input unit value
94  @param eInitialType - input unit type
95  @retval - the closest value greater than one from the SI for the unit of measurement
96 **/
97 template<arithmetic_c T, unit_enum_c unit_t>
98 constexpr unit<T, unit_t> normalize_unit(T value, unit_t eInitialType) noexcept;
99 
100 /**
101  @brief Creates a unit from a string
102  @details Input examples: `20KiB` `20 KiB` `20 KiB ` `20KiB `
103  @tparam T - expected value type
104  @tparam unit_t - unit enum
105  @tparam char_t - input string char type
106  @param svValue - input string value
107  @retval - a unit if created successfully or `std::nullopt`
108 **/
109 template<arithmetic_c T, unit_enum_c unit_t, class char_t>
110 std::optional<unit<T, unit_t>> unit_from_string(basic_string_view<char_t> svValue) noexcept;
111 
112 /**
113  @brief Creates a unit from a string
114  @details Input examples: `20KiB` `20 KiB` `20 KiB ` `20KiB `
115  @tparam T - expected value type
116  @tparam unit_t - unit enum
117  @tparam char_t - input string char type
118  @param pszValue - input string value
119  @retval - a unit if created successfully or `std::nullopt`
120 **/
121 template<arithmetic_c T, unit_enum_c unit_t, class char_t>
122 std::optional<unit<T, unit_t>> unit_from_string(const char_t* pszValue) noexcept;
123 
124 /**
125  @brief Get a unit suffix if exists
126  @tparam unit_t - unit enum
127  @tparam char_t - result string view char type
128  @param eUnit - unit to search
129  @retval - unit suffix
130 **/
131 template<unit_enum_c unit_t, class char_t = char_type>
132 constexpr std::optional<basic_string_view<char_t>> get_unit_suffix(unit_t eUnit) noexcept;
133 
134 } // namespace qx
135 
136 #include <qx/math/units/base.inl>
constexpr unit< T, unit_t > normalize_unit(unit< T, unit_t > unit) noexcept
The function returns the closest value greater than one from the SI for the unit of measurement.
std::optional< unit< T, unit_t > > unit_from_string(basic_string_view< char_t > svValue) noexcept
Creates a unit from a string.
constexpr std::optional< basic_string_view< char_t > > get_unit_suffix(unit_t eUnit) noexcept
Get a unit suffix if exists.
A conversion class.
Definition: base.h:77
requires(same_variadic_args_v< args_t... >) const expr auto coalesce(args_t &&... args)
Coalesce function, C# a ?? b analogue.
Definition: coalesce.inl:57
Definition: base.h:52