MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_bit.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_BIT_H_1610068077
12#define INCGUARD_MGL_BIT_H_1610068077
13
14#include <cstdint>
15#include <type_traits>
16
17namespace MGL
18{
19/* ------------------------------------------------------------------------- */
26/* ------------------------------------------------------------------------- */
27template<typename T>
28constexpr T MakeBit(uint8_t bit) noexcept
29{
30 return static_cast<T>(1u << bit);
31}
32
33
34/* ------------------------------------------------------------------------- */
40/* ------------------------------------------------------------------------- */
41template<class EnumType, typename FlagType = uint32_t>
43{
44public:
46 using UnderlyingType = std::underlying_type_t<EnumType>;
47
49 static constexpr size_t kSize = sizeof(FlagType) * 8;
50
51 /* ------------------------------------------------------------------------- */
55 /* ------------------------------------------------------------------------- */
56 constexpr EnumBitFlags() noexcept
57 : _value(0)
58 {
59 }
60
61 /* ------------------------------------------------------------------------- */
66 /* ------------------------------------------------------------------------- */
67 constexpr EnumBitFlags(EnumType value) noexcept
68 : _value(FlagType(1u << UnderlyingType(value)))
69 {
70 }
71
72 /* ------------------------------------------------------------------------- */
77 /* ------------------------------------------------------------------------- */
78 constexpr explicit EnumBitFlags(FlagType value) noexcept
79 : _value(value)
80 {
81 }
82
83 /* ------------------------------------------------------------------------- */
88 /* ------------------------------------------------------------------------- */
89 [[nodiscard]] constexpr const FlagType &GetValue() const noexcept
90 {
91 return _value;
92 }
93
94 /* ------------------------------------------------------------------------- */
101 /* ------------------------------------------------------------------------- */
102 [[nodiscard]] constexpr bool Has(EnumType value) const noexcept
103 {
104 return _value & FlagType(1u << UnderlyingType(value));
105 }
106
107 /* ------------------------------------------------------------------------- */
113 /* ------------------------------------------------------------------------- */
114 [[nodiscard]] constexpr bool HasAny() const noexcept
115 {
116 return _value != 0;
117 }
118
119 /* ------------------------------------------------------------------------- */
125 /* ------------------------------------------------------------------------- */
126 constexpr EnumBitFlags &Set(EnumType value) noexcept
127 {
128 _value |= FlagType(1u << UnderlyingType(value));
129 return *this;
130 }
131
132 /* ------------------------------------------------------------------------- */
138 /* ------------------------------------------------------------------------- */
139 constexpr EnumBitFlags &Clear(EnumType value) noexcept
140 {
141 _value &= ~FlagType(1u << UnderlyingType(value));
142 return *this;
143 }
144
145 /* ------------------------------------------------------------------------- */
150 /* ------------------------------------------------------------------------- */
151 constexpr EnumBitFlags &Clear() noexcept
152 {
153 _value = 0;
154 return *this;
155 }
156
157 /* ------------------------------------------------------------------------- */
163 /* ------------------------------------------------------------------------- */
164 constexpr EnumBitFlags &Flip(EnumType value) noexcept
165 {
166 _value ^= FlagType(1 << UnderlyingType(value));
167 return *this;
168 }
169
170 /* ------------------------------------------------------------------------- */
175 /* ------------------------------------------------------------------------- */
176 constexpr EnumBitFlags &Flip() noexcept
177 {
178 _value = ~_value;
179 return *this;
180 }
181
182 /* ------------------------------------------------------------------------- */
188 /* ------------------------------------------------------------------------- */
189 constexpr explicit operator bool() const noexcept
190 {
191 return _value != 0;
192 }
193
194 /* ------------------------------------------------------------------------- */
200 /* ------------------------------------------------------------------------- */
201 constexpr bool operator!() const noexcept
202 {
203 return _value == 0;
204 }
205
206 /* ------------------------------------------------------------------------- */
212 /* ------------------------------------------------------------------------- */
213 constexpr EnumBitFlags &operator|=(EnumType rhs) noexcept
214 {
215 _value |= FlagType(1 << UnderlyingType(rhs));
216 return *this;
217 }
218
219 /* ------------------------------------------------------------------------- */
225 /* ------------------------------------------------------------------------- */
226 constexpr EnumBitFlags &operator|=(const EnumBitFlags &rhs) noexcept
227 {
228 _value |= rhs._value;
229 return *this;
230 }
231
232 /* ------------------------------------------------------------------------- */
238 /* ------------------------------------------------------------------------- */
239 constexpr EnumBitFlags &operator&=(EnumType rhs) noexcept
240 {
241 _value &= FlagType(1 << UnderlyingType(rhs));
242 return *this;
243 }
244
245 /* ------------------------------------------------------------------------- */
251 /* ------------------------------------------------------------------------- */
252 constexpr EnumBitFlags &operator&=(const EnumBitFlags &rhs) noexcept
253 {
254 _value &= rhs._value;
255 return *this;
256 }
257
258 /* ------------------------------------------------------------------------- */
264 /* ------------------------------------------------------------------------- */
265 constexpr EnumBitFlags &operator^=(EnumType rhs) noexcept
266 {
267 _value ^= FlagType(1 << UnderlyingType(rhs));
268 return *this;
269 }
270
271 /* ------------------------------------------------------------------------- */
277 /* ------------------------------------------------------------------------- */
278 constexpr EnumBitFlags &operator^=(const EnumBitFlags &rhs) noexcept
279 {
280 _value ^= rhs._value;
281 return *this;
282 }
283
284 /* ------------------------------------------------------------------------- */
290 /* ------------------------------------------------------------------------- */
291 constexpr EnumBitFlags operator|(EnumType rhs) const noexcept
292 {
293 return EnumBitFlags(_value | FlagType(1u << UnderlyingType(rhs)));
294 }
295
296 /* ------------------------------------------------------------------------- */
302 /* ------------------------------------------------------------------------- */
303 constexpr EnumBitFlags operator|(const EnumBitFlags &rhs) const noexcept
304 {
305 return EnumBitFlags(_value | rhs._value);
306 }
307
308 /* ------------------------------------------------------------------------- */
314 /* ------------------------------------------------------------------------- */
315 constexpr EnumBitFlags operator&(EnumType rhs) const noexcept
316 {
317 return EnumBitFlags(_value & FlagType(1u << UnderlyingType(rhs)));
318 }
319
320 /* ------------------------------------------------------------------------- */
326 /* ------------------------------------------------------------------------- */
327 constexpr EnumBitFlags operator&(const EnumBitFlags &rhs) const noexcept
328 {
329 return EnumBitFlags(_value & rhs._value);
330 }
331
332 /* ------------------------------------------------------------------------- */
338 /* ------------------------------------------------------------------------- */
339 constexpr EnumBitFlags operator^(EnumType rhs) const noexcept
340 {
341 return EnumBitFlags(_value ^ FlagType(1u << UnderlyingType(rhs)));
342 }
343
344 /* ------------------------------------------------------------------------- */
350 /* ------------------------------------------------------------------------- */
351 constexpr EnumBitFlags operator^(const EnumBitFlags &rhs) const noexcept
352 {
353 return EnumBitFlags(_value ^ rhs._value);
354 }
355
356 /* ------------------------------------------------------------------------- */
361 /* ------------------------------------------------------------------------- */
362 constexpr EnumBitFlags operator~() const noexcept
363 {
364 return EnumBitFlags(~_value);
365 }
366
367 /* ------------------------------------------------------------------------- */
372 /* ------------------------------------------------------------------------- */
373 constexpr bool operator==(const EnumBitFlags &rhs) const noexcept
374 {
375 return _value == rhs._value;
376 }
377
378 /* ------------------------------------------------------------------------- */
383 /* ------------------------------------------------------------------------- */
384 constexpr bool operator!=(const EnumBitFlags &rhs) const noexcept
385 {
386 return _value != rhs._value;
387 }
388
389private:
390 FlagType _value;
391};
392} // namespace MGL
393#endif // INCGUARD_MGL_BIT_H_1610068077
394
395// vim: et ts=4 sw=4 sts=4
スコープを持つ列挙型に対応したビットフラグを扱うクラス
Definition mgl_bit.h:43
constexpr EnumBitFlags(FlagType value) noexcept
コンストラクタ
Definition mgl_bit.h:78
constexpr EnumBitFlags & operator|=(const EnumBitFlags &rhs) noexcept
自身の型との|=演算子
Definition mgl_bit.h:226
constexpr EnumBitFlags & operator&=(const EnumBitFlags &rhs) noexcept
自身の型との&=演算子
Definition mgl_bit.h:252
constexpr EnumBitFlags & operator^=(EnumType rhs) noexcept
EnumType型との^=演算子
Definition mgl_bit.h:265
constexpr EnumBitFlags & operator|=(EnumType rhs) noexcept
EnumType型との|=演算子
Definition mgl_bit.h:213
constexpr bool HasAny() const noexcept
いずれかの値を保持しているかを取得
Definition mgl_bit.h:114
constexpr EnumBitFlags & Set(EnumType value) noexcept
指定した値をセットする
Definition mgl_bit.h:126
constexpr bool operator!() const noexcept
否定演算子
Definition mgl_bit.h:201
constexpr EnumBitFlags & Clear() noexcept
全てのフラグをクリアする
Definition mgl_bit.h:151
std::underlying_type_t< EnumType > UnderlyingType
EnumTypeの基底の型
Definition mgl_bit.h:46
constexpr EnumBitFlags operator~() const noexcept
~演算子(全てのビットの反転)
Definition mgl_bit.h:362
constexpr const FlagType & GetValue() const noexcept
フラグの値を取得
Definition mgl_bit.h:89
constexpr EnumBitFlags & Flip() noexcept
全ての値を反転させる
Definition mgl_bit.h:176
constexpr EnumBitFlags operator&(EnumType rhs) const noexcept
EnumType型との&演算子
Definition mgl_bit.h:315
static constexpr size_t kSize
ビットフラグのサイズ
Definition mgl_bit.h:49
constexpr EnumBitFlags operator^(EnumType rhs) const noexcept
EnumType型との^演算子
Definition mgl_bit.h:339
constexpr EnumBitFlags & operator^=(const EnumBitFlags &rhs) noexcept
自身の型との^=演算子
Definition mgl_bit.h:278
constexpr EnumBitFlags & Flip(EnumType value) noexcept
指定した値のフラグを反転させる
Definition mgl_bit.h:164
constexpr EnumBitFlags operator^(const EnumBitFlags &rhs) const noexcept
自身の型との^演算子
Definition mgl_bit.h:351
constexpr EnumBitFlags operator|(const EnumBitFlags &rhs) const noexcept
自身の型との|演算子
Definition mgl_bit.h:303
constexpr EnumBitFlags operator|(EnumType rhs) const noexcept
EnumType型との|演算子
Definition mgl_bit.h:291
constexpr EnumBitFlags & Clear(EnumType value) noexcept
指定した値をクリアする
Definition mgl_bit.h:139
constexpr EnumBitFlags() noexcept
コンストラクタ
Definition mgl_bit.h:56
constexpr bool Has(EnumType value) const noexcept
指定した値を保持しているかを取得
Definition mgl_bit.h:102
constexpr bool operator==(const EnumBitFlags &rhs) const noexcept
等価演算子
Definition mgl_bit.h:373
constexpr EnumBitFlags & operator&=(EnumType rhs) noexcept
EnumType型との&=演算子
Definition mgl_bit.h:239
constexpr EnumBitFlags operator&(const EnumBitFlags &rhs) const noexcept
自身の型との&演算子
Definition mgl_bit.h:327
constexpr bool operator!=(const EnumBitFlags &rhs) const noexcept
不等価演算子
Definition mgl_bit.h:384
constexpr EnumBitFlags(EnumType value) noexcept
コンストラクタ
Definition mgl_bit.h:67
constexpr T MakeBit(uint8_t bit) noexcept
ビットフラグを生成
Definition mgl_bit.h:28