Utilities
Utilities header only library
EnumFlags.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_ENUM_FLAGS_H
7 #define UTILS_ENUM_FLAGS_H
8 
9 #include <string>
10 #include <vector>
11 #include <stdexcept>
12 #include <cmath>
13 
14 namespace utils
15 {
16 
33  template <class T, class TMask = uint32_t>
34  class EnumFlags
35  {
36  private:
38  using EnumType = T;
40  using MaskType = TMask;
41 
42  public:
45  : mask_(0)
46  {
47  }
48 
50  template<typename... Types>
51  EnumFlags(const EnumType val1, Types ... valn)
52  : mask_(initializeMask(valueToMask(val1), valueToMask(valn) ...))
53  {
54  }
55 
57  EnumFlags(const std::vector<EnumType> &values)
58  : EnumFlags()
59  {
60  for (const auto &value : values)
61  {
62  Set(value);
63  }
64  }
65 
67  inline void Set(const T val);
68 
70  inline bool IsSet(const T val) const;
71 
73  inline uint8_t CountSet() const;
74 
76  inline bool IsAnySet() const
77  {
78  return CountSet() > 0;
79  }
80 
82  inline std::vector<T> ToVector() const
83  {
84  std::vector<T> result;
85  for (size_t i = 0; i < sizeof(TMask) * 8; ++i)
86  {
87  if (IsSet(static_cast<T>(i)))
88  {
89  result.emplace_back(static_cast<T>(i));
90  }
91  }
92  return result;
93  }
94 
96  bool operator==(const T &value) const
97  {
98  return CountSet() == 1 && IsSet(value);
99  }
100 
102  bool operator!=(const T &value) const
103  {
104  return !(*this == value);
105  }
106 
109  {
110  for (const auto value : other.ToVector())
111  {
112  Set(value);
113  }
114  return *this;
115  }
116 
117  private:
119  inline typename EnumFlags<T, MaskType>::MaskType valueToMask(const T val) const
120  {
121  return static_cast<MaskType>(1) << static_cast<MaskType>(val);
122  }
123 
124  template<typename Type>
125  inline Type initializeMask(const Type val) const
126  {
127  CheckFlagInBounds(val, "initialize");
128  return val;
129  }
130 
131  template<typename Type, typename... Types>
132  inline Type initializeMask(const Type val1, const Type val2, const Types ... vals) const
133  {
134  return initializeMask(val1 | val2, vals...);
135  }
136 
137  private:
139  template<typename Type>
140  inline void CheckFlagInBounds(const Type val, const std::string &action_name) const
141  {
142  const auto val_t = static_cast<size_t>(val);
143  if (val_t >= pow(2, sizeof(TMask) * 8))
144  {
145  throw std::logic_error("EnumFlags Error: attempting to " + action_name + " a flag with value out of bounds.");
146  }
147  }
148 
149  private:
151  TMask mask_;
152  };
153 
154  template <class T, class TMask>
155  inline void EnumFlags<T, TMask>::Set(const T val)
156  {
157  CheckFlagInBounds(val, "set");
158  mask_ |= valueToMask(val);
159  }
160 
161  template <class T, class TMask>
162  inline bool EnumFlags<T, TMask>::IsSet(const T val) const
163  {
164  CheckFlagInBounds(val, "check");
165  return (mask_ & valueToMask(val)) != 0;
166  }
167 
168  template <class T, class TMask>
169  inline uint8_t EnumFlags<T, TMask>::CountSet() const
170  {
171  uint8_t count = 0;
172  for (size_t i = 0; i < sizeof(TMask) * 8; ++i)
173  {
174  if (IsSet(static_cast<T>(i)))
175  {
176  ++count;
177  }
178  }
179  return count;
180  }
181 
182 }
183 #endif /* !UTILS_ENUM_FLAGS_H */
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