-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBitmask.h
44 lines (29 loc) · 1.57 KB
/
Bitmask.h
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
#pragma once
#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 underlying_type to_mask(const enum_type e) { return 1 << static_cast<underlying_type>(e); }
explicit Bitmask(const underlying_type mask) : mask_(mask) {}
public:
Bitmask() : mask_(0) {}
Bitmask(const enum_type e) : mask_(to_mask(e)) {}
Bitmask(const Bitmask &rhs) = default;
Bitmask(Bitmask &&rhs) = default;
Bitmask &operator=(const Bitmask &rhs) = default;
Bitmask &operator=(Bitmask &&rhs) = default;
Bitmask operator|(const Bitmask rhs) const { return Bitmask(mask_ | rhs.mask_); }
Bitmask operator|=(const Bitmask rhs) { return (*this) = Bitmask(mask_ | rhs.mask_); }
Bitmask operator&(const Bitmask rhs) const { return Bitmask(mask_ & rhs.mask_); }
Bitmask operator&=(const Bitmask rhs) { return (*this) = Bitmask(mask_ & rhs.mask_); }
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 operator underlying_type() const { return mask_; }
private:
underlying_type mask_;
};
} // namespace Ray