template class utils::EnumFlags

Overview

Template implementation of Enum Flags. Allows the setting and checking of enum values. More…

#include <EnumFlags.h>

template <class T, class TMask = uint32_t>
class EnumFlags {
public:
    // construction

    EnumFlags();

    template <typename... Types>
    EnumFlags(const EnumType val1, Types ... valn);

    EnumFlags(const std::vector<EnumType>& values);

    // methods

    void Set(const T val);
    bool IsSet(const T val) const;
    uint8_t CountSet() const;
    bool IsAnySet() const;
    std::vector<T> ToVector() const;
    bool operator==(const T& value) const;
    bool operator!=(const T& value) const;
    EnumFlags<T, TMask>& operator+=(const EnumFlags<T, TMask>& other);
};

Detailed Documentation

Template implementation of Enum Flags. Allows the setting and checking of enum values.

Example usage:

Initialization:
  auto flags = utils::EnumFlags<EnumType>(EnumType::val1, EnumType::val2, ...);
Setting and checking:
  flags.Set(EnumType::val3);
  bool is_val4 = flags.IsSet(EnumType::val4);

The underlying enum class should have its values starting with 0 and incrementing by 1, i.e. default. In case of enum classes with many values, consider using a larger storage type for the bit mask - template argument TMask.

Construction

EnumFlags()

Constructor with no flags to be set.

template <typename... Types>
EnumFlags(const EnumType val1, Types ... valn)

Variadic constructor with a variable amount of flags to be set.

EnumFlags(const std::vector<EnumType>& values)

Construct from std::vector.

Methods

void Set(const T val)

Set a single flag.

bool IsSet(const T val) const

Checks whether a given flag is set.

uint8_t CountSet() const

Returns the number of flags set.

bool IsAnySet() const

Return whether there is at least one flag set.

std::vector<T> ToVector() const

Creates a vector containing all elements set.

bool operator==(const T& value) const

Check whether the flags are exactly one single value.

bool operator!=(const T& value) const

Check whether the flags are not exactly one single value.

EnumFlags<T, TMask>& operator+=(const EnumFlags<T, TMask>& other)

Add other flags.