Kouta
A small application framework based on Boost
/home/runner/work/kouta/kouta/include/kouta/utils/enum-set.hpp

Custom bitset implementation that allows using enumeration values as indices.The used enumeration type used must:

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};
// Test and set using enumeration values
set.test(MyEnum::A);
set.set{MyEnum::B};
// Or raw indices
set.test(0);
set.set(1);
Template Parameters
TEnumEnumeration type to use.
#pragma once
#include <bitset>
#include <initializer_list>
#include <type_traits>
namespace kouta::utils
{
template<class TEnum>
requires std::is_enum_v<TEnum>
class EnumSet : public std::bitset<static_cast<std::size_t>(TEnum::_Total)>
{
public:
using EnumType = TEnum;
using BaseType = std::bitset<static_cast<std::size_t>(TEnum::_Total)>;
using reference = BaseType::reference;
// Re-export some methods
using BaseType::BaseType;
using BaseType::operator[];
using BaseType::set;
using BaseType::test;
EnumSet(std::initializer_list<TEnum> values)
{
for (auto v : values)
{
set(v);
}
}
bool operator[](EnumType pos) const
{
return BaseType::operator[](static_cast<std::size_t>(pos));
}
{
return BaseType::operator[](static_cast<std::size_t>(pos));
}
bool test(EnumType pos) const
{
return test(static_cast<std::size_t>(pos));
}
EnumSet& set(EnumType pos, bool value = true)
{
set(static_cast<std::size_t>(pos), value);
return *this;
}
};
} // namespace kouta::utils
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