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"
15
16// std
17#include <string>
18
19namespace sen
20{
21
24
26template <typename T, typename D>
28{
29public:
30 using ValueType = T;
31
32public: // special members
33 constexpr SEN_MOVE_CONSTRUCT(Quantity) = default;
34 constexpr SEN_COPY_CONSTRUCT(Quantity) = default;
35 constexpr SEN_MOVE_ASSIGN(Quantity) = default;
36 constexpr SEN_COPY_ASSIGN(Quantity) = default;
37
38public:
40 constexpr Quantity() noexcept;
41
43 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
44 constexpr Quantity(U value) // NOLINT implicit conversions allowed
45 {
46 checkAndSet(value);
47 }
48
49 ~Quantity() = default;
50
51public: // getters and setters
53 [[nodiscard]] constexpr T get() const noexcept { return value_; }
54
56 constexpr operator T() const noexcept { return value_; } // NOLINT implicit conversions allowed
57
59 [[nodiscard]] constexpr bool isValid() const noexcept { return validity_; }
60
62 constexpr explicit operator bool() const noexcept { return validity_; }
63
65 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
66 constexpr Quantity& operator=(U other)
67 {
68 checkAndSet(other);
69 return *this;
70 }
71
73 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
74 constexpr void set(U other)
75 {
76 checkAndSet(other);
77 }
78
80 constexpr void setValid(bool valid) { validity_ = valid; }
81
82public: // comparison operators
83 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
84 constexpr bool operator==(const U& other) const
85 {
86 return value_ == other;
87 }
88
89 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
90 constexpr bool operator!=(const U& other) const
91 {
92 return value_ != other;
93 }
94
95 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
96 constexpr bool operator<(const U& other) const
97 {
98 return value_ < other;
99 }
100
101 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
102 constexpr bool operator<=(const U& other) const
103 {
104 return value_ <= other;
105 }
106
107 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
108 constexpr bool operator>(const U& other) const
109 {
110 return value_ > other;
111 }
112
113 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
114 constexpr bool operator>=(const U& other) const
115 {
116 return value_ >= other;
117 }
118
119private:
120 SEN_ALWAYS_INLINE void checkAndSet(T value);
121
122private:
123 T value_ = {};
124 bool validity_ = true;
125};
126
127template <typename T, typename D>
128struct ShouldBePassedByValue<Quantity<T, D>>: std::true_type
129{
130};
131
134#define SEN_RANGED_QUANTITY(class_name, value_type, min_value, max_value) \
135 struct class_name: public sen::Quantity<value_type, class_name> \
136 { \
137 static constexpr bool hasRange = true; \
138 static constexpr value_type min = min_value; \
139 static constexpr value_type max = max_value; \
140 static_assert(min <= max); \
141 \
142 using Quantity::Quantity; \
143 };
144
147#define SEN_NON_RANGED_QUANTITY(class_name, value_type) \
148 struct class_name: public sen::Quantity<value_type, class_name> \
149 { \
150 static constexpr bool hasRange = false; \
151 using Quantity::Quantity; \
152 };
153
155
156//----------------------------------------------------------------------------------------------------------------------
157// Inline implementation
158//----------------------------------------------------------------------------------------------------------------------
159
160template <typename T, typename D>
161constexpr Quantity<T, D>::Quantity() noexcept
162{
163 if constexpr (D::hasRange)
164 {
165 if ((T {} < D::min) || (T {} > D::max))
166 {
167 value_ = D::min;
168 }
169 }
170}
171
172template <typename T, typename D>
173void Quantity<T, D>::checkAndSet(T value)
174{
175 if constexpr (D::hasRange)
176 {
177 if ((value < D::min) || (value > D::max))
178 {
179 std::string err;
180 err.append(std::to_string(value));
181 err.append(" is out of the range [");
182 err.append(std::to_string(D::min));
183 err.append(", ");
184 err.append(std::to_string(D::max));
185 err.append("]");
188 }
189 }
190 value_ = value;
191}
192
193} // namespace sen
194
195#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:28
constexpr Quantity & operator=(U other)
Sets the value. Throws std::exception if out of range.
Definition quantity.h:66
constexpr void setValid(bool valid)
Sets the validity of data.
Definition quantity.h:80
constexpr bool operator>(const U &other) const
Definition quantity.h:108
T ValueType
Definition quantity.h:30
constexpr bool isValid() const noexcept
The validity of data.
Definition quantity.h:59
~Quantity()=default
constexpr void set(U other)
Sets the value. Throws std::exception if out of range.
Definition quantity.h:74
constexpr Quantity() noexcept
Default-constructs the internal value. Lower bound is used if not within range.
Definition quantity.h:161
constexpr bool operator!=(const U &other) const
Definition quantity.h:90
constexpr bool operator<(const U &other) const
Definition quantity.h:96
constexpr bool operator==(const U &other) const
Definition quantity.h:84
constexpr bool operator<=(const U &other) const
Definition quantity.h:102
constexpr T get() const noexcept
The stored value.
Definition quantity.h:53
constexpr bool operator>=(const U &other) const
Definition quantity.h:114
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
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