Utilities
Utilities header only library
utils::EnumFlags< T, TMask > Class Template Reference

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

Public Member Functions

 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.
 
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.
 

Detailed Description

template<class T, class TMask = uint32_t>
class utils::EnumFlags< T, TMask >

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.