add bitflags examples to readme

This commit is contained in:
BlackMATov
2020-12-19 03:21:43 +07:00
parent 19979a728c
commit 70f71d0106
2 changed files with 137 additions and 4 deletions

View File

@@ -0,0 +1,49 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/enum.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enum.hpp/enum_bitflags.hpp>
#include "doctest/doctest.hpp"
#include <string>
#include <iostream>
namespace
{
enum class perms : unsigned {
execute = 1 << 0,
write = 1 << 1,
read = 1 << 2,
};
ENUM_HPP_OPERATORS_DECL(perms)
}
TEST_CASE("bitflags_examples") {
SUBCASE("Enum operators using") {
namespace bf = enum_hpp::bitflags;
bf::bitflags<perms> flags = perms::read | perms::write;
if ( flags.has(perms::write) ) {
flags.clear(perms::write);
}
flags.set(perms::write | perms::execute);
if ( flags & perms::execute ) {
flags ^= perms::execute; // flags.toggle(perms::execute);
}
CHECK(flags == (perms::read | perms::write));
}
SUBCASE("Additional bitflags functions") {
namespace bf = enum_hpp::bitflags;
bf::bitflags<perms> flags = perms::read | perms::write;
CHECK(bf::any_of(flags, perms::write | perms::execute));
CHECK(bf::any_except(flags, perms::write | perms::execute));
}
}