Chatterino
FlagsEnum.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <initializer_list>
4 #include <type_traits>
5 
6 namespace chatterino {
7 
8 template <typename T, typename Q = typename std::underlying_type<T>::type>
9 class FlagsEnum
10 {
11 public:
13  : value_(static_cast<T>(0))
14  {
15  }
16 
17  FlagsEnum(T value)
18  : value_(value)
19  {
20  }
21 
22  FlagsEnum(std::initializer_list<T> flags)
23  {
24  for (auto flag : flags)
25  {
26  this->set(flag);
27  }
28  }
29 
30  bool operator==(const FlagsEnum<T> &other)
31  {
32  return this->value_ == other.value_;
33  }
34 
35  bool operator!=(const FlagsEnum &other)
36  {
37  return this->value_ != other.value_;
38  }
39 
40  void set(T flag)
41  {
42  reinterpret_cast<Q &>(this->value_) |= static_cast<Q>(flag);
43  }
44 
45  void unset(T flag)
46  {
47  reinterpret_cast<Q &>(this->value_) &= ~static_cast<Q>(flag);
48  }
49 
50  void set(T flag, bool value)
51  {
52  if (value)
53  this->set(flag);
54  else
55  this->unset(flag);
56  }
57 
58  bool has(T flag) const
59  {
60  return static_cast<Q>(this->value_) & static_cast<Q>(flag);
61  }
62 
64  {
65  FlagsEnum xd;
66  xd.value_ = this->value_;
67  xd.set(flag, true);
68 
69  return xd;
70  }
71 
72  bool hasAny(FlagsEnum flags) const
73  {
74  return static_cast<Q>(this->value_) & static_cast<Q>(flags.value_);
75  }
76 
77  bool hasAll(FlagsEnum<T> flags) const
78  {
79  return (static_cast<Q>(this->value_) & static_cast<Q>(flags.value_)) &&
80  static_cast<Q>(flags->value);
81  }
82 
83  bool hasNone(std::initializer_list<T> flags) const
84  {
85  return !this->hasAny(flags);
86  }
87 
88 private:
89  T value_{};
90 };
91 
92 } // namespace chatterino
bool operator!=(const FlagsEnum &other)
Definition: FlagsEnum.hpp:35
bool operator==(const FlagsEnum< T > &other)
Definition: FlagsEnum.hpp:30
FlagsEnum(T value)
Definition: FlagsEnum.hpp:17
Definition: Application.cpp:48
FlagsEnum(std::initializer_list< T > flags)
Definition: FlagsEnum.hpp:22
bool hasAny(FlagsEnum flags) const
Definition: FlagsEnum.hpp:72
Definition: FlagsEnum.hpp:9
FlagsEnum()
Definition: FlagsEnum.hpp:12
bool has(T flag) const
Definition: FlagsEnum.hpp:58
void unset(T flag)
Definition: FlagsEnum.hpp:45
bool hasNone(std::initializer_list< T > flags) const
Definition: FlagsEnum.hpp:83
bool hasAll(FlagsEnum< T > flags) const
Definition: FlagsEnum.hpp:77
void set(T flag)
Definition: FlagsEnum.hpp:40
FlagsEnum operator|(T flag)
Definition: FlagsEnum.hpp:63