8#ifndef SEN_CORE_BASE_CHECKED_CONVERSIONS_H
9#define SEN_CORE_BASE_CHECKED_CONVERSIONS_H
34 static void report(
const std::string_view message)
43 static void report(std::string_view message) {
trace(std::string(message)); }
48 static void report(std::string_view message);
53 static void report(std::string_view message) { std::ignore = message; }
74template <
typename ToType,
typename FromType,
typename ReportPolicy = ReportPolicyIgnore>
77 if constexpr (std::is_same_v<ToType, bool>)
79 return static_cast<bool>(from);
81 else if constexpr (std::is_floating_point_v<ToType> || std::is_floating_point_v<FromType>)
83 if constexpr (std::is_same_v<FromType, ToType>)
92 if constexpr (!std::is_floating_point_v<ToType>)
94 if (std::isnan(
static_cast<float64_t>(from)))
96 ReportPolicy::report(
"the value is not a number");
101 if (std::isless(
static_cast<float64_t>(from),
static_cast<float64_t>(std::numeric_limits<ToType>::lowest())))
103 ReportPolicy::report(
"the value is too small for the target type");
104 return std::numeric_limits<ToType>::lowest();
107 if (std::isgreater(
static_cast<float64_t>(from),
static_cast<float64_t>(std::numeric_limits<ToType>::max())))
109 ReportPolicy::report(
"the value is too large for the target type");
110 return std::numeric_limits<ToType>::max();
113 return static_cast<ToType
>(from);
120 ReportPolicy::report(
"the value is too small for the target type");
121 return std::numeric_limits<ToType>::lowest();
126 ReportPolicy::report(
"the value is too large for the target type");
127 return std::numeric_limits<ToType>::max();
130 return static_cast<ToType
>(from);
148template <
typename ToType,
typename ReportPolicy = ReportPolicyAssertion,
typename FromType>
166template <
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:41
void throwRuntimeError(const std::string &err)
Throws an exception that attempts to collect the stack trace.
void trace()
Prints the current stack trace to stderr.
double float64_t
Definition numbers.h:17
Definition checked_conversions.h:63
ToType conversionImpl(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:75
ToType ignoredLossyConversion(FromType from)
Safely convert between two different integral types. If the value stored in from cannot be represente...
Definition checked_conversions.h:167
void reportToStandardError(std::string_view message)
Writes to standard error. Out of line so this header, which every user of the conversion helpers incl...
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:149
Definition checked_conversions.h:33
static void report(const std::string_view message)
Definition checked_conversions.h:34
Definition checked_conversions.h:52
static void report(std::string_view message)
Definition checked_conversions.h:53
Definition checked_conversions.h:47
static void report(std::string_view message)
Refuses the conversion instead of returning a clamped value.
Definition checked_conversions.h:58
static void report(std::string_view message)
Definition checked_conversions.h:59
Definition checked_conversions.h:42
static void report(std::string_view message)
Definition checked_conversions.h:43