Line data Source code
1 : #pragma once
2 :
3 : #include <libassert/assert.hpp>
4 :
5 : #include <concepts>
6 : #include <limits>
7 : #include <type_traits>
8 :
9 264337262 : template <std::integral Dst, std::integral Src> constexpr Dst integerCast(Src const &x) {
10 264337262 : DEBUG_ASSERT(std::in_range<Dst>(x), x, std::numeric_limits<Dst>::lowest(),
11 264337262 : std::numeric_limits<Dst>::max());
12 264337262 : return static_cast<Dst>(x);
13 264337262 : }
14 :
15 1107 : template <std::integral Src> constexpr auto castToSigned(Src const &x) {
16 1107 : return integerCast<std::make_signed_t<Src>>(x);
17 1107 : }
18 :
19 : template <std::integral Src> constexpr auto castToUnsigned(Src const &x) {
20 : return integerCast<std::make_unsigned_t<Src>>(x);
21 : }
|