|
| 1 | +module; |
| 2 | + |
| 3 | +#include <concepts> |
| 4 | +#include <cstddef> // NOLINT |
| 5 | +#include <cstdint> |
| 6 | +#include <limits> |
| 7 | +#include <stdexcept> |
| 8 | +#include <type_traits> |
| 9 | + |
| 10 | +export module mcpplibs.primitives.underlying.literals; |
| 11 | + |
| 12 | +import mcpplibs.primitives.algorithms.limits; |
| 13 | +import mcpplibs.primitives.conversion.traits; |
| 14 | +import mcpplibs.primitives.underlying; |
| 15 | + |
| 16 | +namespace mcpplibs::primitives::underlying::details { |
| 17 | + |
| 18 | +template <conversion::risk::kind Kind> |
| 19 | +consteval auto throw_literal_risk() -> void { |
| 20 | + if constexpr (Kind == conversion::risk::kind::overflow || |
| 21 | + Kind == conversion::risk::kind::underflow) { |
| 22 | + throw std::out_of_range{ |
| 23 | + "numeric literal is out of range for target underlying type"}; |
| 24 | + } else if constexpr (Kind == conversion::risk::kind::precision_loss) { |
| 25 | + throw std::invalid_argument{ |
| 26 | + "numeric literal loses precision for target underlying type"}; |
| 27 | + } else if constexpr (Kind == conversion::risk::kind::domain_error) { |
| 28 | + throw std::invalid_argument{ |
| 29 | + "numeric literal must be finite for target underlying type"}; |
| 30 | + } else { |
| 31 | + throw std::invalid_argument{ |
| 32 | + "numeric literal is not representable for target underlying type"}; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +template <std::floating_point T> |
| 37 | +consteval auto ordered(T value) -> bool { |
| 38 | + return (value < static_cast<T>(0)) || (value >= static_cast<T>(0)); |
| 39 | +} |
| 40 | + |
| 41 | +template <std::floating_point T> |
| 42 | +consteval auto finite(T value) -> bool { |
| 43 | + return ordered(value) && |
| 44 | + value >= algorithms::lowest_value<T>() && |
| 45 | + value <= algorithms::max_value<T>(); |
| 46 | +} |
| 47 | + |
| 48 | +template <typename To, typename From> |
| 49 | +consteval auto out_of_floating_range(From value) -> bool { |
| 50 | + using value_type = std::remove_cv_t<To>; |
| 51 | + auto const normalized = static_cast<long double>(value); |
| 52 | + auto const lowest = |
| 53 | + static_cast<long double>(algorithms::lowest_value<value_type>()); |
| 54 | + auto const max = static_cast<long double>(algorithms::max_value<value_type>()); |
| 55 | + return normalized < lowest || normalized > max; |
| 56 | +} |
| 57 | + |
| 58 | +template <std::integral To> |
| 59 | +consteval auto checked_integral_literal(unsigned long long value) -> To { |
| 60 | + using value_type = std::remove_cv_t<To>; |
| 61 | + constexpr auto max_value = |
| 62 | + static_cast<unsigned long long>(algorithms::max_value<value_type>()); |
| 63 | + |
| 64 | + if (value > max_value) { |
| 65 | + throw_literal_risk<conversion::risk::kind::overflow>(); |
| 66 | + } |
| 67 | + |
| 68 | + return static_cast<value_type>(value); |
| 69 | +} |
| 70 | + |
| 71 | +template <std::floating_point To, std_numeric From> |
| 72 | +consteval auto checked_floating_literal(From value) -> To { |
| 73 | + using source_type = std::remove_cv_t<From>; |
| 74 | + using value_type = std::remove_cv_t<To>; |
| 75 | + |
| 76 | + if constexpr (std_floating<source_type>) { |
| 77 | + if (!ordered(value)) { |
| 78 | + throw_literal_risk<conversion::risk::kind::domain_error>(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (out_of_floating_range<value_type>(value)) { |
| 83 | + if constexpr (std::signed_integral<source_type> || std_floating<source_type>) { |
| 84 | + if (value < static_cast<source_type>(0)) { |
| 85 | + throw_literal_risk<conversion::risk::kind::underflow>(); |
| 86 | + } |
| 87 | + } |
| 88 | + throw_literal_risk<conversion::risk::kind::overflow>(); |
| 89 | + } |
| 90 | + |
| 91 | + auto const converted = static_cast<value_type>(value); |
| 92 | + |
| 93 | + if constexpr (std_floating<value_type>) { |
| 94 | + if (!finite(converted)) { |
| 95 | + if constexpr (std::signed_integral<source_type> || std_floating<source_type>) { |
| 96 | + if (value < static_cast<source_type>(0)) { |
| 97 | + throw_literal_risk<conversion::risk::kind::underflow>(); |
| 98 | + } |
| 99 | + } |
| 100 | + throw_literal_risk<conversion::risk::kind::overflow>(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return converted; |
| 105 | +} |
| 106 | + |
| 107 | +template <std::floating_point To, std_numeric From> |
| 108 | +consteval auto exact_floating_literal(From value) -> To { |
| 109 | + using source_type = std::remove_cv_t<From>; |
| 110 | + using value_type = std::remove_cv_t<To>; |
| 111 | + |
| 112 | + auto const converted = checked_floating_literal<value_type>(value); |
| 113 | + |
| 114 | + if constexpr (std::integral<source_type>) { |
| 115 | + if (static_cast<source_type>(converted) != value) { |
| 116 | + throw_literal_risk<conversion::risk::kind::precision_loss>(); |
| 117 | + } |
| 118 | + } else { |
| 119 | + auto const roundtrip = static_cast<source_type>(converted); |
| 120 | + if (!ordered(roundtrip)) { |
| 121 | + throw_literal_risk<conversion::risk::kind::domain_error>(); |
| 122 | + } |
| 123 | + if (roundtrip != value) { |
| 124 | + if (converted == static_cast<value_type>(0) && |
| 125 | + value != static_cast<source_type>(0)) { |
| 126 | + throw_literal_risk<conversion::risk::kind::underflow>(); |
| 127 | + } |
| 128 | + throw_literal_risk<conversion::risk::kind::precision_loss>(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return converted; |
| 133 | +} |
| 134 | + |
| 135 | +template <char... Cs> |
| 136 | +consteval auto parse_unsigned_decimal_literal() -> unsigned long long { |
| 137 | + constexpr char input[] {Cs..., '\0'}; |
| 138 | + constexpr auto max_value = std::numeric_limits<unsigned long long>::max(); |
| 139 | + |
| 140 | + unsigned long long value {}; |
| 141 | + for (std::size_t i = 0; i < sizeof...(Cs); ++i) { |
| 142 | + auto const ch = input[i]; |
| 143 | + if (ch < '0' || ch > '9') { |
| 144 | + throw std::invalid_argument{"invalid integer literal"}; |
| 145 | + } |
| 146 | + |
| 147 | + auto const digit = static_cast<unsigned long long>(ch - '0'); |
| 148 | + if (value > max_value / 10ULL || |
| 149 | + (value == max_value / 10ULL && digit > max_value % 10ULL)) { |
| 150 | + throw std::out_of_range{"integer literal is out of range"}; |
| 151 | + } |
| 152 | + |
| 153 | + value = value * 10ULL + digit; |
| 154 | + } |
| 155 | + |
| 156 | + return value; |
| 157 | +} |
| 158 | + |
| 159 | +template <std::integral To, char... Cs> |
| 160 | +consteval auto literal_integral() -> To { |
| 161 | + return checked_integral_literal<To>(parse_unsigned_decimal_literal<Cs...>()); |
| 162 | +} |
| 163 | + |
| 164 | +} // namespace mcpplibs::primitives::underlying::details |
| 165 | + |
| 166 | +export namespace mcpplibs::primitives::literals { |
| 167 | + |
| 168 | +consteval auto operator""_uchar(const char value) -> unsigned char { |
| 169 | + return static_cast<unsigned char>(value); |
| 170 | +} |
| 171 | + |
| 172 | +consteval auto operator""_char8(const char8_t value) -> char8_t { return value; } |
| 173 | + |
| 174 | +consteval auto operator""_char16(const char16_t value) -> char16_t { return value; } |
| 175 | + |
| 176 | +consteval auto operator""_char32(const char32_t value) -> char32_t { return value; } |
| 177 | + |
| 178 | +consteval auto operator""_wchar(const wchar_t value) -> wchar_t { return value; } |
| 179 | + |
| 180 | +template <char... Cs> |
| 181 | +consteval auto operator""_u8() -> std::uint8_t { |
| 182 | + return underlying::details::literal_integral<std::uint8_t, Cs...>(); |
| 183 | +} |
| 184 | + |
| 185 | +template <char... Cs> |
| 186 | +consteval auto operator""_u16() -> std::uint16_t { |
| 187 | + return underlying::details::literal_integral<std::uint16_t, Cs...>(); |
| 188 | +} |
| 189 | + |
| 190 | +template <char... Cs> |
| 191 | +consteval auto operator""_u32() -> std::uint32_t { |
| 192 | + return underlying::details::literal_integral<std::uint32_t, Cs...>(); |
| 193 | +} |
| 194 | + |
| 195 | +template <char... Cs> |
| 196 | +consteval auto operator""_u64() -> std::uint64_t { |
| 197 | + return underlying::details::literal_integral<std::uint64_t, Cs...>(); |
| 198 | +} |
| 199 | + |
| 200 | +template <char... Cs> |
| 201 | +consteval auto operator""_size() -> std::size_t { |
| 202 | + return underlying::details::literal_integral<std::size_t, Cs...>(); |
| 203 | +} |
| 204 | + |
| 205 | +template <char... Cs> |
| 206 | +consteval auto operator""_diff() -> std::ptrdiff_t { |
| 207 | + return underlying::details::literal_integral<std::ptrdiff_t, Cs...>(); |
| 208 | +} |
| 209 | + |
| 210 | +template <char... Cs> |
| 211 | +consteval auto operator""_i8() -> std::int8_t { |
| 212 | + return underlying::details::literal_integral<std::int8_t, Cs...>(); |
| 213 | +} |
| 214 | + |
| 215 | +template <char... Cs> |
| 216 | +consteval auto operator""_i16() -> std::int16_t { |
| 217 | + return underlying::details::literal_integral<std::int16_t, Cs...>(); |
| 218 | +} |
| 219 | + |
| 220 | +template <char... Cs> |
| 221 | +consteval auto operator""_i32() -> std::int32_t { |
| 222 | + return underlying::details::literal_integral<std::int32_t, Cs...>(); |
| 223 | +} |
| 224 | + |
| 225 | +template <char... Cs> |
| 226 | +consteval auto operator""_i64() -> std::int64_t { |
| 227 | + return underlying::details::literal_integral<std::int64_t, Cs...>(); |
| 228 | +} |
| 229 | + |
| 230 | +consteval auto operator""_f32(const unsigned long long value) -> float { |
| 231 | + return underlying::details::checked_floating_literal<float>(value); |
| 232 | +} |
| 233 | + |
| 234 | +consteval auto operator""_f32(const long double value) -> float { |
| 235 | + return underlying::details::checked_floating_literal<float>(value); |
| 236 | +} |
| 237 | + |
| 238 | +consteval auto operator""_f32e(const unsigned long long value) -> float { |
| 239 | + return underlying::details::exact_floating_literal<float>(value); |
| 240 | +} |
| 241 | + |
| 242 | +consteval auto operator""_f32e(const long double value) -> float { |
| 243 | + return underlying::details::exact_floating_literal<float>(value); |
| 244 | +} |
| 245 | + |
| 246 | +consteval auto operator""_f64(const unsigned long long value) -> double { |
| 247 | + return underlying::details::checked_floating_literal<double>(value); |
| 248 | +} |
| 249 | + |
| 250 | +consteval auto operator""_f64(const long double value) -> double { |
| 251 | + return underlying::details::checked_floating_literal<double>(value); |
| 252 | +} |
| 253 | + |
| 254 | +consteval auto operator""_f64e(const unsigned long long value) -> double { |
| 255 | + return underlying::details::exact_floating_literal<double>(value); |
| 256 | +} |
| 257 | + |
| 258 | +consteval auto operator""_f64e(const long double value) -> double { |
| 259 | + return underlying::details::exact_floating_literal<double>(value); |
| 260 | +} |
| 261 | + |
| 262 | +consteval auto operator""_f80(const unsigned long long value) -> long double { |
| 263 | + return underlying::details::checked_floating_literal<long double>(value); |
| 264 | +} |
| 265 | + |
| 266 | +consteval auto operator""_f80(const long double value) -> long double { |
| 267 | + return underlying::details::checked_floating_literal<long double>(value); |
| 268 | +} |
| 269 | + |
| 270 | +consteval auto operator""_f80e(const unsigned long long value) -> long double { |
| 271 | + return underlying::details::exact_floating_literal<long double>(value); |
| 272 | +} |
| 273 | + |
| 274 | +consteval auto operator""_f80e(const long double value) -> long double { |
| 275 | + return underlying::details::exact_floating_literal<long double>(value); |
| 276 | +} |
| 277 | + |
| 278 | +} // namespace mcpplibs::primitives::literals |
0 commit comments