Skip to content

Commit 314b19a

Browse files
feat: Replace std::numeric_limits with algorithms module for improved type limits handling
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
1 parent 7e21e4e commit 314b19a

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/conversion/underlying.cppm

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ module;
33
#include <cmath>
44
#include <cstdint>
55
#include <expected>
6-
#include <limits>
76
#include <optional>
87
#include <type_traits>
98

109
export module mcpplibs.primitives.conversion.underlying;
1110

11+
import mcpplibs.primitives.algorithms.limits;
1212
import mcpplibs.primitives.conversion.traits;
1313
import mcpplibs.primitives.underlying;
1414

@@ -65,11 +65,11 @@ constexpr auto numeric_risk(SrcRep value)
6565
auto const signed_value = static_cast<std::intmax_t>(value);
6666
if constexpr (std::is_signed_v<dest_type>) {
6767
if (signed_value <
68-
static_cast<std::intmax_t>(std::numeric_limits<dest_type>::min())) {
68+
static_cast<std::intmax_t>(algorithms::min_value<dest_type>())) {
6969
return risk::kind::underflow;
7070
}
7171
if (signed_value >
72-
static_cast<std::intmax_t>(std::numeric_limits<dest_type>::max())) {
72+
static_cast<std::intmax_t>(algorithms::max_value<dest_type>())) {
7373
return risk::kind::overflow;
7474
}
7575
return std::nullopt;
@@ -80,15 +80,15 @@ constexpr auto numeric_risk(SrcRep value)
8080

8181
if (static_cast<std::uintmax_t>(signed_value) >
8282
static_cast<std::uintmax_t>(
83-
std::numeric_limits<dest_type>::max())) {
83+
algorithms::max_value<dest_type>())) {
8484
return risk::kind::overflow;
8585
}
8686
return std::nullopt;
8787
}
8888
} else {
8989
auto const unsigned_value = static_cast<std::uintmax_t>(value);
9090
if (unsigned_value >
91-
static_cast<std::uintmax_t>(std::numeric_limits<dest_type>::max())) {
91+
static_cast<std::uintmax_t>(algorithms::max_value<dest_type>())) {
9292
return risk::kind::overflow;
9393
}
9494
return std::nullopt;
@@ -104,9 +104,9 @@ constexpr auto numeric_risk(SrcRep value)
104104

105105
auto const normalized = static_cast<long double>(value);
106106
auto const min_value =
107-
static_cast<long double>(std::numeric_limits<dest_type>::lowest());
107+
static_cast<long double>(algorithms::lowest_value<dest_type>());
108108
auto const max_value =
109-
static_cast<long double>(std::numeric_limits<dest_type>::max());
109+
static_cast<long double>(algorithms::max_value<dest_type>());
110110

111111
if (normalized < min_value) {
112112
return risk::kind::underflow;
@@ -135,9 +135,9 @@ constexpr auto numeric_risk(SrcRep value)
135135

136136
auto const normalized = static_cast<long double>(value);
137137
auto const min_value =
138-
static_cast<long double>(std::numeric_limits<dest_type>::lowest());
138+
static_cast<long double>(algorithms::lowest_value<dest_type>());
139139
auto const max_value =
140-
static_cast<long double>(std::numeric_limits<dest_type>::max());
140+
static_cast<long double>(algorithms::max_value<dest_type>());
141141

142142
if (normalized < min_value) {
143143
return risk::kind::underflow;
@@ -216,10 +216,10 @@ constexpr auto saturating_rep_cast(SrcRep value) noexcept
216216
if constexpr (std_numeric<dest_type> && std_numeric<src_type>) {
217217
if (auto const kind = numeric_risk<dest_type>(value); kind.has_value()) {
218218
if (*kind == risk::kind::overflow) {
219-
return std::numeric_limits<dest_type>::max();
219+
return algorithms::max_value<dest_type>();
220220
}
221221
if (*kind == risk::kind::underflow) {
222-
return std::numeric_limits<dest_type>::lowest();
222+
return algorithms::lowest_value<dest_type>();
223223
}
224224
if (*kind == risk::kind::domain_error) {
225225
return dest_type{};
@@ -243,21 +243,21 @@ constexpr auto truncating_rep_cast(SrcRep value) noexcept
243243
}
244244
if (std::isinf(value)) {
245245
return value < static_cast<src_type>(0)
246-
? std::numeric_limits<dest_type>::lowest()
247-
: std::numeric_limits<dest_type>::max();
246+
? algorithms::lowest_value<dest_type>()
247+
: algorithms::max_value<dest_type>();
248248
}
249249

250250
auto const normalized = static_cast<long double>(value);
251251
auto const min_value =
252-
static_cast<long double>(std::numeric_limits<dest_type>::lowest());
252+
static_cast<long double>(algorithms::lowest_value<dest_type>());
253253
auto const max_value =
254-
static_cast<long double>(std::numeric_limits<dest_type>::max());
254+
static_cast<long double>(algorithms::max_value<dest_type>());
255255

256256
if (normalized < min_value) {
257-
return std::numeric_limits<dest_type>::lowest();
257+
return algorithms::lowest_value<dest_type>();
258258
}
259259
if (normalized > max_value) {
260-
return std::numeric_limits<dest_type>::max();
260+
return algorithms::max_value<dest_type>();
261261
}
262262
}
263263

src/operations/invoker.cppm

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module;
44
#include <compare>
55
#include <concepts>
66
#include <expected>
7-
#include <limits>
87
#include <optional>
98
#include <type_traits>
109
#include <utility>
1110

1211
export module mcpplibs.primitives.operations.invoker;
1312

13+
import mcpplibs.primitives.algorithms.limits;
1414
import mcpplibs.primitives.operations.traits;
1515
import mcpplibs.primitives.operations.impl;
1616
import mcpplibs.primitives.policy.handler;
@@ -42,7 +42,7 @@ constexpr auto checked_add(T lhs, T rhs) -> policy::value::decision<T> {
4242
out.value = static_cast<T>(lhs + rhs);
4343
return out;
4444
} else if constexpr (std::is_unsigned_v<T>) {
45-
auto const maxv = std::numeric_limits<T>::max();
45+
auto const maxv = algorithms::max_value<T>();
4646
if (lhs > maxv - rhs) {
4747
return make_error<T>(policy::error::kind::overflow,
4848
"checked addition overflow", lhs, rhs);
@@ -52,8 +52,8 @@ constexpr auto checked_add(T lhs, T rhs) -> policy::value::decision<T> {
5252
out.value = static_cast<T>(lhs + rhs);
5353
return out;
5454
} else {
55-
auto const maxv = std::numeric_limits<T>::max();
56-
auto const minv = std::numeric_limits<T>::min();
55+
auto const maxv = algorithms::max_value<T>();
56+
auto const minv = algorithms::min_value<T>();
5757
if ((rhs > 0) && (lhs > maxv - rhs)) {
5858
return make_error<T>(policy::error::kind::overflow,
5959
"checked addition overflow", lhs, rhs);
@@ -86,8 +86,8 @@ constexpr auto checked_sub(T lhs, T rhs) -> policy::value::decision<T> {
8686
out.value = static_cast<T>(lhs - rhs);
8787
return out;
8888
} else {
89-
auto const maxv = std::numeric_limits<T>::max();
90-
auto const minv = std::numeric_limits<T>::min();
89+
auto const maxv = algorithms::max_value<T>();
90+
auto const minv = algorithms::min_value<T>();
9191
if ((rhs < 0) && (lhs > maxv + rhs)) {
9292
return make_error<T>(policy::error::kind::overflow,
9393
"checked subtraction overflow", lhs, rhs);
@@ -119,7 +119,7 @@ constexpr auto checked_mul(T lhs, T rhs) -> policy::value::decision<T> {
119119
}
120120

121121
if constexpr (std::is_unsigned_v<T>) {
122-
auto const maxv = std::numeric_limits<T>::max();
122+
auto const maxv = algorithms::max_value<T>();
123123
if (lhs > maxv / rhs) {
124124
return make_error<T>(policy::error::kind::overflow,
125125
"checked multiplication overflow", lhs, rhs);
@@ -129,8 +129,8 @@ constexpr auto checked_mul(T lhs, T rhs) -> policy::value::decision<T> {
129129
out.value = static_cast<T>(lhs * rhs);
130130
return out;
131131
} else {
132-
auto const maxv = std::numeric_limits<T>::max();
133-
auto const minv = std::numeric_limits<T>::min();
132+
auto const maxv = algorithms::max_value<T>();
133+
auto const minv = algorithms::min_value<T>();
134134

135135
if (lhs > 0) {
136136
if (rhs > 0) {
@@ -174,7 +174,7 @@ constexpr auto checked_div(T lhs, T rhs) -> policy::value::decision<T> {
174174
}
175175

176176
if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
177-
auto const minv = std::numeric_limits<T>::min();
177+
auto const minv = algorithms::min_value<T>();
178178
if (lhs == minv && rhs == static_cast<T>(-1)) {
179179
return make_error<T>(policy::error::kind::overflow,
180180
"checked division overflow", lhs, rhs);
@@ -212,7 +212,7 @@ constexpr auto checked_mod(T lhs, T rhs) -> policy::value::decision<T> {
212212
}
213213

214214
if constexpr (std::is_signed_v<T>) {
215-
auto const minv = std::numeric_limits<T>::min();
215+
auto const minv = algorithms::min_value<T>();
216216
if (lhs == minv && rhs == static_cast<T>(-1)) {
217217
return make_error<T>(policy::error::kind::overflow,
218218
"checked modulus overflow", lhs, rhs);
@@ -242,7 +242,7 @@ constexpr auto checked_unary_plus(T lhs) -> policy::value::decision<T> {
242242
template <typename T>
243243
constexpr auto checked_unary_minus(T lhs) -> policy::value::decision<T> {
244244
if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
245-
auto const minv = std::numeric_limits<T>::min();
245+
auto const minv = algorithms::min_value<T>();
246246
if (lhs == minv) {
247247
return make_error<T>(policy::error::kind::overflow,
248248
"checked unary minus overflow", lhs);
@@ -269,7 +269,7 @@ constexpr auto checked_shift_left(T lhs, T rhs) -> policy::value::decision<T> {
269269
rhs);
270270
} else {
271271
using unsigned_t = std::make_unsigned_t<T>;
272-
constexpr auto bit_width = std::numeric_limits<unsigned_t>::digits;
272+
constexpr auto bit_width = algorithms::limits<unsigned_t>::digits;
273273

274274
if constexpr (std::is_signed_v<T>) {
275275
if (rhs < T{}) {
@@ -290,7 +290,7 @@ constexpr auto checked_shift_left(T lhs, T rhs) -> policy::value::decision<T> {
290290
"checked left shift negative lhs", lhs, rhs);
291291
}
292292

293-
auto const maxv = std::numeric_limits<T>::max();
293+
auto const maxv = algorithms::max_value<T>();
294294
if (lhs > static_cast<T>(maxv >> shift)) {
295295
return make_error<T>(policy::error::kind::overflow,
296296
"checked left shift overflow", lhs, rhs);
@@ -314,7 +314,7 @@ constexpr auto checked_shift_right(T lhs, T rhs)
314314
rhs);
315315
} else {
316316
using unsigned_t = std::make_unsigned_t<T>;
317-
constexpr auto bit_width = std::numeric_limits<unsigned_t>::digits;
317+
constexpr auto bit_width = algorithms::limits<unsigned_t>::digits;
318318

319319
if constexpr (std::is_signed_v<T>) {
320320
if (rhs < T{}) {
@@ -681,11 +681,11 @@ template <typename T> constexpr auto saturating_add(T lhs, T rhs) -> T {
681681
if constexpr (!std::is_integral_v<T> || std::is_same_v<T, bool>) {
682682
return static_cast<T>(lhs + rhs);
683683
} else if constexpr (std::is_unsigned_v<T>) {
684-
auto const maxv = std::numeric_limits<T>::max();
684+
auto const maxv = algorithms::max_value<T>();
685685
return (lhs > maxv - rhs) ? maxv : static_cast<T>(lhs + rhs);
686686
} else {
687-
auto const maxv = std::numeric_limits<T>::max();
688-
auto const minv = std::numeric_limits<T>::min();
687+
auto const maxv = algorithms::max_value<T>();
688+
auto const minv = algorithms::min_value<T>();
689689
if ((rhs > 0) && (lhs > maxv - rhs)) {
690690
return maxv;
691691
}
@@ -702,8 +702,8 @@ template <typename T> constexpr auto saturating_sub(T lhs, T rhs) -> T {
702702
} else if constexpr (std::is_unsigned_v<T>) {
703703
return (lhs < rhs) ? T{} : static_cast<T>(lhs - rhs);
704704
} else {
705-
auto const maxv = std::numeric_limits<T>::max();
706-
auto const minv = std::numeric_limits<T>::min();
705+
auto const maxv = algorithms::max_value<T>();
706+
auto const minv = algorithms::min_value<T>();
707707
if ((rhs < 0) && (lhs > maxv + rhs)) {
708708
return maxv;
709709
}
@@ -723,11 +723,11 @@ template <typename T> constexpr auto saturating_mul(T lhs, T rhs) -> T {
723723
}
724724

725725
if constexpr (std::is_unsigned_v<T>) {
726-
auto const maxv = std::numeric_limits<T>::max();
726+
auto const maxv = algorithms::max_value<T>();
727727
return (lhs > maxv / rhs) ? maxv : static_cast<T>(lhs * rhs);
728728
} else {
729-
auto const maxv = std::numeric_limits<T>::max();
730-
auto const minv = std::numeric_limits<T>::min();
729+
auto const maxv = algorithms::max_value<T>();
730+
auto const minv = algorithms::min_value<T>();
731731

732732
if (lhs > 0) {
733733
if (rhs > 0) {

0 commit comments

Comments
 (0)