-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck_in_range.cpp
More file actions
65 lines (50 loc) · 1.78 KB
/
Copy pathcheck_in_range.cpp
File metadata and controls
65 lines (50 loc) · 1.78 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
// Copyright David Stone 2020.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module;
#include <numeric_traits/has_int128.hpp>
export module bounded.check_in_range;
import bounded.bounded_integer;
import bounded.comparison;
import bounded.comparison_builtin;
import bounded.integer;
import bounded.integral;
import numeric_traits;
import std_module;
namespace bounded {
using std::to_string;
#if defined NUMERIC_TRAITS_HAS_INT128
auto to_string(numeric_traits::uint128_t x) {
auto result = std::string();
do {
result.push_back(static_cast<char>(x % 10 + '0'));
x /= 10;
} while (x > 0);
std::reverse(result.begin(), result.end());
return result;
}
auto to_string(numeric_traits::int128_t const x) {
return x >= 0 ? to_string(static_cast<numeric_traits::uint128_t>(x)) : '-' + to_string(-static_cast<numeric_traits::uint128_t>(x));
}
#endif
auto to_string(bounded_integer auto const x) {
return to_string(+x.value());
}
export template<typename Exception = std::range_error>
constexpr auto check_in_range(integral auto const value, integral auto const minimum, integral auto const maximum) {
if (minimum <= value and value <= maximum) {
return ::bounded::detail::assume_in_range_impl(value, minimum, maximum);
} else {
throw Exception("Got a value of " + to_string(value) + " but expected a value in the range [" + to_string(minimum) + ", " + to_string(maximum) + "]");
}
}
export template<typename Target, typename Exception = std::range_error>
constexpr auto check_in_range(integral auto const value) {
return static_cast<Target>(::bounded::check_in_range<Exception>(
value,
numeric_traits::min_value<Target>,
numeric_traits::max_value<Target>
));
}
} // namespace bounded