OpenTTD Source: src/core/strong_typedef_type.hpp Source File

OpenTTD Source 15.0-RC2

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
10#ifndef STRONG_TYPEDEF_TYPE_HPP
11#define STRONG_TYPEDEF_TYPE_HPP
12
13#include "../3rdparty/fmt/format.h"
14
15namespace StrongType {
19 struct Compare {
20 template <typename TType, typename TBaseType>
21 struct mixin {
22 friend constexpr bool operator ==(const TType &lhs, const TType &rhs) { return lhs.value == rhs.value; }
23 friend constexpr bool operator ==(const TType &lhs, const TBaseType &rhs) { return lhs.value == rhs; }
24
25 friend constexpr auto operator <=>(const TType &lhs, const TType &rhs) { return lhs.value <=> rhs.value; }
26 friend constexpr auto operator <=>(const TType &lhs, const TBaseType &rhs) { return lhs.value <=> rhs; }
27 };
28 };
29
37 struct Integer {
38 template <typename TType, typename TBaseType>
39 struct mixin {
40 friend constexpr TType &operator ++(TType &lhs) { lhs.value++; return lhs; }
41 friend constexpr TType &operator --(TType &lhs) { lhs.value--; return lhs; }
42 friend constexpr TType operator ++(TType &lhs, int) { TType res = lhs; lhs.value++; return res; }
43 friend constexpr TType operator --(TType &lhs, int) { TType res = lhs; lhs.value--; return res; }
44
45 friend constexpr TType &operator +=(TType &lhs, const TType &rhs) { lhs.value += rhs.value; return lhs; }
46 friend constexpr TType operator +(const TType &lhs, const TType &rhs) { return TType(lhs.value + rhs.value); }
47 friend constexpr TType operator +(const TType &lhs, const TBaseType &rhs) { return TType(lhs.value + rhs); }
48
49 friend constexpr TType &operator -=(TType &lhs, const TType &rhs) { lhs.value -= rhs.value; return lhs; }
50 friend constexpr TType operator -(const TType &lhs, const TType &rhs) { return TType(lhs.value - rhs.value); }
51 friend constexpr TType operator -(const TType &lhs, const TBaseType &rhs) { return TType(lhs.value - rhs); }
52
53 /* For most new types, the rest of the operators make no sense. For example,
54 * what does it actually mean to multiply a Year with a value. Or to do a
55 * bitwise OR on a Date. Or to divide a TileIndex by 2. Conceptually, they
56 * don't really mean anything. So force the user to first cast it to the
57 * base type, so the operation no longer returns the new Typedef. */
58
59 constexpr TType &operator *=(const TType &rhs) = delete;
60 constexpr TType operator *(const TType &rhs) = delete;
61 constexpr TType operator *(const TBaseType &rhs) = delete;
62
63 constexpr TType &operator /=(const TType &rhs) = delete;
64 constexpr TType operator /(const TType &rhs) = delete;
65 constexpr TType operator /(const TBaseType &rhs) = delete;
66
67 constexpr TType &operator %=(const TType &rhs) = delete;
68 constexpr TType operator %(const TType &rhs) = delete;
69 constexpr TType operator %(const TBaseType &rhs) = delete;
70
71 constexpr TType &operator &=(const TType &rhs) = delete;
72 constexpr TType operator &(const TType &rhs) = delete;
73 constexpr TType operator &(const TBaseType &rhs) = delete;
74
75 constexpr TType &operator |=(const TType &rhs) = delete;
76 constexpr TType operator |(const TType &rhs) = delete;
77 constexpr TType operator |(const TBaseType &rhs) = delete;
78
79 constexpr TType &operator ^=(const TType &rhs) = delete;
80 constexpr TType operator ^(const TType &rhs) = delete;
81 constexpr TType operator ^(const TBaseType &rhs) = delete;
82
83 constexpr TType &operator <<=(const TType &rhs) = delete;
84 constexpr TType operator <<(const TType &rhs) = delete;
85 constexpr TType operator <<(const TBaseType &rhs) = delete;
86
87 constexpr TType &operator >>=(const TType &rhs) = delete;
88 constexpr TType operator >>(const TType &rhs) = delete;
89 constexpr TType operator >>(const TBaseType &rhs) = delete;
90
91 constexpr TType operator ~() = delete;
92 constexpr TType operator -() = delete;
93 };
94 };
95
103 template <typename TCompatibleType>
104 struct Compatible {
105 template <typename TType, typename TBaseType>
106 struct mixin {
107 friend constexpr bool operator ==(const TType &lhs, TCompatibleType rhs) { return lhs.value == static_cast<TBaseType>(rhs); }
108 friend constexpr auto operator <=>(const TType &lhs, TCompatibleType rhs) { return lhs.value <=> static_cast<TBaseType>(rhs); }
109
110 friend constexpr TType operator +(const TType &lhs, TCompatibleType rhs) { return TType(lhs.value + rhs); }
111 friend constexpr TType operator -(const TType &lhs, TCompatibleType rhs) { return TType(lhs.value - rhs); }
112 };
113 };
114
129 template <typename TBaseType, typename TTag, typename... TProperties>
130 struct EMPTY_BASES Typedef : public TProperties::template mixin<Typedef<TBaseType, TTag, TProperties...>, TBaseType>... {
131 using BaseType = TBaseType;
132
133 constexpr Typedef() = default;
134 constexpr Typedef(const Typedef &) = default;
135 constexpr Typedef(Typedef &&) = default;
136
137 explicit constexpr Typedef(const TBaseType &value) : value(value) {}
138
139 constexpr Typedef &operator =(const Typedef &rhs) { this->value = rhs.value; return *this; }
140 constexpr Typedef &operator =(Typedef &&rhs) { this->value = std::move(rhs.value); return *this; }
141
142 /* Only allow conversion to BaseType via method. */
143 constexpr TBaseType base() const noexcept { return this->value; }
144
145 /* Only allow TProperties classes access to the internal value. Everyone else needs to call .base(). */
146 friend struct Compare;
147 friend struct Integer;
148 template <typename TCompatibleType> friend struct Compatible;
149
150/* GCC / MSVC don't pick up on the "friend struct" above, where CLang does.
151 * As in our CI we compile for all three targets, it is sufficient to have one
152 * that errors on this; but nobody should be using "value" directly. Instead,
153 * use base() to convert to the base type. */
154#ifdef __clang__
155 protected:
156#endif /* __clang__ */
157 TBaseType value{};
158 };
159}
160
174template <typename TBaseType, typename... TProperties>
175 struct std::hash<StrongType::Typedef<TBaseType, TProperties...>> {
182 std::size_t operator()(const StrongType::Typedef<TBaseType, TProperties...> &t) const noexcept
183 {
184 return std::hash<TBaseType>()(t.base());
185 }
186};
187
188#endif /* STRONG_TYPEDEF_TYPE_HPP */
StrongType::Compare::mixin
Definition strong_typedef_type.hpp:21
StrongType::Compare
Mix-in which makes the new Typedef comparable with itself and its base type.
Definition strong_typedef_type.hpp:19
StrongType::Compatible::mixin
Definition strong_typedef_type.hpp:106
StrongType::Compatible
Mix-in which makes the new Typedef compatible with another type (which is not the base type).
Definition strong_typedef_type.hpp:104
StrongType::Integer::mixin
Definition strong_typedef_type.hpp:39
StrongType::Integer
Mix-in which makes the new Typedef behave more like an integer.
Definition strong_typedef_type.hpp:37
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
std::hash< StrongType::Typedef< TBaseType, TProperties... > >::operator()
std::size_t operator()(const StrongType::Typedef< TBaseType, TProperties... > &t) const noexcept
Computes the hash value for a StrongType::Typedef instance.
Definition strong_typedef_type.hpp:182