5#include <Foundation/Basics.h>
6#include <Foundation/Containers/Implementation/BitIterator.h>
7#include <Foundation/Types/Enum.h>
84 using Enum =
typename T::Enum;
85 using Bits =
typename T::Bits;
86 using StorageType =
typename T::StorageType;
100 m_Value = (StorageType)flag1;
103 PL_ALWAYS_INLINE
void operator=(Enum flag1) { m_Value = (StorageType)flag1; }
106 PL_ALWAYS_INLINE
bool operator==(
const StorageType rhs)
const
108 return m_Value == rhs;
112 PL_ALWAYS_INLINE
bool operator!=(
const StorageType rhs)
const {
return m_Value != rhs; }
127 PL_ALWAYS_INLINE
bool IsSet(Enum flag)
const
129 return (m_Value & flag) != 0;
135 return (m_Value & rhs.m_Value) == rhs.m_Value;
141 return (m_Value & rhs.m_Value) == 0;
147 return (m_Value & rhs.m_Value) != 0;
153 m_Value |= rhs.m_Value;
159 m_Value &= (~rhs.m_Value);
165 m_Value ^= rhs.m_Value;
171 m_Value = (bState) ? m_Value | rhs.m_Value : m_Value & (~rhs.m_Value);
189 m_Value |= rhs.m_Value;
195 m_Value &= rhs.m_Value;
236 PL_ALWAYS_INLINE
explicit plBitflags(StorageType flags)
276#define PL_DECLARE_FLAGS_OPERATORS(FlagsType) \
277 inline plBitflags<FlagsType> operator|(FlagsType::Enum lhs, FlagsType::Enum rhs) \
279 return (plBitflags<FlagsType>(lhs) | plBitflags<FlagsType>(rhs)); \
282 inline plBitflags<FlagsType> operator&(FlagsType::Enum lhs, FlagsType::Enum rhs) \
284 return (plBitflags<FlagsType>(lhs) & plBitflags<FlagsType>(rhs)); \
305#define PL_DECLARE_FLAGS_WITH_DEFAULT(InternalStorageType, BitflagsTypeName, DefaultValue, ...) \
306 struct BitflagsTypeName \
308 static constexpr plUInt32 Count = PL_VA_NUM_ARGS(__VA_ARGS__); \
309 using StorageType = InternalStorageType; \
312 PL_EXPAND_ARGS_WITH_INDEX(PL_DECLARE_FLAGS_ENUM, ##__VA_ARGS__) Default = DefaultValue \
316 PL_EXPAND_ARGS(PL_DECLARE_FLAGS_BITS, ##__VA_ARGS__) \
318 PL_ENUM_TO_STRING(__VA_ARGS__) \
320 PL_DECLARE_FLAGS_OPERATORS(BitflagsTypeName)
322#define PL_DECLARE_FLAGS(InternalStorageType, BitflagsTypeName, ...) \
323 PL_DECLARE_FLAGS_WITH_DEFAULT(InternalStorageType, BitflagsTypeName, 0, ##__VA_ARGS__)
327#define PL_DECLARE_FLAGS_ENUM(name, n) name = PL_BIT(n),
330#define PL_DECLARE_FLAGS_BITS(name) StorageType name : 1;
Definition BitIterator.h:27
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
PL_ALWAYS_INLINE bool operator!=(const StorageType rhs) const
Comparison operator.
Definition Bitflags.h:112
PL_ALWAYS_INLINE void Clear()
Clears all flags.
Definition Bitflags.h:121
PL_ALWAYS_INLINE void Toggle(const plBitflags< T > &rhs)
Toggles the state of the given flag.
Definition Bitflags.h:163
PL_ALWAYS_INLINE plBitflags< T > operator|(const plBitflags< T > &rhs) const
Returns an object that has the flags of this and rhs combined.
Definition Bitflags.h:175
PL_ALWAYS_INLINE bool AreAllSet(const plBitflags< T > &rhs) const
Returns whether all the given flags are set.
Definition Bitflags.h:133
PL_ALWAYS_INLINE bool operator==(const StorageType rhs) const
Comparison operator.
Definition Bitflags.h:106
PL_ALWAYS_INLINE bool operator==(const plBitflags< T > &rhs) const
Comparison operator.
Definition Bitflags.h:115
PL_ALWAYS_INLINE bool IsNoFlagSet() const
Returns true if not a single bit is set.
Definition Bitflags.h:211
PL_ALWAYS_INLINE void SetValue(StorageType value)
Overwrites the flags with a new value.
Definition Bitflags.h:205
PL_ALWAYS_INLINE void Remove(const plBitflags< T > &rhs)
Removes the given flag.
Definition Bitflags.h:157
PL_ALWAYS_INLINE void operator|=(const plBitflags< T > &rhs)
Modifies this to also contain the bits from rhs.
Definition Bitflags.h:187
PL_ALWAYS_INLINE StorageType GetValue() const
Returns the stored value as the underlying integer type.
Definition Bitflags.h:199
PL_ALWAYS_INLINE plBitflags()
Constructor. Initializes the flags to the default value.
Definition Bitflags.h:92
PL_ALWAYS_INLINE bool operator!=(const plBitflags< T > &rhs) const
Comparison operator.
Definition Bitflags.h:118
PL_ALWAYS_INLINE void operator&=(const plBitflags< T > &rhs)
Modifies this to only contain the bits that were set in this and rhs.
Definition Bitflags.h:193
PL_ALWAYS_INLINE ConstIterator GetIterator() const
Returns a constant iterator to the very first set bit. Note that due to the way iterating through bit...
Definition Bitflags.h:224
PL_ALWAYS_INLINE void AddOrRemove(const plBitflags< T > &rhs, bool bState)
Sets or clears the given flag.
Definition Bitflags.h:169
PL_ALWAYS_INLINE bool IsAnyFlagSet() const
Returns true if any bitflag is set.
Definition Bitflags.h:217
PL_ALWAYS_INLINE plBitflags< T > operator&(const plBitflags< T > &rhs) const
Returns an object that has the flags that were set both in this and rhs.
Definition Bitflags.h:181
PL_ALWAYS_INLINE bool IsAnySet(const plBitflags< T > &rhs) const
Returns whether any of the given flags is set.
Definition Bitflags.h:145
PL_ALWAYS_INLINE bool IsSet(Enum flag) const
Checks if certain flags are set within the bitfield.
Definition Bitflags.h:127
PL_ALWAYS_INLINE bool AreNoneSet(const plBitflags< T > &rhs) const
Returns whether none of the given flags is set.
Definition Bitflags.h:139
PL_ALWAYS_INLINE plBitflags(Enum flag1)
Converts the incoming type to plBitflags<T>
Definition Bitflags.h:98
PL_ALWAYS_INLINE ConstIterator GetEndIterator() const
Returns an invalid iterator. Needed to support range based for loops.
Definition Bitflags.h:230
PL_ALWAYS_INLINE void Add(const plBitflags< T > &rhs)
Sets the given flag.
Definition Bitflags.h:151