| 1 |
#ifndef NARROW_HH |
| 2 |
#define NARROW_HH |
| 3 |
|
| 4 |
#include "inline.hh" |
| 5 |
#include <cassert> |
| 6 |
#include <type_traits> |
| 7 |
#include <utility> |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
template<typename To, typename From> |
| 21 |
constexpr To narrow_cast(From&& from) noexcept |
| 22 |
{ |
| 23 |
return static_cast<To>(std::forward<From>(from)); |
| 24 |
} |
| 25 |
|
| 26 |
ALWAYS_INLINE constexpr void narrow_assert(bool ok) |
| 27 |
{ |
| 28 |
if (!ok) { |
| 29 |
#ifdef UNITTEST |
| 30 |
throw "narrow() error"; |
| 31 |
#else |
| 32 |
assert(false && "narrow() error"); |
| 33 |
#endif |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
template<typename To, typename From> constexpr To narrow(From from) |
| 38 |
#ifndef UNITTEST |
| 39 |
noexcept |
| 40 |
#endif |
| 41 |
{ |
| 42 |
static_assert(std::is_arithmetic_v<From>); |
| 43 |
static_assert(std::is_arithmetic_v<To>); |
| 44 |
|
| 45 |
const To to = narrow_cast<To>(from); |
| 46 |
|
| 47 |
narrow_assert(static_cast<From>(to) == from); |
| 48 |
if constexpr (std::is_signed_v<From> != std::is_signed_v<To>) { |
| 49 |
narrow_assert((to < To{}) == (from < From{})); |
| 50 |
} |
| 51 |
|
| 52 |
return to; |
| 53 |
} |
| 54 |
|
| 55 |
#endif |