OpenTTD Source 20261112-master-gbcbec5e63f
strong_typedef_type.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef STRONG_TYPEDEF_TYPE_HPP
11#define STRONG_TYPEDEF_TYPE_HPP
12
13namespace StrongType {
17 struct Compare {
18 template <typename TType, typename TBaseType>
19 struct mixin {
20 friend constexpr bool operator ==(const TType &lhs, const TType &rhs) { return lhs.value == rhs.value; }
21 friend constexpr bool operator ==(const TType &lhs, const TBaseType &rhs) { return lhs.value == rhs; }
22
23 friend constexpr auto operator <=>(const TType &lhs, const TType &rhs) { return lhs.value <=> rhs.value; }
24 friend constexpr auto operator <=>(const TType &lhs, const TBaseType &rhs) { return lhs.value <=> rhs; }
25 };
26 };
27
35 struct Integer {
36 template <typename TType, typename TBaseType>
37 struct mixin {
38 friend constexpr TType &operator ++(TType &lhs) { lhs.value++; return lhs; }
39 friend constexpr TType &operator --(TType &lhs) { lhs.value--; return lhs; }
40 friend constexpr TType operator ++(TType &lhs, int) { TType res = lhs; lhs.value++; return res; }
41 friend constexpr TType operator --(TType &lhs, int) { TType res = lhs; lhs.value--; return res; }
42
43 friend constexpr TType &operator +=(TType &lhs, const TType &rhs) { lhs.value += rhs.value; return lhs; }
44 friend constexpr TType operator +(const TType &lhs, const TType &rhs) { return TType(lhs.value + rhs.value); }
45 friend constexpr TType operator +(const TType &lhs, const TBaseType &rhs) { return TType(lhs.value + rhs); }
46
47 friend constexpr TType &operator -=(TType &lhs, const TType &rhs) { lhs.value -= rhs.value; return lhs; }
48 friend constexpr TType operator -(const TType &lhs, const TType &rhs) { return TType(lhs.value - rhs.value); }
49 friend constexpr TType operator -(const TType &lhs, const TBaseType &rhs) { return TType(lhs.value - rhs); }
50
51 /* For most new types, the rest of the operators make no sense. For example,
52 * what does it actually mean to multiply a Year with a value. Or to do a
53 * bitwise OR on a Date. Or to divide a TileIndex by 2. Conceptually, they
54 * don't really mean anything. So force the user to first cast it to the
55 * base type, so the operation no longer returns the new Typedef. */
56
57 constexpr TType &operator *=(const TType &rhs) = delete;
58 constexpr TType operator *(const TType &rhs) = delete;
59 constexpr TType operator *(const TBaseType &rhs) = delete;
60
61 constexpr TType &operator /=(const TType &rhs) = delete;
62 constexpr TType operator /(const TType &rhs) = delete;
63 constexpr TType operator /(const TBaseType &rhs) = delete;
64
65 constexpr TType &operator %=(const TType &rhs) = delete;
66 constexpr TType operator %(const TType &rhs) = delete;
67 constexpr TType operator %(const TBaseType &rhs) = delete;
68
69 constexpr TType &operator &=(const TType &rhs) = delete;
70 constexpr TType operator &(const TType &rhs) = delete;
71 constexpr TType operator &(const TBaseType &rhs) = delete;
72
73 constexpr TType &operator |=(const TType &rhs) = delete;
74 constexpr TType operator |(const TType &rhs) = delete;
75 constexpr TType operator |(const TBaseType &rhs) = delete;
76
77 constexpr TType &operator ^=(const TType &rhs) = delete;
78 constexpr TType operator ^(const TType &rhs) = delete;
79 constexpr TType operator ^(const TBaseType &rhs) = delete;
80
81 constexpr TType &operator <<=(const TType &rhs) = delete;
82 constexpr TType operator <<(const TType &rhs) = delete;
83 constexpr TType operator <<(const TBaseType &rhs) = delete;
84
85 constexpr TType &operator >>=(const TType &rhs) = delete;
86 constexpr TType operator >>(const TType &rhs) = delete;
87 constexpr TType operator >>(const TBaseType &rhs) = delete;
88
89 constexpr TType operator ~() = delete;
90 constexpr TType operator -() = delete;
91 };
92 };
93
101 template <typename TCompatibleType>
102 struct Compatible {
103 template <typename TType, typename TBaseType>
104 struct mixin {
105 friend constexpr bool operator ==(const TType &lhs, TCompatibleType rhs) { return lhs.value == static_cast<TBaseType>(rhs); }
106 friend constexpr auto operator <=>(const TType &lhs, TCompatibleType rhs) { return lhs.value <=> static_cast<TBaseType>(rhs); }
107
108 friend constexpr TType operator +(const TType &lhs, TCompatibleType rhs) { return TType(lhs.value + rhs); }
109 friend constexpr TType operator -(const TType &lhs, TCompatibleType rhs) { return TType(lhs.value - rhs); }
110 };
111 };
112
127 template <typename TBaseType, typename TTag, typename... TProperties>
128 struct EMPTY_BASES Typedef : public TProperties::template mixin<Typedef<TBaseType, TTag, TProperties...>, TBaseType>... {
129 using BaseType = TBaseType;
130
131 constexpr Typedef() = default;
132 constexpr Typedef(const Typedef &) = default;
133 constexpr Typedef(Typedef &&) = default;
134
135 explicit constexpr Typedef(const TBaseType &value) : value(value) {}
136
137 constexpr Typedef &operator =(const Typedef &rhs) { this->value = rhs.value; return *this; }
138 constexpr Typedef &operator =(Typedef &&rhs) { this->value = std::move(rhs.value); return *this; }
139
140 /* Only allow conversion to BaseType via method. */
141 constexpr TBaseType base() const noexcept { return this->value; }
142
143 /* Only allow TProperties classes access to the internal value. Everyone else needs to call .base(). */
144 friend struct Compare;
145 friend struct Integer;
146 template <typename TCompatibleType> friend struct Compatible;
147
148/* GCC / MSVC don't pick up on the "friend struct" above, where CLang does.
149 * As in our CI we compile for all three targets, it is sufficient to have one
150 * that errors on this; but nobody should be using "value" directly. Instead,
151 * use base() to convert to the base type. */
152#ifdef __clang__
153 protected:
154#endif /* __clang__ */
155 TBaseType value{};
156 };
157}
158
172template <typename TBaseType, typename... TProperties>
173struct std::hash<StrongType::Typedef<TBaseType, TProperties...>> {
181 {
182 return std::hash<TBaseType>()(t.base());
183 }
184};
185
186#endif /* STRONG_TYPEDEF_TYPE_HPP */
Mix-in which makes the new Typedef comparable with itself and its base type.
Mix-in which makes the new Typedef compatible with another type (which is not the base type).
Mix-in which makes the new Typedef behave more like an integer.
Templated helper to make a type-safe 'typedef' representing a single POD value.
std::size_t operator()(const StrongType::Typedef< TBaseType, TProperties... > &t) const noexcept
Computes the hash value for a StrongType::Typedef instance.