https://github.com/mrts/flag-set-cpp
flag_set is a type-safe class for using enums as flags in C++
https://github.com/mrts/flag-set-cpp
cpp enums
Last synced: 11 months ago
JSON representation
flag_set is a type-safe class for using enums as flags in C++
- Host: GitHub
- URL: https://github.com/mrts/flag-set-cpp
- Owner: mrts
- License: mit
- Created: 2019-10-04T10:35:22.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-19T18:37:10.000Z (about 6 years ago)
- Last Synced: 2025-03-30T14:02:41.834Z (about 1 year ago)
- Topics: cpp, enums
- Language: C++
- Homepage:
- Size: 15.6 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flag-set-cpp
`flag_set` is a type-safe class for using enums as flags in C++14 with an underlying `std::bitset`.
The original idea is by [Arnaud Kapp (Xaqq)](https://codereview.stackexchange.com/questions/96146/c-flagset-typesafe-usage-of-enumeration-as-bitset-bitmask)
with additions from
[Barry Revzin](https://codereview.stackexchange.com/users/31292/barry).
## Usage
`flag_set` is a header-only library, simply copy `include/flag_set.hpp` to your
project to use it.
The `enum` type of the `flag_set` **must have** a last value sentinel `_`,
a single underscore character. Underscore was chosen as it stands out clearly and is
unlikely to collide with a real enum value.
Usage example:
```c++
#include "flag_set.hpp"
enum class Options : uint8_t {
FULLSCREEN,
INVERT_MOUSE,
FLASH,
RED_BACKGROUND,
RED_FOREGROUND,
_
};
int main()
{
flag_set red(Options::RED_FOREGROUND | Options::RED_BACKGROUND);
if (red[Options::RED_BACKGROUND]) // or red & Options::RED_BACKGROUND
cout << "Red background activated";
}
```
See more examples in [tests](tests/src/flag-set-tests.cpp).
## Contributing
Run `clang-format` on patches as follows:
```sh
find include/ tests/ -iname '*.hpp' -o -iname '*.h' -o -iname '*.cpp' | xargs clang-format -i
```