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 
12 #include <qx/meta/concepts.h>
13 
14 namespace qx
15 {
16 
17 /**
18  @struct unit
19  @code
20  // The things you can do with `qx::unit`:
21  // 1. Convert it using `qx::convert`:
22  qx::unit result = qx::convert(unit).to(qx::units::unit_type::type2);
23  // 2. Format it:
24  qx::string = qx::string::static_format(QX_TEXT("{}"), unit); // -> for ex. 20kB
25  // 3. Create it from a string:
26  std::optional<qx::unit<int, qx::units::data>> optResult =
27  qx::unit_from_string<int, qx::units::data, qx::char_type>(QX_TEXT("20KiB"));
28  // 4. Get the nearest meaningful value (SI units):
29  // 2000B -> 1.95KiB
30  // 3000B -> 2.92KiB
31  qx::unit converted1 = qx::normalize_unit(unit);
32  @endcode
33  @tparam T - floating point or integral type
34  @tparam unit_t - unit type enum
35 **/
36 template<arithmetic_c T, enumeration_c unit_t>
37 struct unit
38 {
39  T value;
40  unit_t type;
41 
42  operator T() const noexcept;
43 };
44 
45 /**
46 
47  @class convert
48  @brief A conversion class
49  @code
50  // Usage:
51  float result = qx::convert(value, qx::units::unit_type::type1).to(qx::units::unit_type::type2);
52  // or
53  qx::unit result = qx::convert(value, qx::units::unit_type::type1).to(qx::units::unit_type::type2);
54  @endcode
55  @tparam T - floating point or integral value type
56  @tparam unit_t - unit type enum
57  @author Khrapov
58  @date 13.08.2025
59 
60 **/
61 template<arithmetic_c T, enumeration_c unit_t>
62 class convert;
63 
64 template<arithmetic_c T, enumeration_c unit_t>
65 convert(T, unit_t) -> convert<T, unit_t>;
66 
67 template<arithmetic_c T, enumeration_c unit_t>
69 
70 /**
71  @brief The function returns the closest value greater than one from the SI for the unit of measurement.
72  @tparam T - floating point or integral value type
73  @tparam unit_t - unit type enum
74  @param unit - input unit
75  @retval - the closest value greater than one from the SI for the unit of measurement
76 **/
77 template<arithmetic_c T, enumeration_c unit_t>
79 
80 /**
81  @brief The function returns the closest value greater than one from the SI for the unit of measurement.
82  @tparam T - floating point or integral value type
83  @tparam unit_t - unit type enum
84  @param value - input unit value
85  @param eInitialType - input unit type
86  @retval - the closest value greater than one from the SI for the unit of measurement
87 **/
88 template<arithmetic_c T, enumeration_c unit_t>
89 unit<T, unit_t> normalize_unit(T value, unit_t eInitialType) noexcept;
90 
91 /**
92  @brief Creates a unit from a string
93  @details Input examples: `20KiB` `20 KiB` `20 KiB ` `20KiB `
94  @tparam T - expected value type
95  @tparam unit_t - unit enum
96  @tparam char_t - input string char type
97  @param svValue - input string value
98  @retval - a unit if created successfully or `std::nullopt`
99 **/
100 template<arithmetic_c T, enumeration_c unit_t, class char_t>
101 std::optional<unit<T, unit_t>> unit_from_string(basic_string_view<char_t> svValue) noexcept;
102 
103 } // namespace qx
104 
105 template<qx::arithmetic_c T, qx::enumeration_c unit_t>
106 struct std::formatter<qx::unit<T, unit_t>, qx::char_type>
107 {
108  template<class format_parse_context_t>
109  constexpr auto parse(format_parse_context_t& context) noexcept;
110 
111  template<class format_context_type_t>
112  constexpr auto format(const qx::unit<T, unit_t>& unit, format_context_type_t& ctx) const noexcept;
113 
114 private:
115  std::formatter<T, qx::char_type> valueFormatter;
116 };
117 
118 #include <qx/math/units/base.inl>
std::optional< unit< T, unit_t > > unit_from_string(basic_string_view< char_t > svValue) noexcept
Creates a unit from a string.
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.
A conversion class.
Definition: base.h:62
Definition: base.h:38