@@ -10,119 +10,25 @@ The only drawback to type safety is you cannot treat variables of *enum class*
1010types as sets of flags. That is because * enum classes* do not cast to integers
1111and there are no bitwise operators overloads defined for them.
1212
13- This library brings a ` flags ` class template which:
14- * does not convert to or from integer types;
15- * does implicitly convert from enum it was instantiated by;
16- * does not convert to that enum;
17- * supports all bitwise operations except for shifts (since shifts are not very
18- reasonable for flags);
19- * explicitly converts to bool (which allows to use it in boolean contexts, like
20- in branching or loop conditions);
21- * provides access to the underlying integer through member functions;
22- * instantiates only for enums it was explicitly enabled for.
13+ This library brings a ` flags ` class template which provides bit flags for
14+ scoped enums.
2315
24- ## Usage
25- ``` c++
26- enum class MyEnum { Value1 = 1 << 0, Value2 = 1 << 1 };
16+ ----------
2717
28- namespace flags {
29- template <> struct is_flags<MyEnum > : std::true_type {};
30- }
31- // or just use macro:
32- // ALLOW_FLAGS_FOR_ENUM(MyEnum)
18+ Simple usage:
3319
34- using MyEnums = flags::flags<MyEnum >;
20+ ``` c++
21+ enum class MyEnum { Value1 = 1 << 0, Value2 = 1 << 1 };
22+ ALLOW_FLAGS_FOR_ENUM(MyEnum)
3523
36- MyEnums mask1 = MyEnum::Value1 | MyEnum::Value2; // set flags Value1 and Value 2
37- if (mask1 & MyEnum::Value2) { // if Value2 flag is set
38- doSomething();
24+ int main() {
25+ auto mask1 = MyEnum::Value1 | MyEnum::Value2; // set flags Value1 and Value 2
26+ if (mask1 & MyEnum::Value2) { // if Value2 flag is set
27+ /* ... * /
28+ }
3929}
4030```
4131
42- ## Description
43-
44- **Disclaimer: docs are currently out of sync a little bit**
45-
46- ``` c++
47- template <class E> class flags;
48- ```
49- where ` E ` is an enum.
50-
51- ### Member types
52- Member type |Definition |Notes
53- ---------------|----------------------------------------------------------|---------------------------------------------
54- enum_type |The template parameter (E) |
55- underlying_type|The integer type that is used as representation of ` flags ` |Equivalent of ` std::underlying_type<E>::type `
56-
57- ### Member functions
58-
59- #### Constructors and assignment operators
60- Name |Description
61- -----------------------------|-----------
62- ` flags() ` |Default contructor, keeps object uninitialized (note: object may contain garbage)
63- ` flags(empty_t) ` |Unsets all flags
64- ` flags(flags::enum_type) ` |Sets flag denoted by the parameter
65- ` flags(const flags&) ` |Copy constructor
66- ` flags(flags&&) ` |Move constructor
67- ` operator=(flags::enum_type) ` |Unsets all flags, then sets flag denoted by the parameter
68- ` operator=(const flags&) ` |Copy assignment
69- ` operator=(flags&&) ` |Move assignment
70-
71- #### Bitwise operators
72- All functions in this group return reference to ` flags ` , except for ` operator~ `
73- which returns a new ` flags ` value.
74-
75- Name |Description
76- ----------------------------------------------|-----------
77- <code>operator| ; =(flags::enum_type)</code >|Bitwise OR with flag denoted by the parameter
78- <code>operator| ; =(const flags&)</code > |Bitwise OR with flags in the parameter
79- ` operator&=(flags::enum_type) ` |Bitwise AND with flag denoted by the parameter`
80- ` operator&=(const flags&) ` |Bitwise AND with flags in the parameter
81- ` operator^=(flags::enum_type) ` |Bitwise XOR with flag denoted by the parameter
82- ` operator^=(const flags&) ` |Bitwise XOR with flags in the parameter
83- ` operator~() ` |Bitwise negation
84-
85- #### Boolean conversions
86- Name |Description
87- -----------------|-----------
88- ` operator bool() ` |Returns ` true ` if any of the flags is set
89- ` operator!() ` |Returns ` true ` if none of the flags is set
90-
91- #### Raw access to integer representation
92- Name |Description
93- ----------------------------------------------|-----------
94- ` underlying_value() ` |Returns integer representation
95- ` set_underlying_value(flags::underlying_type) ` |Sets integer representation to new value
96-
97- #### Various functions
98- Name |Description
99- -------------|-----------
100- ` swap(flags) ` |Swaps two ` flags ` values
101-
102- ### Nonmember functions
103-
104- #### Bitwise operators
105- All functions in this group return new ` flags ` value.
106-
107- Name |Description
108- ---------------------------------------------------------------|-----------
109- <code>operator| ; (flags, flags)</code > |Bitwise OR of two ` flags ` values
110- <code>operator| ; (flags, flags::enum_type)</code > |Bitwise OR of a ` flags ` value and a flag denoted by an enum value
111- <code>operator| ; (flags::enum_type, flags)</code > |Bitwise OR of a ` flags ` value and a flag denoted by an enum value
112- <code>operator| ; (flags::enum_type, flags::enum_type)</code >|Bitwise OR of two flags denoted by enum values
113- ` operator&(flags, flags) ` |Bitwise AND of two ` flags ` values
114- ` operator&(flags, flags::enum_type) ` |Bitwise AND of a ` flags ` value and a flag denoted by an enum value
115- ` operator&(flags::enum_type, flags) ` |Bitwise AND of a ` flags ` value and a flag denoted by an enum value
116- ` operator&(flags::enum_type, flags::enum_type) ` |Bitwise AND of two flags denoted by enum values
117- ` operator^(flags, flags) ` |Bitwise XOR of two ` flags ` values
118- ` operator^(flags, flags::enum_type) ` |Bitwise XOR of a ` flags ` value and a flag denoted by an enum value
119- ` operator^(flags::enum_type, flags) ` |Bitwise XOR of a ` flags ` value and a flag denoted by an enum value
120- ` operator^(flags::enum_type, flags::enum_type) ` |Bitwise XOR of two flags denoted by enum values
121-
122- #### Comparison operators
123- All functions in this group return ` bool ` .
32+ More info can be found in the [docs][DOC].
12433
125- Name |Description
126- --------------------------|-----------
127- ` operator==(flags, flags) ` |Compares if two ` flags ` values are equal
128- ` operator!=(flags, flags) ` |Compares if two ` flags ` values are not equal
34+ [DOC]: http://grisumbras.github.io/enum-flags/
0 commit comments