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 
16 #define DECLARE_POSTFIX_INCREMENT(enum_type) \
17  inline enum_type operator ++(enum_type& e, int) \
18  { \
19  enum_type e_org = e; \
20  e = (enum_type)((std::underlying_type<enum_type>::type)e + 1); \
21  return e_org; \
22  } \
23  inline enum_type operator --(enum_type& e, int) \
24  { \
25  enum_type e_org = e; \
26  e = (enum_type)((std::underlying_type<enum_type>::type)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)((std::underlying_type<mask_t>::type)m1 | m2);} \
35  inline mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((std::underlying_type<mask_t>::type)m1 & m2);} \
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) {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)(~(std::underlying_type<mask_t>::type)m);}
41 
42 
50 template <typename Tenum_t> struct EnumPropsT;
51 
63 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)>
65  typedef Tenum_t type;
66  typedef Tstorage_t storage;
67  static const Tenum_t begin = Tbegin;
68  static const Tenum_t end = Tend;
69  static const Tenum_t invalid = Tinvalid;
70  static const uint num_bits = Tnum_bits;
71 };
72 
73 #endif /* ENUM_TYPE_HPP */
Helper template class that makes basic properties of given enumeration type visible from outsize...
Definition: enum_type.hpp:64
Tstorage_t storage
storage type (i.e. byte)
Definition: enum_type.hpp:66
static const Tenum_t invalid
what value is used as invalid value (i.e. INVALID_TRACKDIR)
Definition: enum_type.hpp:69
static const uint num_bits
Number of bits for storing the enum in command parameters.
Definition: enum_type.hpp:70
static const Tenum_t end
one after the last valid value (i.e. TRACKDIR_END)
Definition: enum_type.hpp:68
Tenum_t type
enum type (i.e. Trackdir)
Definition: enum_type.hpp:65
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:50
static const Tenum_t begin
lowest valid value (i.e. TRACKDIR_BEGIN)
Definition: enum_type.hpp:67