aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/CMakeLists.txt2
-rw-r--r--libs/kstd/include/kstd/ext/bitfield_enum64
2 files changed, 66 insertions, 0 deletions
diff --git a/libs/kstd/CMakeLists.txt b/libs/kstd/CMakeLists.txt
index e0c551c..d83e704 100644
--- a/libs/kstd/CMakeLists.txt
+++ b/libs/kstd/CMakeLists.txt
@@ -22,6 +22,8 @@ target_sources("kstd" PUBLIC
"include/kstd/bits/shared_ptr.hpp"
"include/kstd/bits/unique_ptr.hpp"
+ "include/kstd/ext/bitfield_enum"
+
"include/kstd/asm_ptr"
"include/kstd/memory"
"include/kstd/mutex"
diff --git a/libs/kstd/include/kstd/ext/bitfield_enum b/libs/kstd/include/kstd/ext/bitfield_enum
new file mode 100644
index 0000000..327af45
--- /dev/null
+++ b/libs/kstd/include/kstd/ext/bitfield_enum
@@ -0,0 +1,64 @@
+#ifndef KSTD_EXT_BITFIELD_ENUM_HPP
+#define KSTD_EXT_BITFIELD_ENUM_HPP
+
+#include <bit>
+#include <type_traits>
+#include <utility>
+
+namespace kstd::ext
+{
+
+ template<typename EnumType>
+ requires std::is_enum_v<EnumType>
+ struct is_bitfield_enum : std::false_type
+ {
+ };
+
+ template<typename EnumType>
+ concept bitfield_enum = is_bitfield_enum<EnumType>::value;
+
+}; // namespace kstd::ext
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator|(EnumType lhs, EnumType rhs) -> EnumType
+{
+ return std::bit_cast<EnumType>(std::to_underlying(lhs) | std::to_underlying(rhs));
+}
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator|=(EnumType & lhs, EnumType rhs) -> EnumType &
+{
+ return lhs = lhs | rhs;
+}
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator&(EnumType lhs, EnumType rhs) -> EnumType
+{
+ return std::bit_cast<EnumType>(std::to_underlying(lhs) & std::to_underlying(rhs));
+}
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator&=(EnumType & lhs, EnumType rhs) -> EnumType &
+{
+ return lhs = lhs & rhs;
+}
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator^(EnumType lhs, EnumType rhs) -> EnumType
+{
+ return std::bit_cast<EnumType>(std::to_underlying(lhs) ^ std::to_underlying(rhs));
+}
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator^=(EnumType & lhs, EnumType rhs) -> EnumType &
+{
+ return lhs = lhs ^ rhs;
+}
+
+template<kstd::ext::bitfield_enum EnumType>
+constexpr auto operator~(EnumType lhs) -> EnumType
+{
+ return std::bit_cast<EnumType>(~std::to_underlying(lhs));
+}
+
+#endif \ No newline at end of file