-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBitmask.h
More file actions
61 lines (46 loc) · 2.06 KB
/
Bitmask.h
File metadata and controls
61 lines (46 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#include <cassert>
#include <limits>
#include <type_traits>
namespace Ray {
template <class enum_type, typename = typename std::enable_if<std::is_enum<enum_type>::value>::type> class Bitmask {
using underlying_type = typename std::underlying_type<enum_type>::type;
static constexpr underlying_type to_mask(const enum_type e) {
assert(static_cast<underlying_type>(e) >= 0 &&
static_cast<underlying_type>(e) < std::numeric_limits<underlying_type>::digits);
return static_cast<underlying_type>(1) << static_cast<underlying_type>(e);
}
public:
constexpr Bitmask() : mask_(0) {}
constexpr Bitmask(const enum_type e) : mask_(to_mask(e)) {}
explicit constexpr Bitmask(const underlying_type mask) : mask_(mask) {}
Bitmask(const Bitmask &rhs) = default;
Bitmask(Bitmask &&rhs) noexcept = default;
Bitmask &operator=(const Bitmask &rhs) = default;
Bitmask &operator=(Bitmask &&rhs) noexcept = default;
Bitmask operator|(const Bitmask rhs) const { return Bitmask(mask_ | rhs.mask_); }
Bitmask &operator|=(const Bitmask rhs) {
mask_ |= rhs.mask_;
return *this;
}
Bitmask operator&(const Bitmask rhs) const { return Bitmask(mask_ & rhs.mask_); }
Bitmask &operator&=(const Bitmask rhs) {
mask_ &= rhs.mask_;
return *this;
}
Bitmask operator^(const Bitmask rhs) const { return Bitmask(mask_ ^ rhs.mask_); }
Bitmask &operator^=(const Bitmask rhs) {
mask_ ^= rhs.mask_;
return *this;
}
Bitmask operator~() const { return Bitmask(~mask_); }
bool operator==(const enum_type rhs) const { return mask_ == to_mask(rhs); }
bool operator==(const Bitmask rhs) const { return mask_ == rhs.mask_; }
bool operator!=(const enum_type rhs) const { return mask_ != to_mask(rhs); }
bool operator!=(const Bitmask rhs) const { return mask_ != rhs.mask_; }
operator bool() const { return mask_ != 0; }
explicit constexpr operator underlying_type() const { return mask_; }
private:
underlying_type mask_;
};
} // namespace Ray