OpenTTD Source  20240519-master-g46d7586ab1
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 
14 #define DECLARE_POSTFIX_INCREMENT(enum_type) \
15  inline constexpr enum_type operator ++(enum_type& e, int) \
16  { \
17  enum_type e_org = e; \
18  e = (enum_type)((std::underlying_type<enum_type>::type)e + 1); \
19  return e_org; \
20  } \
21  inline constexpr enum_type operator --(enum_type& e, int) \
22  { \
23  enum_type e_org = e; \
24  e = (enum_type)((std::underlying_type<enum_type>::type)e - 1); \
25  return e_org; \
26  }
27 
28 
29 
31 #define DECLARE_ENUM_AS_BIT_SET(enum_type) \
32  inline constexpr enum_type operator | (enum_type m1, enum_type m2) {return (enum_type)((std::underlying_type<enum_type>::type)m1 | (std::underlying_type<enum_type>::type)m2);} \
33  inline constexpr enum_type operator & (enum_type m1, enum_type m2) {return (enum_type)((std::underlying_type<enum_type>::type)m1 & (std::underlying_type<enum_type>::type)m2);} \
34  inline constexpr enum_type operator ^ (enum_type m1, enum_type m2) {return (enum_type)((std::underlying_type<enum_type>::type)m1 ^ (std::underlying_type<enum_type>::type)m2);} \
35  inline constexpr enum_type& operator |= (enum_type& m1, enum_type m2) {m1 = m1 | m2; return m1;} \
36  inline constexpr enum_type& operator &= (enum_type& m1, enum_type m2) {m1 = m1 & m2; return m1;} \
37  inline constexpr enum_type& operator ^= (enum_type& m1, enum_type m2) {m1 = m1 ^ m2; return m1;} \
38  inline constexpr enum_type operator ~(enum_type m) {return (enum_type)(~(std::underlying_type<enum_type>::type)m);}
39 
41 #define DECLARE_ENUM_AS_ADDABLE(EnumType) \
42  template <typename OtherEnumType, typename = typename std::enable_if<std::is_enum_v<OtherEnumType>, OtherEnumType>::type> \
43  constexpr OtherEnumType operator + (OtherEnumType m1, EnumType m2) { \
44  return static_cast<OtherEnumType>(static_cast<typename std::underlying_type<OtherEnumType>::type>(m1) + static_cast<typename std::underlying_type<EnumType>::type>(m2)); \
45  }
46 
47 #endif /* ENUM_TYPE_HPP */