FireSTARR
Loading...
Searching...
No Matches
StrictType.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_INDEX_H
3#define FS_INDEX_H
4#include "stdafx.h"
5namespace fs
6{
7namespace units
8{
9template <size_t N>
11{
12 consteval explicit UnitType(const char (&str)[N]) { std::copy_n(str, N, value); }
13 char value[N];
14 auto operator<=>(const UnitType&) const = default;
15 bool operator==(const UnitType&) const = default;
16};
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"};
24}
28template <
29 class ConcreteType,
30 units::UnitType Units = units::Unitless,
31 class ValueType = MathSize,
32 int InvalidValue = -1>
34{
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()
42 {
43 return ConcreteType{static_cast<ValueType>(InvalidValue)};
44 };
45 ValueType value{static_cast<ValueType>(0.0)};
46 constexpr StrictType() = default;
47 constexpr explicit StrictType(const ValueType value) noexcept : value(value)
48 {
49 // HACK: ensure we're always using
50 // struct X : public StrictType<X>
51 static_assert(
52 std::is_same_v<decltype(*this), StrictType<ConcreteType, Units, ValueType, InvalidValue>&>,
53 "Different types"
54 );
55 static_assert(std::is_same_v<decltype(*this), T&>, "Different types");
56 static_assert(
57 std::is_same_v<T, StrictType<ConcreteType, Units, ValueType, InvalidValue>>, "Different types"
58 );
59 static_assert(std::is_convertible_v<ConcreteType, T>, "Different types");
60 // // doesn't work in this direction
61 // static_assert(std::is_convertible_v<T, ConcreteType>, "Different types");
62 }
63 constexpr StrictType(T&& rhs) noexcept = default;
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
70 {
71 // HACK: avoid narrowing conversion warning
72 return ConcreteType{static_cast<ValueType>(value + rhs.value)};
73 }
74 [[nodiscard]] constexpr ConcreteType operator-(const T& rhs) const noexcept
75 {
76 // HACK: avoid narrowing conversion warning
77 return ConcreteType{static_cast<ValueType>(value - rhs.value)};
78 }
79 // NOTE: should dividing by same type remove units?
80 [[nodiscard]] constexpr ConcreteType operator*(const ValueType& rhs) const noexcept
81 {
82 return ConcreteType{value * rhs};
83 }
84 [[nodiscard]] constexpr ConcreteType operator/(const ValueType& rhs) const noexcept
85 {
86 return ConcreteType{value / rhs};
87 }
88 constexpr T& operator+=(const T& rhs) noexcept
89 {
90 value += rhs.value;
91 return *this;
92 }
93 constexpr T& operator-=(const T& rhs) noexcept
94 {
95 value -= rhs.value;
96 return *this;
97 }
98 constexpr T& operator*=(const T& rhs) noexcept
99 {
100 value *= rhs.value;
101 return *this;
102 }
103 constexpr T& operator/=(const T& rhs) noexcept
104 {
105 value /= rhs.value;
106 return *this;
107 }
108 operator string() noexcept { return string(value); }
109};
110template <class ConcreteType, units::UnitType Units, class ValueType, int InvalidValue>
111[[nodiscard]] constexpr ConcreteType operator*(
112 const ValueType& rhs,
114) noexcept
115{
116 return ConcreteType{rhs * lhs.value};
117}
118template <class ConcreteType, units::UnitType Units, class ValueType, int InvalidValue>
119[[nodiscard]] constexpr ConcreteType abs(
120 const StrictType<ConcreteType, Units, ValueType, InvalidValue>& lhs
121) noexcept
122{
123 return ConcreteType{abs(lhs.value)};
124}
125// NOTE: dividing non-units by units makes no sense?
126// template <class ConcreteType, units::UnitType Units, class ValueType, int InvalidValue>
127// [[nodiscard]] constexpr ConcreteType operator/(
128// const ValueType& rhs,
129// const StrictType<ConcreteType, Units, ValueType, InvalidValue>& lhs
130// ) noexcept
131// {
132// return ConcreteType{rhs / lhs.value};
133// }
134template <class ConcreteType, fs::units::UnitType Units, class ValueType, int InvalidValue>
135std::ostream& operator<<(
136 std::ostream& os,
138)
139{
140 os << v.value;
141 return os;
142}
143}
144#endif
A wrapper around a ValueType to ensure correct types are used.
Definition StrictType.h:34
Definition StrictType.h:11