aboutsummaryrefslogtreecommitdiff
path: root/ProjectBrussel/Common/Enum.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-06-03 23:30:01 -0700
committerrtk0c <[email protected]>2022-06-03 23:30:01 -0700
commit791b3f354b378769bffe623b05f1305c91b77101 (patch)
tree5409b311e6232eb4a6d3f8259b780d76b8ee1c59 /ProjectBrussel/Common/Enum.hpp
parent60ccc62f4934e44ad5b905fdbcf458302b8d8a09 (diff)
Changeset: 64 [WIP] Rename directoriesmaster-switch-to-build2
Diffstat (limited to 'ProjectBrussel/Common/Enum.hpp')
-rw-r--r--ProjectBrussel/Common/Enum.hpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/ProjectBrussel/Common/Enum.hpp b/ProjectBrussel/Common/Enum.hpp
new file mode 100644
index 0000000..8ad75ba
--- /dev/null
+++ b/ProjectBrussel/Common/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{ static_cast<Underlying>(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) {
+ mValue |= that.mValue;
+ return *this;
+ }
+
+ EnumFlags& operator&=(EnumFlags that) {
+ mValue &= that.mValue;
+ return *this;
+ }
+
+ EnumFlags& operator^=(EnumFlags that) {
+ mValue ^= that.mValue;
+ return *this;
+ }
+
+ EnumFlags& operator|=(TEnum e) {
+ mValue |= 1 << static_cast<Underlying>(e);
+ return *this;
+ }
+
+ EnumFlags& operator&=(TEnum e) {
+ mValue &= 1 << static_cast<Underlying>(e);
+ return *this;
+ }
+
+ EnumFlags& operator^=(TEnum e) {
+ 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); }
+};