Sen API
Sen Libraries
Loading...
Searching...
No Matches
checked_conversions.h
Go to the documentation of this file.
1// === checked_conversions.h ===========================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_CHECKED_CONVERSIONS_H
9#define SEN_CORE_BASE_CHECKED_CONVERSIONS_H
10
11// sen
14
15// std
16#include <cassert>
17#include <cmath>
18#include <iostream>
19#include <limits>
20#include <string>
21#include <string_view>
22#include <tuple>
23#include <type_traits>
24
25namespace sen::std_util
26{
27
29{
30 static void report(std::string message)
31 {
32 std::cerr << message.c_str() << std::endl;
33 SEN_ASSERT(false);
34 }
35};
36
38{
39 static void report(std::string_view message) { trace(std::string(message)); }
40};
41
43{
44 static void report(std::string_view message);
45};
46
48{
49 static void report(std::string_view message) { std::ignore = message; }
50};
51
52namespace impl
53{
64template <typename ToType, typename FromType, typename ReportPolicy = ReportPolicyIgnore>
65ToType conversionImpl(FromType from)
66{
67 if constexpr (std::is_same_v<ToType, bool>)
68 {
69 return static_cast<bool>(from);
70 }
71 else if constexpr (std::is_floating_point_v<ToType> || std::is_floating_point_v<FromType>)
72 {
73
74 if (std::isless(from, std::numeric_limits<ToType>::lowest()))
75 {
76 ReportPolicy::report("Needed to truncate `from` as it's value was to small for ToType.");
77 return std::numeric_limits<ToType>::lowest();
78 }
79
80 if (std::isgreater(from, std::numeric_limits<ToType>::max()))
81 {
82 ReportPolicy::report("Needed to truncate `from` as it's value was to big for ToType.");
83 return std::numeric_limits<ToType>::max();
84 }
85
86 return static_cast<ToType>(from);
87 }
88 else
89 {
90 if (sen::std_util::cmp_less(from, std::numeric_limits<ToType>::lowest()))
91 {
92 ReportPolicy::report("Needed to truncate `from` as it's value was to small for ToType.");
93 return std::numeric_limits<ToType>::lowest();
94 }
95
96 if (sen::std_util::cmp_greater(from, std::numeric_limits<ToType>::max()))
97 {
98 ReportPolicy::report("Needed to truncate `from` as it's value was to big for ToType.");
99 return std::numeric_limits<ToType>::max();
100 }
101
102 return static_cast<ToType>(from);
103 }
104}
105
106} // namespace impl
107
120template <typename ToType, typename ReportPolicy = ReportPolicyAssertion, typename FromType>
121[[nodiscard]] ToType checkedConversion(FromType from)
122{
124}
125
138template <typename ToType, typename ReportPolicy = ReportPolicyIgnore, typename FromType>
139[[nodiscard]] ToType ignoredLossyConversion(FromType from)
140{
142}
143
144} // namespace sen::std_util
145
146#endif // SEN_CORE_BASE_CHECKED_CONVERSIONS_H
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
Definition bits.h:26
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