8#ifndef SEN_CORE_BASE_QUANTITY_H
9#define SEN_CORE_BASE_QUANTITY_H
26template <
typename T,
typename D>
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;
43 template <typename U, typename =
std::enable_if_t<
std::is_convertible_v<U, T>>>
53 [[nodiscard]]
constexpr T
get() const noexcept {
return value_; }
56 constexpr operator T() const noexcept {
return value_; }
59 [[nodiscard]]
constexpr bool isValid() const noexcept {
return validity_; }
62 constexpr explicit operator bool() const noexcept {
return validity_; }
65 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
73 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
74 constexpr void set(U other)
80 constexpr void setValid(
bool valid) { validity_ = valid; }
83 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
86 return value_ == other;
89 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
92 return value_ != other;
95 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
98 return value_ < other;
101 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
104 return value_ <= other;
107 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
110 return value_ > other;
113 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
116 return value_ >= other;
120 SEN_ALWAYS_INLINE
void checkAndSet(T value);
124 bool validity_ =
true;
127template <
typename T,
typename D>
134#define SEN_RANGED_QUANTITY(class_name, value_type, min_value, max_value) \
135 struct class_name: public sen::Quantity<value_type, class_name> \
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); \
142 using Quantity::Quantity; \
147#define SEN_NON_RANGED_QUANTITY(class_name, value_type) \
148 struct class_name: public sen::Quantity<value_type, class_name> \
150 static constexpr bool hasRange = false; \
151 using Quantity::Quantity; \
160template <
typename T,
typename D>
163 if constexpr (D::hasRange)
165 if ((T {} < D::min) || (T {} > D::max))
172template <
typename T,
typename D>
173void Quantity<T, D>::checkAndSet(T value)
175 if constexpr (D::hasRange)
177 if ((value < D::min) || (value > D::max))
180 err.append(std::to_string(value));
181 err.append(
" is out of the range [");
182 err.append(std::to_string(D::min));
184 err.append(std::to_string(D::max));
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
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
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34