8#ifndef SEN_CORE_BASE_CHECKED_CONVERSIONS_H
9#define SEN_CORE_BASE_CHECKED_CONVERSIONS_H
30 static void report(std::string message)
32 std::cerr << message.c_str() << std::endl;
39 static void report(std::string_view message) {
trace(std::string(message)); }
44 static void report(std::string_view message);
49 static void report(std::string_view message) { std::ignore = message; }
64template <
typename ToType,
typename FromType,
typename ReportPolicy = ReportPolicyIgnore>
67 if constexpr (std::is_same_v<ToType, bool>)
69 return static_cast<bool>(from);
71 else if constexpr (std::is_floating_point_v<ToType> || std::is_floating_point_v<FromType>)
74 if (std::isless(from, std::numeric_limits<ToType>::lowest()))
76 ReportPolicy::report(
"Needed to truncate `from` as it's value was to small for ToType.");
77 return std::numeric_limits<ToType>::lowest();
80 if (std::isgreater(from, std::numeric_limits<ToType>::max()))
82 ReportPolicy::report(
"Needed to truncate `from` as it's value was to big for ToType.");
83 return std::numeric_limits<ToType>::max();
86 return static_cast<ToType
>(from);
92 ReportPolicy::report(
"Needed to truncate `from` as it's value was to small for ToType.");
93 return std::numeric_limits<ToType>::lowest();
98 ReportPolicy::report(
"Needed to truncate `from` as it's value was to big for ToType.");
99 return std::numeric_limits<ToType>::max();
102 return static_cast<ToType
>(from);
120template <
typename ToType,
typename ReportPolicy = ReportPolicyAssertion,
typename FromType>
138template <
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.
Definition checked_conversions.h:53
ToType conversionImpl(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:65
ToType ignoredLossyConversion(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:139
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:121
Definition checked_conversions.h:29
static void report(std::string message)
Definition checked_conversions.h:30
Definition checked_conversions.h:48
static void report(std::string_view message)
Definition checked_conversions.h:49
Definition checked_conversions.h:43
static void report(std::string_view message)
Definition checked_conversions.h:38
static void report(std::string_view message)
Definition checked_conversions.h:39