OpenTTD Source 20241224-master-gf74b0cf984
enum_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 ENUM_TYPE_HPP
11#define ENUM_TYPE_HPP
12
14template <typename enum_type>
15constexpr std::underlying_type_t<enum_type> to_underlying(enum_type e) { return static_cast<std::underlying_type_t<enum_type>>(e); }
16
18#define DECLARE_POSTFIX_INCREMENT(enum_type) \
19 inline constexpr enum_type operator ++(enum_type& e, int) \
20 { \
21 enum_type e_org = e; \
22 e = static_cast<enum_type>(to_underlying(e) + 1); \
23 return e_org; \
24 } \
25 inline constexpr enum_type operator --(enum_type& e, int) \
26 { \
27 enum_type e_org = e; \
28 e = static_cast<enum_type>(to_underlying(e) - 1); \
29 return e_org; \
30 }
31
32
33
35#define DECLARE_ENUM_AS_BIT_SET(enum_type) \
36 inline constexpr enum_type operator | (enum_type m1, enum_type m2) { return static_cast<enum_type>(to_underlying(m1) | to_underlying(m2)); } \
37 inline constexpr enum_type operator & (enum_type m1, enum_type m2) { return static_cast<enum_type>(to_underlying(m1) & to_underlying(m2)); } \
38 inline constexpr enum_type operator ^ (enum_type m1, enum_type m2) { return static_cast<enum_type>(to_underlying(m1) ^ to_underlying(m2)); } \
39 inline constexpr enum_type& operator |= (enum_type& m1, enum_type m2) { m1 = m1 | m2; return m1; } \
40 inline constexpr enum_type& operator &= (enum_type& m1, enum_type m2) { m1 = m1 & m2; return m1; } \
41 inline constexpr enum_type& operator ^= (enum_type& m1, enum_type m2) { m1 = m1 ^ m2; return m1; } \
42 inline constexpr enum_type operator ~(enum_type m) { return static_cast<enum_type>(~to_underlying(m)); }
43
45#define DECLARE_ENUM_AS_ADDABLE(EnumType) \
46 template <typename OtherEnumType, typename = typename std::enable_if<std::is_enum_v<OtherEnumType>, OtherEnumType>::type> \
47 constexpr OtherEnumType operator + (OtherEnumType m1, EnumType m2) { \
48 return static_cast<OtherEnumType>(to_underlying(m1) + to_underlying(m2)); \
49 }
50
57template <typename T, class = typename std::enable_if_t<std::is_enum_v<T>>>
58debug_inline constexpr bool HasFlag(const T x, const T y)
59{
60 return (x & y) == y;
61}
62
63#endif /* ENUM_TYPE_HPP */
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:15
debug_inline constexpr bool HasFlag(const T x, const T y)
Checks if a value in a bitset enum is set.
Definition enum_type.hpp:58