Add util::flag_set for enum-based bitsets
This commit is contained in:
parent
9d8e83d957
commit
5eab34a1dd
1 changed files with 53 additions and 0 deletions
53
libs/util/include/psemek/util/flag_set.hpp
Normal file
53
libs/util/include/psemek/util/flag_set.hpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
|
||||
namespace psemek::util
|
||||
{
|
||||
|
||||
template <typename Flag, typename Repr = std::uint32_t>
|
||||
struct flag_set
|
||||
{
|
||||
using flag_type = Flag;
|
||||
using underlying_type = std::conditional_t<std::is_enum_v<Flag>, std::underlying_type_t<Flag>, Flag>;
|
||||
using repr_type = Repr;
|
||||
|
||||
repr_type value = repr_type{};
|
||||
|
||||
flag_set() = default;
|
||||
|
||||
flag_set(flag_set const &) = default;
|
||||
|
||||
template <typename ... Flags>
|
||||
flag_set(Flags const & ... flags)
|
||||
: value{((1 << static_cast<underlying_type>(flags)) | ...)}
|
||||
{}
|
||||
|
||||
bool is_set(Flag const & flag) const
|
||||
{
|
||||
return value & (1 << static_cast<underlying_type>(flag));
|
||||
}
|
||||
|
||||
void set(Flag const & flag)
|
||||
{
|
||||
value |= (1 << static_cast<underlying_type>(flag));
|
||||
}
|
||||
|
||||
void flip(Flag const & flag)
|
||||
{
|
||||
value ^= (1 << static_cast<underlying_type>(flag));
|
||||
}
|
||||
|
||||
void unset(Flag const & flag)
|
||||
{
|
||||
value &= ~(1 << static_cast<underlying_type>(flag));
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
value = {};
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue