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
15
16// std
17#include <cassert>
18#include <cmath>
19#include <iostream>
20#include <limits>
21#include <string>
22#include <string_view>
23#include <tuple>
24#include <type_traits>
25
26namespace sen::std_util
27{
28
30{
31 static void report(std::string message)
32 {
33 std::cerr << message.c_str() << std::endl;
34 SEN_ASSERT(false);
35 }
36};
37
39{
40 static void report(std::string_view message) { trace(std::string(message)); }
41};
42
44{
45 static void report(std::string_view message);
46};
47
49{
50 static void report(std::string_view message) { std::ignore = message; }
51};
52
53namespace impl
54{
65template <typename ToType, typename FromType, typename ReportPolicy = ReportPolicyIgnore>
66ToType conversionImpl(FromType from)
67{
68 if constexpr (std::is_same_v<ToType, bool>)
69 {
70 return static_cast<bool>(from);
71 }
72 else if constexpr (std::is_floating_point_v<ToType> || std::is_floating_point_v<FromType>)
73 {
74 if constexpr (std::is_same_v<FromType, ToType>)
75 {
76 return from;
77 }
78 else
79 {
80 if (std::isless(static_cast<float64_t>(from), static_cast<float64_t>(std::numeric_limits<ToType>::lowest())))
81 {
82 ReportPolicy::report("Needed to truncate `from` as it's value was to small for ToType.");
83 return std::numeric_limits<ToType>::lowest();
84 }
85
86 if (std::isgreater(static_cast<float64_t>(from), static_cast<float64_t>(std::numeric_limits<ToType>::max())))
87 {
88 ReportPolicy::report("Needed to truncate `from` as it's value was to big for ToType.");
89 return std::numeric_limits<ToType>::max();
90 }
91
92 return static_cast<ToType>(from);
93 }
94 }
95 else
96 {
97 if (sen::std_util::cmp_less(from, std::numeric_limits<ToType>::lowest()))
98 {
99 ReportPolicy::report("Needed to truncate `from` as it's value was to small for ToType.");
100 return std::numeric_limits<ToType>::lowest();
101 }
102
103 if (sen::std_util::cmp_greater(from, std::numeric_limits<ToType>::max()))
104 {
105 ReportPolicy::report("Needed to truncate `from` as it's value was to big for ToType.");
106 return std::numeric_limits<ToType>::max();
107 }
108
109 return static_cast<ToType>(from);
110 }
111}
112
113} // namespace impl
114
127template <typename ToType, typename ReportPolicy = ReportPolicyAssertion, typename FromType>
128[[nodiscard]] ToType checkedConversion(FromType from)
129{
131}
132
145template <typename ToType, typename ReportPolicy = ReportPolicyIgnore, typename FromType>
146[[nodiscard]] ToType ignoredLossyConversion(FromType from)
147{
149}
150
151} // namespace sen::std_util
152
153#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.
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
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: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