https://github.com/zhuagenborn/scoped-enum-flags
🚩 A C++23 header-only type-safe bit flag manager for C++ scoped enumerations.(使用C++23开发的基于强类型枚举的标志位操作库。)
https://github.com/zhuagenborn/scoped-enum-flags
enum-flags scoped-enum
Last synced: 10 days ago
JSON representation
🚩 A C++23 header-only type-safe bit flag manager for C++ scoped enumerations.(使用C++23开发的基于强类型枚举的标志位操作库。)
- Host: GitHub
- URL: https://github.com/zhuagenborn/scoped-enum-flags
- Owner: Zhuagenborn
- License: mit
- Created: 2024-09-29T12:48:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-15T03:14:25.000Z (12 months ago)
- Last Synced: 2025-11-12T04:04:25.647Z (8 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
# Scoped Enumeration Bit Flags

[](https://cmake.org)


## Introduction
A header-only type-safe bit flag manager for *C++* scoped enumerations written in *C++23*, supporting:
- Setting and clearing individual or multiple flags.
- Checking if any or multiple flags are set.
- Combining multiple flags into a single flag.
## Unit Tests
### Prerequisites
- Install *GoogleTest*.
- Install *CMake*.
### Building
Go to the project folder and run:
```bash
mkdir -p build
cd build
cmake -DENUM_FLAGS_BUILD_TESTS=ON ..
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.