ezEngine  Milestone 9
Bitflags.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <Foundation/Basics.h>
7 
79 template <typename T>
80 struct ezBitflags
81 {
82 private:
83  typedef typename T::Enum Enum;
84  typedef typename T::Bits Bits;
85  typedef typename T::StorageType StorageType;
86 
87 public:
89  EZ_ALWAYS_INLINE ezBitflags()
90  : m_Value(T::Default) // [tested]
91  {
92  }
93 
95  EZ_ALWAYS_INLINE ezBitflags(Enum flag1) // [tested]
96  {
97  m_Value = (StorageType)flag1;
98  }
99 
101  EZ_ALWAYS_INLINE bool operator==(const StorageType rhs) const // [tested]
102  {
103  return m_Value == rhs;
104  }
105 
107  EZ_ALWAYS_INLINE bool operator!=(const StorageType rhs) const { return m_Value != rhs; }
108 
110  EZ_ALWAYS_INLINE bool operator==(const ezBitflags<T>& rhs) const { return m_Value == rhs.m_Value; }
111 
113  EZ_ALWAYS_INLINE bool operator!=(const ezBitflags<T>& rhs) const { return m_Value != rhs.m_Value; }
114 
116  EZ_ALWAYS_INLINE void Clear() // [tested]
117  {
118  m_Value = 0;
119  }
120 
122  EZ_ALWAYS_INLINE bool IsSet(Enum flag) const // [tested]
123  {
124  return (m_Value & flag) != 0;
125  }
126 
128  EZ_ALWAYS_INLINE bool AreAllSet(const ezBitflags<T>& rhs) const // [tested]
129  {
130  return (m_Value & rhs.m_Value) == rhs.m_Value;
131  }
132 
134  EZ_ALWAYS_INLINE bool IsAnySet(const ezBitflags<T>& rhs) const // [tested]
135  {
136  return (m_Value & rhs.m_Value) != 0;
137  }
138 
140  EZ_ALWAYS_INLINE void Add(const ezBitflags<T>& rhs) // [tested]
141  {
142  m_Value |= rhs.m_Value;
143  }
144 
146  EZ_ALWAYS_INLINE void Remove(const ezBitflags<T>& rhs) // [tested]
147  {
148  m_Value &= (~rhs.m_Value);
149  }
150 
152  EZ_ALWAYS_INLINE void Toggle(const ezBitflags<T>& rhs) // [tested]
153  {
154  m_Value ^= rhs.m_Value;
155  }
156 
158  EZ_ALWAYS_INLINE void AddOrRemove(const ezBitflags<T>& rhs, bool state) // [tested]
159  {
160  m_Value = (state) ? m_Value | rhs.m_Value : m_Value & (~rhs.m_Value);
161  }
162 
164  EZ_ALWAYS_INLINE ezBitflags<T> operator|(const ezBitflags<T>& rhs) const // [tested]
165  {
166  return ezBitflags<T>(m_Value | rhs.m_Value);
167  }
168 
170  EZ_ALWAYS_INLINE ezBitflags<T> operator&(const ezBitflags<T>& rhs) const // [tested]
171  {
172  return ezBitflags<T>(m_Value & rhs.m_Value);
173  }
174 
176  EZ_ALWAYS_INLINE void operator|=(const ezBitflags<T>& rhs) // [tested]
177  {
178  m_Value |= rhs.m_Value;
179  }
180 
182  EZ_ALWAYS_INLINE void operator&=(const ezBitflags<T>& rhs) // [tested]
183  {
184  m_Value &= rhs.m_Value;
185  }
186 
188  EZ_ALWAYS_INLINE StorageType GetValue() const // [tested]
189  {
190  return m_Value;
191  }
192 
194  EZ_ALWAYS_INLINE void SetValue(StorageType value) // [tested]
195  {
196  m_Value = value;
197  }
198 
199 private:
200  EZ_ALWAYS_INLINE explicit ezBitflags(StorageType flags)
201  : m_Value(flags)
202  {
203  }
204 
205  union {
206  StorageType m_Value;
207  Bits m_bits;
208  };
209 };
210 
211 
214 #define EZ_DECLARE_FLAGS_OPERATORS(FlagsType) \
215  inline ezBitflags<FlagsType> operator|(FlagsType::Enum lhs, FlagsType::Enum rhs) \
216  { \
217  return (ezBitflags<FlagsType>(lhs) | ezBitflags<FlagsType>(rhs)); \
218  } \
219  \
220  inline ezBitflags<FlagsType> operator&(FlagsType::Enum lhs, FlagsType::Enum rhs) \
221  { \
222  return (ezBitflags<FlagsType>(lhs) & ezBitflags<FlagsType>(rhs)); \
223  }
224 
225 
226 
243 #define EZ_DECLARE_FLAGS_WITH_DEFAULT(InternalStorageType, BitflagsTypeName, DefaultValue, ...) \
244  struct BitflagsTypeName \
245  { \
246  static const ezUInt32 Count = EZ_VA_NUM_ARGS(__VA_ARGS__); \
247  typedef InternalStorageType StorageType; \
248  enum Enum \
249  { \
250  EZ_EXPAND_ARGS_WITH_INDEX(EZ_DECLARE_FLAGS_ENUM, ##__VA_ARGS__) \
251  Default = DefaultValue \
252  }; \
253  struct Bits \
254  { \
255  EZ_EXPAND_ARGS(EZ_DECLARE_FLAGS_BITS, ##__VA_ARGS__) \
256  }; \
257  EZ_ENUM_TO_STRING(__VA_ARGS__) \
258  }; \
259  EZ_DECLARE_FLAGS_OPERATORS(BitflagsTypeName)
260 
261 #define EZ_DECLARE_FLAGS(InternalStorageType, BitflagsTypeName, ...) \
262  EZ_DECLARE_FLAGS_WITH_DEFAULT(InternalStorageType, BitflagsTypeName, 0, __VA_ARGS__)
263 
266 #define EZ_DECLARE_FLAGS_ENUM(name, n) name = EZ_BIT(n),
267 
269 #define EZ_DECLARE_FLAGS_BITS(name) StorageType name : 1;
270 
272 
EZ_ALWAYS_INLINE bool AreAllSet(const ezBitflags< T > &rhs) const
Returns whether all the given flags are set.
Definition: Bitflags.h:128
EZ_ALWAYS_INLINE void SetValue(StorageType value)
Overwrites the flags with a new value.
Definition: Bitflags.h:194
EZ_ALWAYS_INLINE bool operator==(const ezBitflags< T > &rhs) const
Comparison operator.
Definition: Bitflags.h:110
EZ_ALWAYS_INLINE bool operator!=(const StorageType rhs) const
Comparison operator.
Definition: Bitflags.h:107
EZ_ALWAYS_INLINE void Add(const ezBitflags< T > &rhs)
Sets the given flag.
Definition: Bitflags.h:140
EZ_ALWAYS_INLINE bool IsAnySet(const ezBitflags< T > &rhs) const
Returns whether any of the given flags is set.
Definition: Bitflags.h:134
EZ_ALWAYS_INLINE void AddOrRemove(const ezBitflags< T > &rhs, bool state)
Sets or clears the given flag.
Definition: Bitflags.h:158
The ezBitflags class allows you to work with type-safe bitflags.
Definition: Bitflags.h:80
EZ_ALWAYS_INLINE ezBitflags(Enum flag1)
Converts the incoming type to ezBitflags<T>
Definition: Bitflags.h:95
EZ_ALWAYS_INLINE ezBitflags()
Constructor. Initializes the flags to the default value.
Definition: Bitflags.h:89
EZ_ALWAYS_INLINE bool operator==(const StorageType rhs) const
Comparison operator.
Definition: Bitflags.h:101
EZ_ALWAYS_INLINE void operator &=(const ezBitflags< T > &rhs)
Modifies this to only contain the bits that were set in this and rhs.
Definition: Bitflags.h:182
EZ_ALWAYS_INLINE void Toggle(const ezBitflags< T > &rhs)
Toggles the state of the given flag.
Definition: Bitflags.h:152
EZ_ALWAYS_INLINE bool IsSet(Enum flag) const
Checks if certain flags are set within the bitfield.
Definition: Bitflags.h:122
EZ_ALWAYS_INLINE ezBitflags< T > operator|(const ezBitflags< T > &rhs) const
Returns an object that has the flags of this and rhs combined.
Definition: Bitflags.h:164
EZ_ALWAYS_INLINE bool operator!=(const ezBitflags< T > &rhs) const
Comparison operator.
Definition: Bitflags.h:113
EZ_ALWAYS_INLINE void Remove(const ezBitflags< T > &rhs)
Removes the given flag.
Definition: Bitflags.h:146
EZ_ALWAYS_INLINE ezBitflags< T > operator &(const ezBitflags< T > &rhs) const
Returns an object that has the flags that were set both in this and rhs.
Definition: Bitflags.h:170
EZ_ALWAYS_INLINE StorageType GetValue() const
Returns the stored value as the underlying integer type.
Definition: Bitflags.h:188
EZ_ALWAYS_INLINE void operator|=(const ezBitflags< T > &rhs)
Modifies this to also contain the bits from rhs.
Definition: Bitflags.h:176
EZ_ALWAYS_INLINE void Clear()
Clears all flags.
Definition: Bitflags.h:116