diff --git a/libs/util/include/psemek/util/flag_set.hpp b/libs/util/include/psemek/util/flag_set.hpp new file mode 100644 index 00000000..51a99259 --- /dev/null +++ b/libs/util/include/psemek/util/flag_set.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +namespace psemek::util +{ + + template + struct flag_set + { + using flag_type = Flag; + using underlying_type = std::conditional_t, std::underlying_type_t, Flag>; + using repr_type = Repr; + + repr_type value = repr_type{}; + + flag_set() = default; + + flag_set(flag_set const &) = default; + + template + flag_set(Flags const & ... flags) + : value{((1 << static_cast(flags)) | ...)} + {} + + bool is_set(Flag const & flag) const + { + return value & (1 << static_cast(flag)); + } + + void set(Flag const & flag) + { + value |= (1 << static_cast(flag)); + } + + void flip(Flag const & flag) + { + value ^= (1 << static_cast(flag)); + } + + void unset(Flag const & flag) + { + value &= ~(1 << static_cast(flag)); + } + + void clear() + { + value = {}; + } + }; + +}