OpenTTD
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)>
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;
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>
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 */
Helper template class that makes basic properties of given enumeration type visible from outsize...
Definition: enum_type.hpp:66
Tstorage_t storage
storage type (i.e. byte)
Definition: enum_type.hpp:68
static const Tenum_t invalid
what value is used as invalid value (i.e. INVALID_TRACKDIR)
Definition: enum_type.hpp:71
static const uint num_bits
Number of bits for storing the enum in command parameters.
Definition: enum_type.hpp:72
static const Tenum_t end
one after the last valid value (i.e. TRACKDIR_END)
Definition: enum_type.hpp:70
storage_type m_val
here we hold the actual value in small (i.e. byte) form
Definition: enum_type.hpp:140
Tenum_t type
enum type (i.e. Trackdir)
Definition: enum_type.hpp:67
EnumPropsT< Tenum_t > Props
make easier access to our enumeration properties
Definition: enum_type.hpp:92
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:52
Props::storage storage_type
small storage type
Definition: enum_type.hpp:93
In some cases we use byte or uint16 to store values that are defined as enum.
Definition: enum_type.hpp:86
Template of struct holding enum types (on most archs, enums are stored in an int32).
Definition: enum_type.hpp:139
static const Tenum_t begin
lowest valid value (i.e. TRACKDIR_BEGIN)
Definition: enum_type.hpp:69
storage_type m_val
here we hold the actual value in small (i.e. byte) form
Definition: enum_type.hpp:98
Tenum_t enum_type
expose our enumeration type (i.e. Trackdir) to outside
Definition: enum_type.hpp:91