2020-09-09 12:18:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
// Wrapper around enum that can have multiple values (or none) set at once.
|
|
|
|
template <typename EnumTypeT, typename StorageTypeT = std::uint64_t>
|
|
|
|
struct MultiEnum
|
|
|
|
{
|
|
|
|
using StorageType = StorageTypeT;
|
|
|
|
using EnumType = EnumTypeT;
|
|
|
|
|
|
|
|
MultiEnum() = default;
|
|
|
|
|
|
|
|
template <typename ... EnumValues, typename = std::enable_if_t<std::conjunction_v<std::is_same<EnumTypeT, EnumValues>...>>>
|
2020-12-01 09:10:12 +00:00
|
|
|
constexpr explicit MultiEnum(EnumValues ... v)
|
2020-09-09 12:18:02 +00:00
|
|
|
: MultiEnum((toBitFlag(v) | ... | 0u))
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <typename ValueType, typename = std::enable_if_t<std::is_convertible_v<ValueType, StorageType>>>
|
2020-12-01 09:10:12 +00:00
|
|
|
constexpr explicit MultiEnum(ValueType v)
|
2020-09-09 12:18:02 +00:00
|
|
|
: bitset(v)
|
|
|
|
{
|
|
|
|
static_assert(std::is_unsigned_v<ValueType>);
|
|
|
|
static_assert(std::is_unsigned_v<StorageType> && std::is_integral_v<StorageType>);
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiEnum(const MultiEnum & other) = default;
|
|
|
|
MultiEnum & operator=(const MultiEnum & other) = default;
|
|
|
|
|
|
|
|
bool isSet(EnumType value) const
|
|
|
|
{
|
|
|
|
return bitset & toBitFlag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(EnumType value)
|
|
|
|
{
|
|
|
|
bitset |= toBitFlag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unSet(EnumType value)
|
|
|
|
{
|
|
|
|
bitset &= ~(toBitFlag(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
bitset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageType getValue() const
|
|
|
|
{
|
|
|
|
return bitset;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType, typename = std::enable_if_t<std::is_convertible_v<ValueType, StorageType>>>
|
|
|
|
void setValue(ValueType new_value)
|
|
|
|
{
|
|
|
|
// Can't set value from any enum avoid confusion
|
|
|
|
static_assert(!std::is_enum_v<ValueType>);
|
|
|
|
bitset = new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const MultiEnum & other) const
|
|
|
|
{
|
|
|
|
return bitset == other.bitset;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType, typename = std::enable_if_t<std::is_convertible_v<ValueType, StorageType>>>
|
|
|
|
bool operator==(ValueType other) const
|
|
|
|
{
|
|
|
|
// Shouldn't be comparable with any enum to avoid confusion
|
|
|
|
static_assert(!std::is_enum_v<ValueType>);
|
|
|
|
return bitset == other;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
bool operator!=(U && other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType, typename = std::enable_if_t<std::is_convertible_v<ValueType, StorageType>>>
|
|
|
|
friend bool operator==(ValueType left, MultiEnum right)
|
|
|
|
{
|
2020-09-11 15:51:08 +00:00
|
|
|
return right.operator==(left);
|
2020-09-09 12:18:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 10:02:30 +00:00
|
|
|
template <typename L, typename = typename std::enable_if<!std::is_same_v<L, MultiEnum>>::type>
|
2020-09-09 12:18:02 +00:00
|
|
|
friend bool operator!=(L left, MultiEnum right)
|
|
|
|
{
|
2020-09-11 15:51:08 +00:00
|
|
|
return !(right.operator==(left));
|
2020-09-09 12:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
StorageType bitset = 0;
|
|
|
|
|
2020-12-01 09:10:12 +00:00
|
|
|
static constexpr StorageType toBitFlag(EnumType v) { return StorageType{1} << static_cast<StorageType>(v); }
|
2020-09-09 12:18:02 +00:00
|
|
|
};
|