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

OpenTTD Source 12.0-beta1

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 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 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(mask_t) \
32 inline mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 | m2);} \
33 inline mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 & m2);} \
34 inline mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 ^ m2);} \
35 inline mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \
36 inline mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \
37 inline mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \
38 inline mask_t operator ~(mask_t m) {return (mask_t)(~(std::underlying_type<mask_t>::type)m);}
39
40
48 template <typename Tenum_t> struct EnumPropsT;
49
61 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)>
62 struct MakeEnumPropsT {
63 typedef Tenum_t type;
64 typedef Tstorage_t storage;
65 static const Tenum_t begin = Tbegin;
66 static const Tenum_t end = Tend;
67 static const Tenum_t invalid = Tinvalid;
68 static const uint num_bits = Tnum_bits;
69 };
70
71 #endif /* ENUM_TYPE_HPP */
MakeEnumPropsT::num_bits
static const uint num_bits
Number of bits for storing the enum in command parameters.
Definition: enum_type.hpp:68
MakeEnumPropsT::type
Tenum_t type
enum type (i.e. Trackdir)
Definition: enum_type.hpp:63
EnumPropsT
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:48
MakeEnumPropsT::invalid
static const Tenum_t invalid
what value is used as invalid value (i.e. INVALID_TRACKDIR)
Definition: enum_type.hpp:67
MakeEnumPropsT::end
static const Tenum_t end
one after the last valid value (i.e. TRACKDIR_END)
Definition: enum_type.hpp:66
MakeEnumPropsT::storage
Tstorage_t storage
storage type (i.e. byte)
Definition: enum_type.hpp:64
MakeEnumPropsT
Helper template class that makes basic properties of given enumeration type visible from outsize.
Definition: enum_type.hpp:62
MakeEnumPropsT::begin
static const Tenum_t begin
lowest valid value (i.e. TRACKDIR_BEGIN)
Definition: enum_type.hpp:65