diff options
author | hnOsmium0001 <[email protected]> | 2022-04-17 20:08:57 -0700 |
---|---|---|
committer | hnOsmium0001 <[email protected]> | 2022-04-17 20:08:57 -0700 |
commit | d43508ba4843801cbbf1f42a27af260d4eef5701 (patch) | |
tree | 39c51368cfe8ec097c08f198877cf07e9ff835ee /source/Enum.hpp | |
parent | 509201784d6525fc26345e55a56ab81e4a7616b3 (diff) |
Initial work on sprites and texture system
Diffstat (limited to 'source/Enum.hpp')
-rw-r--r-- | source/Enum.hpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/source/Enum.hpp b/source/Enum.hpp new file mode 100644 index 0000000..5e106fe --- /dev/null +++ b/source/Enum.hpp @@ -0,0 +1,103 @@ +#pragma once + +#include <initializer_list> +#include <type_traits> + +template <class TEnum> +class EnumFlags { +public: + using Enum = TEnum; + using Underlying = std::underlying_type_t<TEnum>; + +private: + Underlying mValue; + +public: + EnumFlags() + : mValue{ 0 } { + } + + EnumFlags(TEnum e) + : mValue{ 1 << static_cast<Underlying>(e) } { + } + + bool IsSet(EnumFlags mask) const { + return (mValue & mask.mValue) == mask.mValue; + } + + bool IsSet(std::initializer_list<TEnum> enums) { + EnumFlags flags; + for (auto& e : enums) { + flags.mValue |= static_cast<Underlying>(e); + } + return IsSet(flags); + } + + bool IsSetExclusive(EnumFlags mask) const { + return mValue == mask.mValue; + } + + bool IsSetExclusive(std::initializer_list<TEnum> enums) { + EnumFlags flags; + for (auto& e : enums) { + flags.mValue |= static_cast<Underlying>(e); + } + return IsSetExclusive(flags); + } + + void SetOn(EnumFlags mask) { + mValue |= mask.mValue; + } + + void SetOff(EnumFlags mask) { + mValue &= ~mask.mValue; + } + + void Set(EnumFlags mask, bool enabled) { + if (enabled) { + SetOn(mask); + } else { + SetOff(mask); + } + } + + EnumFlags& operator|=(EnumFlags that) const { + mValue |= that.mValue; + return *this; + } + + EnumFlags& operator&=(EnumFlags that) const { + mValue &= that.mValue; + return *this; + } + + EnumFlags& operator^=(EnumFlags that) const { + mValue ^= that.mValue; + return *this; + } + + EnumFlags& operator|=(TEnum e) const { + mValue |= 1 << static_cast<Underlying>(e); + return *this; + } + + EnumFlags& operator&=(TEnum e) const { + mValue &= 1 << static_cast<Underlying>(e); + return *this; + } + + EnumFlags& operator^=(TEnum e) const { + mValue ^= 1 << static_cast<Underlying>(e); + return *this; + } + + EnumFlags operator|(EnumFlags that) const { return EnumFlags(mValue | that.mValue); } + EnumFlags operator&(EnumFlags that) const { return EnumFlags(mValue & that.mValue); } + EnumFlags operator^(EnumFlags that) const { return EnumFlags(mValue ^ that.mValue); } + + EnumFlags operator|(TEnum e) const { return EnumFlags(mValue | 1 << static_cast<Underlying>(e)); } + EnumFlags operator&(TEnum e) const { return EnumFlags(mValue & 1 << static_cast<Underlying>(e)); } + EnumFlags operator^(TEnum e) const { return EnumFlags(mValue ^ 1 << static_cast<Underlying>(e)); } + + EnumFlags operator~() const { return EnumFlags(~mValue); } +}; |