6 #ifndef UTILS_ENUM_FLAGS_H 7 #define UTILS_ENUM_FLAGS_H 33 template <
class T,
class TMask = u
int32_t>
40 using MaskType = TMask;
50 template<
typename... Types>
52 : mask_(initializeMask(valueToMask(val1), valueToMask(valn) ...))
60 for (
const auto &value : values)
67 inline void Set(
const T val);
70 inline bool IsSet(
const T val)
const;
84 std::vector<T> result;
85 for (
size_t i = 0; i <
sizeof(TMask) * 8; ++i)
87 if (
IsSet(static_cast<T>(i)))
89 result.emplace_back(static_cast<T>(i));
104 return !(*
this == value);
110 for (
const auto value : other.
ToVector())
119 inline typename EnumFlags<T, MaskType>::MaskType valueToMask(
const T val)
const 121 return static_cast<MaskType
>(1) << static_cast<MaskType>(val);
124 template<
typename Type>
125 inline Type initializeMask(
const Type val)
const 127 CheckFlagInBounds(val,
"initialize");
131 template<
typename Type,
typename... Types>
132 inline Type initializeMask(
const Type val1,
const Type val2,
const Types ... vals)
const 134 return initializeMask(val1 | val2, vals...);
139 template<
typename Type>
140 inline void CheckFlagInBounds(
const Type val,
const std::string &action_name)
const 142 const auto val_t =
static_cast<size_t>(val);
143 if (val_t >= pow(2,
sizeof(TMask) * 8))
145 throw std::logic_error(
"EnumFlags Error: attempting to " + action_name +
" a flag with value out of bounds.");
154 template <
class T,
class TMask>
157 CheckFlagInBounds(val,
"set");
158 mask_ |= valueToMask(val);
161 template <
class T,
class TMask>
164 CheckFlagInBounds(val,
"check");
165 return (mask_ & valueToMask(val)) != 0;
168 template <
class T,
class TMask>
172 for (
size_t i = 0; i <
sizeof(TMask) * 8; ++i)
174 if (
IsSet(static_cast<T>(i)))
EnumFlags(const std::vector< EnumType > &values)
Construct from std::vector.
Definition: EnumFlags.h:57
bool operator!=(const T &value) const
Check whether the flags are not exactly one single value.
Definition: EnumFlags.h:102
EnumFlags()
Constructor with no flags to be set.
Definition: EnumFlags.h:44
EnumFlags(const EnumType val1, Types ... valn)
Variadic constructor with a variable amount of flags to be set.
Definition: EnumFlags.h:51
void Set(const T val)
Set a single flag.
Definition: EnumFlags.h:155
bool operator==(const T &value) const
Check whether the flags are exactly one single value.
Definition: EnumFlags.h:96
bool IsAnySet() const
Return whether there is at least one flag set.
Definition: EnumFlags.h:76
bool IsSet(const T val) const
Checks whether a given flag is set.
Definition: EnumFlags.h:162
Template implementation of Enum Flags. Allows the setting and checking of enum values.
Definition: EnumFlags.h:34
uint8_t CountSet() const
Returns the number of flags set.
Definition: EnumFlags.h:169
General namespace for utilities.
Definition: BulletConversions.h:9
EnumFlags< T, TMask > & operator+=(const EnumFlags< T, TMask > &other)
Add other flags.
Definition: EnumFlags.h:108
std::vector< T > ToVector() const
Creates a vector containing all elements set.
Definition: EnumFlags.h:82