aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/bitmask.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-01 22:52:01 +0800
committercrupest <crupest@outlook.com>2020-01-01 22:52:01 +0800
commit36708e88596364e81dc9ba2fd3cbddbcaedeedc0 (patch)
tree0c7990b9e64c6ea1e9df7c01f3521c4fbf7ea2a0 /include/cru/common/bitmask.hpp
parentae6f797561cdfa438ebef1fbbf94d784d315e655 (diff)
downloadcru-36708e88596364e81dc9ba2fd3cbddbcaedeedc0.tar.gz
cru-36708e88596364e81dc9ba2fd3cbddbcaedeedc0.tar.bz2
cru-36708e88596364e81dc9ba2fd3cbddbcaedeedc0.zip
...
Diffstat (limited to 'include/cru/common/bitmask.hpp')
-rw-r--r--include/cru/common/bitmask.hpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/include/cru/common/bitmask.hpp b/include/cru/common/bitmask.hpp
new file mode 100644
index 00000000..6dfb651c
--- /dev/null
+++ b/include/cru/common/bitmask.hpp
@@ -0,0 +1,42 @@
+#pragma once
+#include "base.hpp"
+
+namespace cru {
+template <typename Tag, typename TUnderlying = unsigned>
+struct Bitmask final {
+ using Underlying = TUnderlying;
+
+ constexpr Bitmask() : value(0) {}
+ constexpr explicit Bitmask(TUnderlying value) : value(value) {}
+
+ CRU_DEFAULT_COPY(Bitmask)
+ CRU_DEFAULT_MOVE(Bitmask)
+
+ ~Bitmask() = default;
+
+ Bitmask operator|(Bitmask rhs) const { return Bitmask(value | rhs.value); }
+ Bitmask operator&(Bitmask rhs) const { return Bitmask(value & rhs.value); }
+ Bitmask operator^(Bitmask rhs) const { return Bitmask(value ^ rhs.value); }
+ Bitmask operator~() const { return Bitmask(~value); }
+ Bitmask& operator|=(Bitmask rhs) {
+ value |= rhs;
+ return *this;
+ }
+ Bitmask& operator&=(Bitmask rhs) {
+ value &= rhs;
+ return *this;
+ }
+ Bitmask& operator^=(Bitmask rhs) {
+ value ^= rhs;
+ return *this;
+ }
+
+ bool operator==(Bitmask rhs) const { return this->value == rhs.value; }
+ bool operator!=(Bitmask rhs) const { return this->value != rhs.value; }
+
+ explicit operator TUnderlying() const { return value; }
+ explicit operator bool() const { return value != 0; }
+
+ TUnderlying value;
+};
+} // namespace cru