ezEngine  Milestone 9
Enum.h
Go to the documentation of this file.
1 #pragma once
2 
4 
35 template <typename Derived>
36 struct ezEnum : public Derived
37 {
38 public:
39  typedef ezEnum<Derived> SelfType;
40  typedef typename Derived::StorageType StorageType;
41 
43  EZ_ALWAYS_INLINE ezEnum()
44  : m_value(Derived::Default)
45  {
46  } // [tested]
47 
49  EZ_ALWAYS_INLINE ezEnum(typename Derived::Enum init)
50  : m_value(init)
51  {
52  } // [tested]
53 
55  EZ_ALWAYS_INLINE void operator=(const SelfType& rh) // [tested]
56  {
57  m_value = rh.m_value;
58  }
59 
61  EZ_ALWAYS_INLINE void operator=(const typename Derived::Enum value) // [tested]
62  {
63  m_value = (StorageType)value;
64  }
65 
67  EZ_ALWAYS_INLINE bool operator==(const SelfType& rhs) const { return m_value == rhs.m_value; }
68  EZ_ALWAYS_INLINE bool operator!=(const SelfType& rhs) const { return m_value != rhs.m_value; }
69  EZ_ALWAYS_INLINE bool operator>(const SelfType& rhs) const { return m_value > rhs.m_value; }
70  EZ_ALWAYS_INLINE bool operator<(const SelfType& rhs) const { return m_value < rhs.m_value; }
71  EZ_ALWAYS_INLINE bool operator>=(const SelfType& rhs) const { return m_value >= rhs.m_value; }
72  EZ_ALWAYS_INLINE bool operator<=(const SelfType& rhs) const { return m_value <= rhs.m_value; }
73 
74  EZ_ALWAYS_INLINE bool operator==(typename Derived::Enum value) const { return m_value == value; }
75  EZ_ALWAYS_INLINE bool operator!=(typename Derived::Enum value) const { return m_value != value; }
76  EZ_ALWAYS_INLINE bool operator>(typename Derived::Enum value) const { return m_value > value; }
77  EZ_ALWAYS_INLINE bool operator<(typename Derived::Enum value) const { return m_value < value; }
78  EZ_ALWAYS_INLINE bool operator>=(typename Derived::Enum value) const { return m_value >= value; }
79  EZ_ALWAYS_INLINE bool operator<=(typename Derived::Enum value) const { return m_value <= value; }
80 
82  EZ_ALWAYS_INLINE SelfType operator|(const SelfType& rhs) const { return static_cast<typename Derived::Enum>(m_value | rhs.m_value); } // [tested]
83  EZ_ALWAYS_INLINE SelfType operator&(const SelfType& rhs) const { return static_cast<typename Derived::Enum>(m_value & rhs.m_value); } // [tested]
84 
86  EZ_ALWAYS_INLINE operator typename Derived::Enum() const // [tested]
87  {
88  return static_cast<typename Derived::Enum>(m_value);
89  }
90 
92  EZ_ALWAYS_INLINE StorageType GetValue() const // [tested]
93  {
94  return m_value;
95  }
96 
98  EZ_ALWAYS_INLINE void SetValue(StorageType value) // [tested]
99  {
100  m_value = value;
101  }
102 
103 private:
104  StorageType m_value;
105 };
106 
107 
108 #define EZ_ENUM_VALUE_TO_STRING(name) \
109  case name: \
110  return EZ_STRINGIZE(name);
111 
128 #define EZ_ENUM_TO_STRING(...) \
129  const char* ToString(ezUInt32 value) \
130  { \
131  switch (value) \
132  { \
133  EZ_EXPAND_ARGS(EZ_ENUM_VALUE_TO_STRING, ##__VA_ARGS__) \
134  default: \
135  return nullptr; \
136  } \
137  }
138 
EZ_ALWAYS_INLINE ezEnum()
Default constructor.
Definition: Enum.h:43
EZ_ALWAYS_INLINE StorageType GetValue() const
Returns the enum value as an integer.
Definition: Enum.h:92
EZ_ALWAYS_INLINE bool operator==(const SelfType &rhs) const
Comparison operators.
Definition: Enum.h:67
EZ_ALWAYS_INLINE ezEnum(typename Derived::Enum init)
Construct from a C++ enum, and implicit conversion from enum type.
Definition: Enum.h:49
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition: Enum.h:36
EZ_ALWAYS_INLINE void operator=(const typename Derived::Enum value)
Assignment operator.
Definition: Enum.h:61
EZ_ALWAYS_INLINE SelfType operator|(const SelfType &rhs) const
brief Bitwise operators
Definition: Enum.h:82
EZ_ALWAYS_INLINE void SetValue(StorageType value)
Sets the enum value through an integer.
Definition: Enum.h:98
EZ_ALWAYS_INLINE void operator=(const SelfType &rh)
Assignment operator.
Definition: Enum.h:55