12 consteval explicit UnitType(
const char (&str)[N]) { std::copy_n(str, N, value); }
14 auto operator<=>(
const UnitType&)
const =
default;
15 bool operator==(
const UnitType&)
const =
default;
17static constexpr UnitType Unitless{
"unitless"};
18static constexpr UnitType Celsius{
"degrees Celsius"};
19static constexpr UnitType Percent{
"percent"};
20static constexpr UnitType KilometresPerHour{
"km/h"};
21static constexpr UnitType CompassDegrees{
"degrees"};
22static constexpr UnitType CompassRadians{
"radians"};
23static constexpr UnitType MillimetresAccumulated{
"mm accumulated"};
30 units::UnitType Units = units::Unitless,
31 class ValueType = MathSize,
32 int InvalidValue = -1>
36 using concrete_type = ConcreteType;
37 static constexpr auto units = Units;
38 using value_type = ValueType;
39 static constexpr auto invalid_value = InvalidValue;
40 static consteval ConcreteType Zero() {
return ConcreteType{0.0}; };
41 static consteval ConcreteType Invalid()
43 return ConcreteType{
static_cast<ValueType
>(InvalidValue)};
45 ValueType value{
static_cast<ValueType
>(0.0)};
47 constexpr explicit StrictType(
const ValueType value) noexcept : value(value)
55 static_assert(std::is_same_v<
decltype(*this),
T&>,
"Different types");
57 std::is_same_v<T, StrictType<ConcreteType, Units, ValueType, InvalidValue>>,
"Different types"
59 static_assert(std::is_convertible_v<ConcreteType, T>,
"Different types");
64 constexpr StrictType(
const T& rhs)
noexcept =
default;
65 T& operator=(
T&& rhs)
noexcept =
default;
66 T& operator=(
const T& rhs)
noexcept =
default;
67 constexpr auto operator==(
const T& rhs)
const {
return value == rhs.value; }
68 constexpr auto operator<=>(
const T& rhs)
const {
return value <=> rhs.value; }
69 [[nodiscard]]
constexpr ConcreteType operator+(
const T& rhs)
const noexcept
72 return ConcreteType{
static_cast<ValueType
>(value + rhs.value)};
74 [[nodiscard]]
constexpr ConcreteType operator-(
const T& rhs)
const noexcept
77 return ConcreteType{
static_cast<ValueType
>(value - rhs.value)};
80 [[nodiscard]]
constexpr ConcreteType operator*(
const ValueType& rhs)
const noexcept
82 return ConcreteType{value * rhs};
84 [[nodiscard]]
constexpr ConcreteType operator/(
const ValueType& rhs)
const noexcept
86 return ConcreteType{value / rhs};
88 constexpr T& operator+=(
const T& rhs)
noexcept
93 constexpr T& operator-=(
const T& rhs)
noexcept
98 constexpr T& operator*=(
const T& rhs)
noexcept
103 constexpr T& operator/=(
const T& rhs)
noexcept
108 operator string()
noexcept {
return string(value); }
110template <
class ConcreteType, units::UnitType Units,
class ValueType,
int Inval
idValue>
111[[nodiscard]]
constexpr ConcreteType operator*(
112 const ValueType& rhs,
116 return ConcreteType{rhs * lhs.value};
118template <
class ConcreteType, units::UnitType Units,
class ValueType,
int Inval
idValue>
119[[nodiscard]]
constexpr ConcreteType abs(
120 const StrictType<ConcreteType, Units, ValueType, InvalidValue>& lhs
123 return ConcreteType{abs(lhs.value)};
134template <
class ConcreteType, fs::units::UnitType Units,
class ValueType,
int Inval
idValue>
135std::ostream& operator<<(
A wrapper around a ValueType to ensure correct types are used.
Definition StrictType.h:34
Definition StrictType.h:11