@@ -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
1211export module mcpplibs.primitives.operations.invoker;
1312
13+ import mcpplibs.primitives.algorithms.limits;
1414import mcpplibs.primitives.operations.traits;
1515import mcpplibs.primitives.operations.impl;
1616import 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> {
242242template <typename T>
243243constexpr 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