OpenTTD
enum_type.hpp
Go to the documentation of this file.
1 /* $Id$ */
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 
15 #include <type_traits>
16 
18 #define DECLARE_POSTFIX_INCREMENT(enum_type) \
19  inline enum_type operator ++(enum_type& e, int) \
20  { \
21  enum_type e_org = e; \
22  e = (enum_type)((std::underlying_type<enum_type>::type)e + 1); \
23  return e_org; \
24  } \
25  inline enum_type operator --(enum_type& e, int) \
26  { \
27  enum_type e_org = e; \
28  e = (enum_type)((std::underlying_type<enum_type>::type)e - 1); \
29  return e_org; \
30  }
31 
32 
33 
35 # define DECLARE_ENUM_AS_BIT_SET(mask_t) \
36  inline mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 | m2);} \
37  inline mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 & m2);} \
38  inline mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 ^ m2);} \
39  inline mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \
40  inline mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \
41  inline mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \
42  inline mask_t operator ~(mask_t m) {return (mask_t)(~(std::underlying_type<mask_t>::type)m);}
43 
44 
54 template <typename Tenum_t> struct EnumPropsT;
55 
67 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)>
69  typedef Tenum_t type;
70  typedef Tstorage_t storage;
71  static const Tenum_t begin = Tbegin;
72  static const Tenum_t end = Tend;
73  static const Tenum_t invalid = Tinvalid;
74  static const uint num_bits = Tnum_bits;
75 };
76 
77 
78 
88 template <typename Tenum_t> struct TinyEnumT;
89 
91 template <typename Tenum_t>
92 struct TinyEnumT {
93  typedef Tenum_t enum_type;
95  typedef typename Props::storage storage_type;
96  static const enum_type begin = Props::begin;
97  static const enum_type end = Props::end;
98  static const enum_type invalid = Props::invalid;
99 
100  storage_type m_val;
101 
103  inline operator enum_type () const
104  {
105  return (enum_type)m_val;
106  }
107 
109  inline TinyEnumT& operator = (enum_type e)
110  {
111  m_val = (storage_type)e;
112  return *this;
113  }
114 
116  inline TinyEnumT& operator = (uint u)
117  {
118  m_val = (storage_type)u;
119  return *this;
120  }
121 
123  inline TinyEnumT operator ++ (int)
124  {
125  TinyEnumT org = *this;
126  if (++m_val >= end) m_val -= (storage_type)(end - begin);
127  return org;
128  }
129 
131  inline TinyEnumT& operator ++ ()
132  {
133  if (++m_val >= end) m_val -= (storage_type)(end - begin);
134  return *this;
135  }
136 };
137 
138 
140 template <typename enum_type, typename storage_type>
142  storage_type m_val;
143 
145  inline operator enum_type () const
146  {
147  return (enum_type)this->m_val;
148  }
149 
151  inline SimpleTinyEnumT &operator = (enum_type e)
152  {
153  this->m_val = (storage_type)e;
154  return *this;
155  }
156 
158  inline SimpleTinyEnumT &operator = (uint u)
159  {
160  this->m_val = (storage_type)u;
161  return *this;
162  }
163 
165  inline SimpleTinyEnumT &operator |= (enum_type e)
166  {
167  this->m_val = (storage_type)((enum_type)this->m_val | e);
168  return *this;
169  }
170 
172  inline SimpleTinyEnumT &operator &= (enum_type e)
173  {
174  this->m_val = (storage_type)((enum_type)this->m_val & e);
175  return *this;
176  }
177 };
178 
179 #endif /* ENUM_TYPE_HPP */
Helper template class that makes basic properties of given enumeration type visible from outsize...
Definition: enum_type.hpp:68
Tstorage_t storage
storage type (i.e. byte)
Definition: enum_type.hpp:70
static const Tenum_t invalid
what value is used as invalid value (i.e. INVALID_TRACKDIR)
Definition: enum_type.hpp:73
static const uint num_bits
Number of bits for storing the enum in command parameters.
Definition: enum_type.hpp:74
static const Tenum_t end
one after the last valid value (i.e. TRACKDIR_END)
Definition: enum_type.hpp:72
storage_type m_val
here we hold the actual value in small (i.e. byte) form
Definition: enum_type.hpp:142
Tenum_t type
enum type (i.e. Trackdir)
Definition: enum_type.hpp:69
EnumPropsT< Tenum_t > Props
make easier access to our enumeration properties
Definition: enum_type.hpp:94
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:54
Props::storage storage_type
small storage type
Definition: enum_type.hpp:95
In some cases we use byte or uint16 to store values that are defined as enum.
Definition: enum_type.hpp:88
Template of struct holding enum types (on most archs, enums are stored in an int32).
Definition: enum_type.hpp:141
static const Tenum_t begin
lowest valid value (i.e. TRACKDIR_BEGIN)
Definition: enum_type.hpp:71
storage_type m_val
here we hold the actual value in small (i.e. byte) form
Definition: enum_type.hpp:100
Tenum_t enum_type
expose our enumeration type (i.e. Trackdir) to outside
Definition: enum_type.hpp:93