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

OpenTTD Source 13.2.1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef STRONG_TYPEDEF_TYPE_HPP
11 #define STRONG_TYPEDEF_TYPE_HPP
12
14 struct StrongTypedefBase {};
15
26 template <class T, class Tthis>
27 struct StrongTypedef : StrongTypedefBase {
28 using Type = T;
29
30 T value{};
31
32 constexpr StrongTypedef() = default;
33 constexpr StrongTypedef(const StrongTypedef &o) = default;
34 constexpr StrongTypedef(StrongTypedef &&o) = default;
35
36 constexpr StrongTypedef(const T &value) : value(value) {}
37
38 constexpr Tthis &operator =(const StrongTypedef &rhs) { this->value = rhs.value; return static_cast<Tthis &>(*this); }
39 constexpr Tthis &operator =(StrongTypedef &&rhs) { this->value = std::move(rhs.value); return static_cast<Tthis &>(*this); }
40 constexpr Tthis &operator =(const T &rhs) { this->value = rhs; return static_cast<Tthis &>(*this); }
41
42 explicit constexpr operator T() const { return this->value; }
43
44 constexpr bool operator ==(const StrongTypedef &rhs) const { return this->value == rhs.value; }
45 constexpr bool operator !=(const StrongTypedef &rhs) const { return this->value != rhs.value; }
46 constexpr bool operator ==(const T &rhs) const { return this->value == rhs; }
47 constexpr bool operator !=(const T &rhs) const { return this->value != rhs; }
48 };
49
55 template <class T, class Tthis>
56 struct StrongIntegralTypedef : StrongTypedef<T, Tthis> {
57 using StrongTypedef<T, Tthis>::StrongTypedef;
58
59 constexpr Tthis &operator ++() { this->value++; return static_cast<Tthis &>(*this); }
60 constexpr Tthis &operator --() { this->value--; return static_cast<Tthis &>(*this); }
61 constexpr Tthis operator ++(int) { auto res = static_cast<Tthis &>(*this); this->value++; return res; }
62 constexpr Tthis operator --(int) { auto res = static_cast<Tthis &>(*this); this->value--; return res; }
63
64 constexpr Tthis &operator +=(const Tthis &rhs) { this->value += rhs.value; return *static_cast<Tthis *>(this); }
65 constexpr Tthis &operator -=(const Tthis &rhs) { this->value -= rhs.value; return *static_cast<Tthis *>(this); }
66
67 constexpr Tthis operator +(const Tthis &rhs) const { return Tthis{ this->value + rhs.value }; }
68 constexpr Tthis operator -(const Tthis &rhs) const { return Tthis{ this->value - rhs.value }; }
69 constexpr Tthis operator +(const T &rhs) const { return Tthis{ this->value + rhs }; }
70 constexpr Tthis operator -(const T &rhs) const { return Tthis{ this->value - rhs }; }
71 };
72
73 #endif /* STRONG_TYPEDEF_TYPE_HPP */
StrongTypedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:27
StrongTypedef::value
T value
Backing storage field.
Definition: strong_typedef_type.hpp:30
StrongTypedefBase
Non-templated base for StrongTypedef for use with type trait queries.
Definition: strong_typedef_type.hpp:14
StrongIntegralTypedef
Extension of StrongTypedef with operators for addition and subtraction.
Definition: strong_typedef_type.hpp:56