-
Notifications
You must be signed in to change notification settings - Fork 710
/
flags.hh
78 lines (61 loc) · 1.84 KB
/
flags.hh
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef flags_hh_INCLUDED
#define flags_hh_INCLUDED
#include <type_traits>
#include "meta.hh"
namespace Kakoune
{
template<typename Flags>
constexpr bool with_bit_ops(Meta::Type<Flags>) { return false; }
template<typename Flags>
concept WithBitOps = with_bit_ops(Meta::Type<Flags>{});
template<typename Flags>
using UnderlyingType = std::underlying_type_t<Flags>;
template<WithBitOps Flags>
constexpr Flags operator|(Flags lhs, Flags rhs)
{
return (Flags)((UnderlyingType<Flags>) lhs | (UnderlyingType<Flags>) rhs);
}
template<WithBitOps Flags>
constexpr Flags& operator|=(Flags& lhs, Flags rhs)
{
(UnderlyingType<Flags>&) lhs |= (UnderlyingType<Flags>) rhs;
return lhs;
}
template<typename Flags>
struct TestableFlags
{
Flags value;
constexpr operator bool() const { return (UnderlyingType<Flags>)value; }
constexpr operator Flags() const { return value; }
constexpr operator UnderlyingType<Flags>() const { return (UnderlyingType<Flags>)value; }
constexpr bool operator==(const TestableFlags<Flags>& other) const = default;
};
template<WithBitOps Flags>
constexpr TestableFlags<Flags> operator&(Flags lhs, Flags rhs)
{
return { (Flags)((UnderlyingType<Flags>) lhs & (UnderlyingType<Flags>) rhs) };
}
template<WithBitOps Flags>
constexpr Flags& operator&=(Flags& lhs, Flags rhs)
{
(UnderlyingType<Flags>&) lhs &= (UnderlyingType<Flags>) rhs;
return lhs;
}
template<WithBitOps Flags>
constexpr Flags operator~(Flags lhs)
{
return (Flags)(~(UnderlyingType<Flags>)lhs);
}
template<WithBitOps Flags>
constexpr Flags operator^(Flags lhs, Flags rhs)
{
return (Flags)((UnderlyingType<Flags>) lhs ^ (UnderlyingType<Flags>) rhs);
}
template<WithBitOps Flags>
constexpr Flags& operator^=(Flags& lhs, Flags rhs)
{
(UnderlyingType<Flags>&) lhs ^= (UnderlyingType<Flags>) rhs;
return lhs;
}
}
#endif // flags_hh_INCLUDED