8#ifndef SEN_CORE_BASE_QUANTITY_H
9#define SEN_CORE_BASE_QUANTITY_H
27template <
typename T,
typename D>
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;
44 template <typename U, typename =
std::enable_if_t<
std::is_convertible_v<U, T>>>
54 [[nodiscard]]
constexpr T
get() const noexcept {
return value_; }
57 constexpr operator T() const noexcept {
return value_; }
60 [[nodiscard]]
constexpr bool isValid() const noexcept {
return validity_; }
63 constexpr explicit operator bool() const noexcept {
return validity_; }
66 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
74 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
75 constexpr void set(U other)
81 constexpr void setValid(
bool valid) { validity_ = valid; }
84 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
87 return value_ == other;
90 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
93 return value_ != other;
96 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
99 return value_ < other;
102 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
105 return value_ <= other;
108 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
111 return value_ > other;
114 template <
typename U,
typename = std::enable_if_t<std::is_convertible_v<U, T>>>
117 return value_ >= other;
121 SEN_ALWAYS_INLINE
void checkAndSet(T value);
125 bool validity_ =
true;
128template <
typename T,
typename D>
135#define SEN_RANGED_QUANTITY(class_name, value_type, min_value, max_value) \
136 struct class_name: public sen::Quantity<value_type, class_name> \
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); \
143 using Quantity::Quantity; \
148#define SEN_NON_RANGED_QUANTITY(class_name, value_type) \
149 struct class_name: public sen::Quantity<value_type, class_name> \
151 static constexpr bool hasRange = false; \
152 using Quantity::Quantity; \
161template <
typename T,
typename D>
164 if constexpr (D::hasRange)
166 if ((T {} < D::min) || (T {} > D::max))
173template <
typename T,
typename D>
174void Quantity<T, D>::checkAndSet(T value)
176 if constexpr (D::hasRange)
178 if ((value < D::min) || (value > D::max))
181 err.append(std::to_string(value));
182 err.append(
" is out of the range [");
183 err.append(std::to_string(D::min));
185 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: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
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
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34