29#include <fmt/format.h>
42 using num_collect_exception::num_collect_exception;
53template <base::concepts::
integral To, base::concepts::
integral From>
54[[nodiscard]]
inline auto safe_cast(
const From& value) -> To {
56 if constexpr (std::numeric_limits<To>::digits <
57 std::numeric_limits<From>::digits) {
58 if (value >
static_cast<From
>(std::numeric_limits<To>::max())) {
60 "unsafe cast of value {} from {} to {} (upper bound)", value,
61 typeid(From).name(),
typeid(To).name());
66 if constexpr (std::is_signed_v<From> && std::is_unsigned_v<To>) {
67 if (value <
static_cast<From
>(0)) {
69 "unsafe cast of value {} from {} to {} (lower bound)", value,
70 typeid(From).name(),
typeid(To).name());
73 if constexpr (std::is_signed_v<From> && std::is_signed_v<To> &&
74 (std::numeric_limits<To>::digits < std::numeric_limits<From>::digits)) {
75 if (value <
static_cast<From
>(std::numeric_limits<To>::min())) {
77 "unsafe cast of value {} from {} to {} (lower bound)", value,
78 typeid(From).name(),
typeid(To).name());
82 return static_cast<To
>(value);
Class of exception in this project.
Class of exception on unsafe casts.
Definition of exceptions.
Definition of integral concept.
Definition of macros for logging.
#define NUM_COLLECT_LOG_AND_THROW(EXCEPTION_TYPE,...)
Write an error log and throw an exception for an error.
auto safe_cast(const From &value) -> To
Cast safely.