SPH
Flags.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Assert.h"
9 #include "common/Traits.h"
10 #include "math/MathUtils.h"
11 
13 
14 struct EmptyFlags {};
15 
17 
19 template <typename TEnum>
20 class Flags {
21 private:
22  using TValue = std::underlying_type_t<TEnum>;
23  TValue data = TValue(0);
24 
25 public:
27  constexpr Flags() = default;
28 
30  constexpr Flags(const Flags& other)
31  : data(other.data) {}
32 
36  template <typename... TArgs>
37  constexpr Flags(const TEnum flag, const TArgs... others)
38  : data(construct(flag, others...)) {}
39 
43  constexpr Flags(const EmptyFlags) {}
44 
49  constexpr static Flags fromValue(const TValue value) {
50  Flags flags;
51  flags.data = value;
52  return flags;
53  }
54 
56  Flags& operator=(const Flags& other) {
57  data = other.data;
58  return *this;
59  }
60 
64  Flags& operator=(const TEnum flag) {
65  SPH_ASSERT(isPower2(TValue(flag)));
66  data = TValue(flag);
67  return *this;
68  }
69 
72  data = TValue(0);
73  return *this;
74  }
75 
77  INLINE constexpr bool has(const TEnum flag) const {
78  return (data & TValue(flag)) != 0;
79  }
80 
82  template <typename... TArgs>
83  INLINE constexpr bool hasAny(const TEnum flag, const TArgs... others) const {
84  return has(flag) || hasAny(others...);
85  }
86 
88  template <typename... TArgs>
89  INLINE constexpr bool hasAll(const TEnum flag, const TArgs... others) const {
90  return has(flag) && hasAll(others...);
91  }
92 
94  INLINE void set(const TEnum flag) {
95  SPH_ASSERT(isPower2(TValue(flag)));
96  data |= TValue(flag);
97  }
98 
102  INLINE void unset(const TEnum flag) {
103  SPH_ASSERT(isPower2(TValue(flag)));
104  data &= ~TValue(flag);
105  }
106 
108  INLINE void setIf(const TEnum flag, const bool use) {
109  SPH_ASSERT(isPower2(TValue(flag)));
110  if (use) {
111  data |= TValue(flag);
112  } else {
113  data &= ~TValue(flag);
114  }
115  }
116 
118  INLINE TValue value() const {
119  return data;
120  }
121 
123  INLINE constexpr Flags operator|(const TEnum flag) const {
124  SPH_ASSERT(isPower2(TValue(flag)));
125  Flags<TEnum> flags(*this);
126  flags.set(flag);
127  return flags;
128  }
129 
131  INLINE bool operator==(const Flags& other) const {
132  return data == other.data;
133  }
134 
136  INLINE bool operator!=(const Flags& other) const {
137  return data != other.data;
138  }
139 
140 private:
141  // overload with no argument ending the recursion
142  INLINE constexpr bool hasAny() const {
143  return false;
144  }
145 
146  INLINE constexpr bool hasAll() const {
147  return true;
148  }
149 
150  template <typename... TArgs>
151  INLINE constexpr TValue construct(const TEnum first, const TArgs... rest) {
152  SPH_ASSERT(TValue(first) == 0 || isPower2(TValue(first)));
153  return TValue(first) | construct(rest...);
154  }
155 
156  INLINE constexpr TValue construct() {
157  return 0;
158  }
159 };
160 
161 template <typename TEnum, typename = std::enable_if_t<IsEnumClass<TEnum>::value>>
162 INLINE constexpr Flags<TEnum> operator|(const TEnum flag1, const TEnum flag2) {
163  return Flags<TEnum>(flag1, flag2);
164 }
165 
166 template <typename T>
167 struct FlagsTraits {
168  static constexpr bool isFlags = false;
169 };
170 
171 template <typename T>
172 struct FlagsTraits<Flags<T>> {
173  static constexpr bool isFlags = true;
174  using Type = T;
175 };
176 
Custom assertions.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
constexpr INLINE Flags< TEnum > operator|(const TEnum flag1, const TEnum flag2)
Definition: Flags.h:162
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
Additional math routines (with more includes).
constexpr INLINE bool isPower2(const Size n) noexcept
Returns true if given n is a power of 2. N must at least 1.
Definition: MathUtils.h:72
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Few non-standard type traits.
Wrapper of an integral value providing functions for reading and modifying individual bits.
Definition: Flags.h:20
INLINE void unset(const TEnum flag)
Removed a single flag.
Definition: Flags.h:102
constexpr INLINE Flags operator|(const TEnum flag) const
Returns a Flags object by adding a single flag to currently stored values.
Definition: Flags.h:123
constexpr INLINE bool has(const TEnum flag) const
Checks if the object has a given flag.
Definition: Flags.h:77
INLINE bool operator!=(const Flags &other) const
Checks for inequality with other Flags object.
Definition: Flags.h:136
Flags & operator=(const Flags &other)
Copies other Flags object.
Definition: Flags.h:56
constexpr INLINE bool hasAll(const TEnum flag, const TArgs... others) const
Checks if the object has all of given flags.
Definition: Flags.h:89
constexpr INLINE bool hasAny(const TEnum flag, const TArgs... others) const
Checks if the object has any of given flags.
Definition: Flags.h:83
Flags & operator=(const TEnum flag)
Assigns a single flag, all previous flags are deleted.
Definition: Flags.h:64
INLINE TValue value() const
Returns the underlying value.
Definition: Flags.h:118
constexpr Flags(const TEnum flag, const TArgs... others)
Constructs object given a list of flags.
Definition: Flags.h:37
constexpr Flags()=default
Constructs empty flags.
Flags & operator=(const EmptyFlags)
Deletes all flags.
Definition: Flags.h:71
constexpr Flags(const Flags &other)
Constucts flags by copying other object.
Definition: Flags.h:30
constexpr static Flags fromValue(const TValue value)
Constructs object from underlying value.
Definition: Flags.h:49
INLINE void setIf(const TEnum flag, const bool use)
Sets or removes given flag based given boolean value.
Definition: Flags.h:108
INLINE bool operator==(const Flags &other) const
Checks for equality with other Flags object.
Definition: Flags.h:131
INLINE void set(const TEnum flag)
Adds a single flag into the object. All previously stored flags are kept unchanged.
Definition: Flags.h:94
constexpr Flags(const EmptyFlags)
Constructs empty flags.
Definition: Flags.h:43
static constexpr bool isFlags
Definition: Flags.h:168