Skip to content

Commit

Permalink
Update README and add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
uyha committed Jun 1, 2020
1 parent 0db8c4f commit 1055790
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
48 changes: 47 additions & 1 deletion README.md
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.
7 changes: 5 additions & 2 deletions examples/file_permission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ 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_read | FilePermission::user_write;
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);
Expand Down

0 comments on commit 1055790

Please sign in to comment.