Sen API
Sen Libraries
Loading...
Searching...
No Matches
quantity.h
Go to the documentation of this file.
1// === quantity.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_QUANTITY_H
9#define SEN_CORE_BASE_QUANTITY_H
10
11// sen
12#include "compiler_macros.h"
16
17// std
18#include <string>
19
20namespace sen
21{
22
25
27template <typename T, typename D>
29{
30public:
31 using ValueType = T;
32
33public: // special members
34 constexpr SEN_MOVE_CONSTRUCT(Quantity) = default;
35 constexpr SEN_COPY_CONSTRUCT(Quantity) = default;
36 constexpr SEN_MOVE_ASSIGN(Quantity) = default;
37 constexpr SEN_COPY_ASSIGN(Quantity) = default;
38
39public:
41 constexpr Quantity() noexcept;
42
44 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
45 constexpr Quantity(U value) // NOLINT implicit conversions allowed
46 {
47 checkAndSet(std_util::checkedConversion<T>(value));
48 }
49
50 ~Quantity() = default;
51
52public: // getters and setters
54 [[nodiscard]] constexpr T get() const noexcept { return value_; }
55
57 constexpr operator T() const noexcept { return value_; } // NOLINT implicit conversions allowed
58
60 [[nodiscard]] constexpr bool isValid() const noexcept { return validity_; }
61
63 constexpr explicit operator bool() const noexcept { return validity_; }
64
66 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
67 constexpr Quantity& operator=(U other)
68 {
69 checkAndSet(other);
70 return *this;
71 }
72
74 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
75 constexpr void set(U other)
76 {
77 checkAndSet(other);
78 }
79
81 constexpr void setValid(bool valid) { validity_ = valid; }
82
83public: // comparison operators
84 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
85 constexpr bool operator==(const U& other) const
86 {
87 return value_ == other;
88 }
89
90 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
91 constexpr bool operator!=(const U& other) const
92 {
93 return value_ != other;
94 }
95
96 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
97 constexpr bool operator<(const U& other) const
98 {
99 return value_ < other;
100 }
101
102 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
103 constexpr bool operator<=(const U& other) const
104 {
105 return value_ <= other;
106 }
107
108 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
109 constexpr bool operator>(const U& other) const
110 {
111 return value_ > other;
112 }
113
114 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
115 constexpr bool operator>=(const U& other) const
116 {
117 return value_ >= other;
118 }
119
120private:
121 SEN_ALWAYS_INLINE void checkAndSet(T value);
122
123private:
124 T value_ = {};
125 bool validity_ = true;
126};
127
128template <typename T, typename D>
129struct ShouldBePassedByValue<Quantity<T, D>>: std::true_type
130{
131};
132
135#define SEN_RANGED_QUANTITY(class_name, value_type, min_value, max_value) \
136 struct class_name: public sen::Quantity<value_type, class_name> \
137 { \
138 static constexpr bool hasRange = true; \
139 static constexpr value_type min = min_value; \
140 static constexpr value_type max = max_value; \
141 static_assert(min <= max); \
142 \
143 using Quantity::Quantity; \
144 };
145
148#define SEN_NON_RANGED_QUANTITY(class_name, value_type) \
149 struct class_name: public sen::Quantity<value_type, class_name> \
150 { \
151 static constexpr bool hasRange = false; \
152 using Quantity::Quantity; \
153 };
154
156
157//----------------------------------------------------------------------------------------------------------------------
158// Inline implementation
159//----------------------------------------------------------------------------------------------------------------------
160
161template <typename T, typename D>
162constexpr Quantity<T, D>::Quantity() noexcept
163{
164 if constexpr (D::hasRange)
165 {
166 if ((T {} < D::min) || (T {} > D::max))
167 {
168 value_ = D::min;
169 }
170 }
171}
172
173template <typename T, typename D>
174void Quantity<T, D>::checkAndSet(T value)
175{
176 if constexpr (D::hasRange)
177 {
178 if ((value < D::min) || (value > D::max))
179 {
180 std::string err;
181 err.append(std::to_string(value));
182 err.append(" is out of the range [");
183 err.append(std::to_string(D::min));
184 err.append(", ");
185 err.append(std::to_string(D::max));
186 err.append("]");
189 }
190 }
191 value_ = value;
192}
193
194} // namespace sen
195
196#endif // SEN_CORE_BASE_QUANTITY_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
CRTP class that wraps T to ensure it's value stays within a certain range.
Definition quantity.h:29
constexpr Quantity & operator=(U other)
Sets the value. Throws std::exception if out of range.
Definition quantity.h:67
constexpr void setValid(bool valid)
Sets the validity of data.
Definition quantity.h:81
constexpr bool operator>(const U &other) const
Definition quantity.h:109
T ValueType
Definition quantity.h:31
constexpr bool isValid() const noexcept
The validity of data.
Definition quantity.h:60
~Quantity()=default
constexpr void set(U other)
Sets the value. Throws std::exception if out of range.
Definition quantity.h:75
constexpr Quantity() noexcept
Default-constructs the internal value. Lower bound is used if not within range.
Definition quantity.h:162
constexpr bool operator!=(const U &other) const
Definition quantity.h:91
constexpr bool operator<(const U &other) const
Definition quantity.h:97
constexpr bool operator==(const U &other) const
Definition quantity.h:85
constexpr bool operator<=(const U &other) const
Definition quantity.h:103
constexpr T get() const noexcept
The stored value.
Definition quantity.h:54
constexpr bool operator>=(const U &other) const
Definition quantity.h:115
void throwRuntimeError(const std::string &err)
Throws std::exception that attempts to collect the stack trace. We also wrap it to avoid including st...
#define SEN_UNREACHABLE()
Definition compiler_macros.h:420
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 assert.h:17
STL namespace.
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34