|
3 | 3 |
|
4 | 4 | namespace flg
|
5 | 5 | {
|
6 |
| -template <typename numeric_type, typename tag> |
7 |
| -struct flag |
8 |
| -{ |
9 |
| - |
10 |
| -struct type |
| 6 | +template <typename numeric_type, typename user_type> |
| 7 | +struct flags |
11 | 8 | {
|
12 | 9 | using value_type = numeric_type;
|
13 | 10 |
|
14 |
| - explicit operator value_type() const { return value; } |
15 |
| - operator bool() const { return value; } |
| 11 | + explicit constexpr operator value_type() const { return value; } |
| 12 | + constexpr operator bool() const { return value; } |
16 | 13 |
|
17 |
| - type operator~() const |
| 14 | + constexpr flags operator~() const |
18 | 15 | {
|
19 |
| - return type{~value}; |
| 16 | + return flags{~value}; |
20 | 17 | }
|
21 |
| - type operator&(type other) const |
| 18 | + constexpr flags operator&(flags other) const |
22 | 19 | {
|
23 |
| - return type{value & other.value}; |
| 20 | + return flags{value & other.value}; |
24 | 21 | }
|
25 |
| - type operator|(type other) const |
| 22 | + constexpr flags operator|(flags other) const |
26 | 23 | {
|
27 |
| - return type{value | other.value}; |
| 24 | + return flags{value | other.value}; |
28 | 25 | }
|
29 |
| - type operator^(type other) const |
| 26 | + constexpr flags operator^(flags other) const |
30 | 27 | {
|
31 |
| - return type{value ^ other.value}; |
| 28 | + return flags{value ^ other.value}; |
32 | 29 | }
|
33 |
| - type& operator&=(type other) |
| 30 | + constexpr flags& operator&=(flags other) |
34 | 31 | {
|
35 | 32 | value &= other.value;
|
36 | 33 | return *this;
|
37 | 34 | }
|
38 |
| - type& operator|=(type other) |
| 35 | + constexpr flags& operator|=(flags other) |
39 | 36 | {
|
40 | 37 | value |= other.value;
|
41 | 38 | return *this;
|
42 | 39 | }
|
43 |
| - type& operator^=(type other) |
| 40 | + constexpr flags& operator^=(flags other) |
44 | 41 | {
|
45 | 42 | value ^= other.value;
|
46 | 43 | return *this;
|
47 | 44 | }
|
48 |
| - bool operator==(type other) const |
| 45 | + constexpr bool operator==(flags other) const |
49 | 46 | {
|
50 | 47 | return value == other.value;
|
51 | 48 | }
|
52 |
| - bool operator!=(type other) const |
| 49 | + constexpr bool operator!=(flags other) const |
53 | 50 | {
|
54 | 51 | return value != other.value;
|
55 | 52 | }
|
56 | 53 |
|
57 | 54 | value_type value;
|
58 | 55 |
|
59 |
| - constexpr static type none_flag() |
| 56 | + struct flag |
| 57 | + { |
| 58 | + value_type value; |
| 59 | + |
| 60 | + constexpr operator user_type() const |
| 61 | + { |
| 62 | + return user_type{value}; |
| 63 | + } |
| 64 | + constexpr user_type operator~() const |
| 65 | + { |
| 66 | + return user_type{~value}; |
| 67 | + } |
| 68 | + constexpr user_type operator&(flag other) const |
| 69 | + { |
| 70 | + return user_type{value & other.value}; |
| 71 | + } |
| 72 | + constexpr user_type operator|(flag other) const |
| 73 | + { |
| 74 | + return user_type{value | other.value}; |
| 75 | + } |
| 76 | + constexpr user_type operator^(flag other) const |
| 77 | + { |
| 78 | + return user_type{value ^ other.value}; |
| 79 | + } |
| 80 | + constexpr bool operator==(flag other) const |
| 81 | + { |
| 82 | + return value == other.value; |
| 83 | + } |
| 84 | + constexpr bool operator!=(flag other) const |
| 85 | + { |
| 86 | + return value != other.value; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + constexpr static flag none_flag() |
60 | 91 | {
|
61 | 92 | return {0};
|
62 | 93 | }
|
63 |
| - |
64 |
| - constexpr static type from_index(value_type v) |
| 94 | + |
| 95 | + constexpr static flag from_index(value_type v) |
65 | 96 | {
|
66 | 97 | return {1 << v};
|
67 | 98 | }
|
68 | 99 | };
|
69 |
| -}; |
70 | 100 | }
|
0 commit comments