OpenTTD: src/core/enum_type.hpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

enum_type.hpp
Go to the documentation of this file.
1 /* $Id: enum_type.hpp 24900 2013-01-08 22:46:42Z planetmaker $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef ENUM_TYPE_HPP
13 #define ENUM_TYPE_HPP
14
16 #define DECLARE_POSTFIX_INCREMENT(type) \
17 inline type operator ++(type& e, int) \
18 { \
19 type e_org = e; \
20 e = (type)((int)e + 1); \
21 return e_org; \
22 } \
23 inline type operator --(type& e, int) \
24 { \
25 type e_org = e; \
26 e = (type)((int)e - 1); \
27 return e_org; \
28 }
29
30
31
33 # define DECLARE_ENUM_AS_BIT_SET(mask_t) \
34 inline mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \
35 inline mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \
36 inline mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \
37 inline mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \
38 inline mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \
39 inline mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \
40 inline mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);}
41
42
52 template <typename Tenum_t> struct EnumPropsT;
53
65 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)>
66 struct MakeEnumPropsT {
67 typedef Tenum_t type;
68 typedef Tstorage_t storage;
69 static const Tenum_t begin = Tbegin;
70 static const Tenum_t end = Tend;
71 static const Tenum_t invalid = Tinvalid;
72 static const uint num_bits = Tnum_bits;
73 };
74
75
76
86 template <typename Tenum_t> struct TinyEnumT;
87
89 template <typename Tenum_t>
90 struct TinyEnumT {
91 typedef Tenum_t enum_type;
92 typedef EnumPropsT<Tenum_t> Props;
93 typedef typename Props::storage storage_type;
94 static const enum_type begin = Props::begin;
95 static const enum_type end = Props::end;
96 static const enum_type invalid = Props::invalid;
97
98 storage_type m_val;
99
101 inline operator enum_type () const
102 {
103 return (enum_type)m_val;
104 }
105
107 inline TinyEnumT& operator = (enum_type e)
108 {
109 m_val = (storage_type)e;
110 return *this;
111 }
112
114 inline TinyEnumT& operator = (uint u)
115 {
116 m_val = (storage_type)u;
117 return *this;
118 }
119
121 inline TinyEnumT operator ++ (int)
122 {
123 TinyEnumT org = *this;
124 if (++m_val >= end) m_val -= (storage_type)(end - begin);
125 return org;
126 }
127
129 inline TinyEnumT& operator ++ ()
130 {
131 if (++m_val >= end) m_val -= (storage_type)(end - begin);
132 return *this;
133 }
134 };
135
136
138 template <typename enum_type, typename storage_type>
139 struct SimpleTinyEnumT {
140 storage_type m_val;
141
143 inline operator enum_type () const
144 {
145 return (enum_type)this->m_val;
146 }
147
149 inline SimpleTinyEnumT &operator = (enum_type e)
150 {
151 this->m_val = (storage_type)e;
152 return *this;
153 }
154
156 inline SimpleTinyEnumT &operator = (uint u)
157 {
158 this->m_val = (storage_type)u;
159 return *this;
160 }
161
163 inline SimpleTinyEnumT &operator |= (enum_type e)
164 {
165 this->m_val = (storage_type)((enum_type)this->m_val | e);
166 return *this;
167 }
168
170 inline SimpleTinyEnumT &operator &= (enum_type e)
171 {
172 this->m_val = (storage_type)((enum_type)this->m_val & e);
173 return *this;
174 }
175 };
176
177 #endif /* ENUM_TYPE_HPP */