Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhuagenborn/enum-flags
🚩 A header-only type-safe bit flags for C++ scoped enumerations written in C++23.
https://github.com/zhuagenborn/enum-flags
enum-flags scoped-enum
Last synced: about 1 month ago
JSON representation
🚩 A header-only type-safe bit flags for C++ scoped enumerations written in C++23.
- Host: GitHub
- URL: https://github.com/zhuagenborn/enum-flags
- Owner: Zhuagenborn
- License: mit
- Created: 2024-09-29T12:48:48.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-17T07:17:24.000Z (2 months ago)
- Last Synced: 2024-10-19T09:59:03.974Z (2 months ago)
- Topics: enum-flags, scoped-enum
- Language: C++
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# *C++* Enumeration Bit Flags
![C++](docs/badges/C++.svg)
[![CMake](docs/badges/Made-with-CMake.svg)](https://cmake.org)
![GitHub Actions](docs/badges/Made-with-GitHub-Actions.svg)
![License](docs/badges/License-MIT.svg)## Introduction
A header-only type-safe bit flag manager for *C++* scoped enumerations written in *C++23*.
## Unit Tests
### Prerequisites
- Install *GoogleTest*.
- Install *CMake*.### Building
Go to the project folder and run:
```bash
mkdir -p build
cd build
cmake ..
cmake --build .
```### Running
Go to the `build` folder and run:
```bash
ctest -VV
```## Examples
See more examples in `tests/enum_flags_tests.cpp`.
```c++
enum class Opt : unsigned int {
A = EnumFlags::CreateFlag(0),
B = EnumFlags::CreateFlag(1),
C = EnumFlags::CreateFlag(2),
D = EnumFlags::CreateFlag(3),
E = EnumFlags::CreateFlag(4)
};
``````c++
const EnumFlags flags {Opt::A, Opt::B, Opt::C};
EXPECT_TRUE(flags & Opt::A);
EXPECT_FALSE(flags & Opt::D);EXPECT_TRUE(flags.HasAny({Opt::A, Opt::D}));
EXPECT_FALSE(flags.HasAny({Opt::D, Opt::E}));EXPECT_TRUE(flags.HasAll({Opt::A, Opt::B}));
EXPECT_TRUE((flags & EnumFlags {Opt::A, Opt::B}));
EXPECT_FALSE(flags.HasAll({Opt::C, Opt::D}));
EXPECT_FALSE((flags & EnumFlags {Opt::C, Opt::D}));
``````c++
EnumFlags flags;
EXPECT_FALSE(flags & Opt::A);
flags |= Opt::A;
EXPECT_TRUE(flags & Opt::A);const auto new_flags {flags | Opt::D};
EXPECT_TRUE(new_flags & Opt::D);
``````c++
EnumFlags flags {Opt::A, Opt::B, Opt::C};
EXPECT_TRUE(flags & Opt::C);
flags.Remove(Opt::C);
EXPECT_FALSE(flags & Opt::C);
```## License
Distributed under the *MIT License*. See `LICENSE` for more information.