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