8#ifndef SEN_CORE_BASE_CHECKED_CONVERSIONS_H
9#define SEN_CORE_BASE_CHECKED_CONVERSIONS_H
31 static void report(std::string message)
33 std::cerr << message.c_str() << std::endl;
40 static void report(std::string_view message) {
trace(std::string(message)); }
45 static void report(std::string_view message);
50 static void report(std::string_view message) { std::ignore = message; }
65template <
typename ToType,
typename FromType,
typename ReportPolicy = ReportPolicyIgnore>
68 if constexpr (std::is_same_v<ToType, bool>)
70 return static_cast<bool>(from);
72 else if constexpr (std::is_floating_point_v<ToType> || std::is_floating_point_v<FromType>)
74 if constexpr (std::is_same_v<FromType, ToType>)
80 if (std::isless(
static_cast<float64_t>(from),
static_cast<float64_t>(std::numeric_limits<ToType>::lowest())))
82 ReportPolicy::report(
"Needed to truncate `from` as it's value was to small for ToType.");
83 return std::numeric_limits<ToType>::lowest();
86 if (std::isgreater(
static_cast<float64_t>(from),
static_cast<float64_t>(std::numeric_limits<ToType>::max())))
88 ReportPolicy::report(
"Needed to truncate `from` as it's value was to big for ToType.");
89 return std::numeric_limits<ToType>::max();
92 return static_cast<ToType
>(from);
99 ReportPolicy::report(
"Needed to truncate `from` as it's value was to small for ToType.");
100 return std::numeric_limits<ToType>::lowest();
105 ReportPolicy::report(
"Needed to truncate `from` as it's value was to big for ToType.");
106 return std::numeric_limits<ToType>::max();
109 return static_cast<ToType
>(from);
127template <
typename ToType,
typename ReportPolicy = ReportPolicyAssertion,
typename FromType>
145template <
typename ToType,
typename ReportPolicy = ReportPolicyIgnore,
typename FromType>
The following macros implement a replacement of assert that is connected to the overall fault handlin...
#define SEN_ASSERT(expr)
Checks an intermediate result produced by a procedure (not an input or output). NOLINTNEXTLINE.
Definition assert.h:39
void trace()
Prints the current stack trace to stderr.
double float64_t
Definition numbers.h:17
Definition checked_conversions.h:54
ToType conversionImpl(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:66
ToType ignoredLossyConversion(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:146
constexpr bool cmp_greater(T t, U u) noexcept
Definition integer_compare.h:83
constexpr bool cmp_less(T t, U u) noexcept
Definition integer_compare.h:63
ToType checkedConversion(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:128
Definition checked_conversions.h:30
static void report(std::string message)
Definition checked_conversions.h:31
Definition checked_conversions.h:49
static void report(std::string_view message)
Definition checked_conversions.h:50
Definition checked_conversions.h:44
static void report(std::string_view message)
Definition checked_conversions.h:39
static void report(std::string_view message)
Definition checked_conversions.h:40