Custom bitset implementation that allows using enumeration values as indices.The used enumeration type used must:
- Derive from std::size_t
- Not set any value for the labels (optionally, value 0 can be set for the first one)
- Contain a
_Total
label at the end
The _Total
label is used to determine the number of values in the enumeration (by explicitly casting it to an std::size_t value).
{c++}
enum class MyEnum : std::size_t
{
A,
B,
C,
_Total
};
EnumSet<MyEnum> set{MyEnum::A, MyEnum::C};
set.test(MyEnum::A);
set.set{MyEnum::B};
set.test(0);
set.set(1);
- Template Parameters
-
TEnum | Enumeration type to use. |
#pragma once
#include <bitset>
#include <initializer_list>
#include <type_traits>
{
template<class TEnum>
requires std::is_enum_v<TEnum>
class EnumSet : public std::bitset<static_cast<std::size_t>(TEnum::_Total)>
{
public:
using BaseType = std::bitset<static_cast<std::size_t>(TEnum::_Total)>;
using BaseType::BaseType;
using BaseType::operator[];
using BaseType::set;
using BaseType::test;
EnumSet(std::initializer_list<TEnum> values)
{
for (auto v : values)
{
}
}
{
return BaseType::operator[](static_cast<std::size_t>(pos));
}
{
return BaseType::operator[](static_cast<std::size_t>(pos));
}
{
return test(
static_cast<std::size_t
>(pos));
}
{
set(
static_cast<std::size_t
>(pos), value);
return *this;
}
};
}
BaseType::reference reference
Definition: enum-set.hpp:55
bool test(EnumType pos) const
Access a specific bit.
Definition: enum-set.hpp:106
std::bitset< static_cast< std::size_t >(TEnum::_Total)> BaseType
Base biset type.
Definition: enum-set.hpp:53
bool operator[](EnumType pos) const
Access a specific bit.
Definition: enum-set.hpp:82
EnumSet(std::initializer_list< TEnum > values)
Constructor from a set of values.
Definition: enum-set.hpp:66
TEnum EnumType
Enumeration type used.
Definition: enum-set.hpp:50
EnumSet & set(EnumType pos, bool value=true)
Set the value of a specific bit.
Definition: enum-set.hpp:119
Definition: enum-set.hpp:8