-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,47 @@ | ||
# A utility library for using bitwise operators on flag enums | ||
# A C++20 library for using bitwise operators on flag enums | ||
|
||
This library provides a set of free function templates which perform bitwise opertions on scoped enums that satisfy the `Flag` concept. When these function templates are used, instead of return `int` like normal bitwise operators, they return the underlying type of the input enum. | ||
|
||
## Examples | ||
|
||
```cpp | ||
#include <cassert> | ||
#include <cstdint> | ||
#include <flag.hpp> | ||
|
||
enum class FilePermission : std::uint16_t { | ||
all_execute = 0b0'000'000'001, | ||
all_write = 0b0'000'000'010, | ||
all_read = 0b0'000'000'100, | ||
|
||
group_execute = 0b0'000'001'000, | ||
group_write = 0b0'000'010'000, | ||
group_read = 0b0'000'100'000, | ||
|
||
user_execute = 0b0'001'000'000, | ||
user_write = 0b0'010'000'000, | ||
user_read = 0b0'100'000'000, | ||
|
||
directory = 0b1'000'000'000 | ||
}; | ||
|
||
int main() { | ||
using namespace river; // All operators are define in the river namespace | ||
|
||
// type of file_permission is std::uint16_t | ||
auto const file_permission = FilePermission::all_execute | FilePermission::all_write | | ||
FilePermission::all_read | FilePermission::group_execute | | ||
FilePermission::group_write | FilePermission::group_read | | ||
FilePermission::user_execute | FilePermission::user_write | | ||
FilePermission::user_read | FilePermission::directory; | ||
|
||
auto const all_execute = has<FilePermission::all_execute>(file_permission); | ||
auto const user_write = has<FilePermission::user_write>(file_permission); | ||
|
||
assert(all_execute); | ||
assert(user_write); | ||
} | ||
``` | ||
## Usage | ||
This is a header only library, copy the file `flag.hpp` to your source code and start using it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters