From f138311d2d2e0cc9ba0496d523bb46f2c1c9fb73 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Wed, 20 Sep 2023 23:58:58 -0700 Subject: Copy from the PlasticSCM repo, replace vendored glm wtih conan --- source/10-common/Enum.hpp | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 source/10-common/Enum.hpp (limited to 'source/10-common/Enum.hpp') diff --git a/source/10-common/Enum.hpp b/source/10-common/Enum.hpp new file mode 100644 index 0000000..7afbe8e --- /dev/null +++ b/source/10-common/Enum.hpp @@ -0,0 +1,110 @@ +#pragma once + +#include +#include + +template +class EnumFlags { +public: + using Enum = TEnum; + using Underlying = std::underlying_type_t; + +private: + Underlying mValue; + +public: + EnumFlags() + : mValue{ 0 } { + } + + EnumFlags(TEnum e) + : mValue{ static_cast(1) << static_cast(e) } { + } + + bool IsSet(EnumFlags mask) const { + return (mValue & mask.mValue) == mask.mValue; + } + + bool IsSet(std::initializer_list enums) { + EnumFlags flags; + for (auto& e : enums) { + flags.mValue |= static_cast(e); + } + return IsSet(flags); + } + + bool IsSetExclusive(EnumFlags mask) const { + return mValue == mask.mValue; + } + + bool IsSetExclusive(std::initializer_list enums) { + EnumFlags flags; + for (auto& e : enums) { + flags.mValue |= static_cast(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(e); + return *this; + } + + EnumFlags& operator&=(TEnum e) const { + mValue &= 1 << static_cast(e); + return *this; + } + + EnumFlags& operator^=(TEnum e) const { + mValue ^= 1 << static_cast(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(e)); } + EnumFlags operator&(TEnum e) const { return EnumFlags(mValue & 1 << static_cast(e)); } + EnumFlags operator^(TEnum e) const { return EnumFlags(mValue ^ 1 << static_cast(e)); } + + EnumFlags operator~() const { return EnumFlags(~mValue); } +}; + +// Helper class for enumerating enum elements for ImGui::Begin/EndCombo +template +struct EnumElement { + const char* name; + TEnum value; +}; -- cgit v1.2.3-70-g09d2