diff options
Diffstat (limited to 'libs/kstd/include')
| -rw-r--r-- | libs/kstd/include/kstd/ext/bitfield_enum | 64 |
1 files changed, 64 insertions, 0 deletions
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 |
