Skip to content

Commit

Permalink
Update examples and README
Browse files Browse the repository at this point in the history
  • Loading branch information
uyha committed Jun 7, 2020
1 parent 3fdc660 commit 504dad6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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.
This library provides a set of free function templates which perform bitwise operations 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

Expand Down Expand Up @@ -30,17 +30,20 @@ int main() {
using namespace river::flags; // 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 file_permission = FilePermission::all_execute | FilePermission::user_execute;

auto const all_execute = has<FilePermission::all_execute>(file_permission);
auto const user_write = has<FilePermission::user_write>(file_permission);
assert(has<FilePermission::all_execute>(file_permission));
assert(has<FilePermission::user_execute>(file_permission));

assert(all_execute);
assert(user_write);
// Add group_execute to file_permission
file_permission |= FilePermission::group_execute;
assert(has<FilePermission::group_execute>(file_permission));

// Toggle user_execute bit of file_permission
file_permission ^= FilePermission::user_execute;
assert(!has<FilePermission::user_execute>(file_permission));
file_permission ^= FilePermission::user_execute;
assert(has<FilePermission::user_execute>(file_permission));
}
```
Expand Down
21 changes: 12 additions & 9 deletions examples/file_permission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ int main() {
using namespace river::flags; // 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 file_permission = FilePermission::all_execute | FilePermission::user_execute;

auto const all_execute = has<FilePermission::all_execute>(file_permission);
auto const user_write = has<FilePermission::user_write>(file_permission);
assert(has<FilePermission::all_execute>(file_permission));
assert(has<FilePermission::user_execute>(file_permission));

assert(all_execute);
assert(user_write);
// Add group_execute to file_permission
file_permission |= FilePermission::group_execute;
assert(has<FilePermission::group_execute>(file_permission));

// Toggle user_execute bit of file_permission
file_permission ^= FilePermission::user_execute;
assert(!has<FilePermission::user_execute>(file_permission));
file_permission ^= FilePermission::user_execute;
assert(has<FilePermission::user_execute>(file_permission));
}

0 comments on commit 504dad6

Please sign in to comment.