Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a flag_set out of an integral value #161

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/type_safe/flag_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ namespace detail
return flag_set_impl(int_type(0));
}

static constexpr flag_set_impl from_int(int_type intVal)
{
return flag_set_impl(int_type(intVal));
}

explicit constexpr flag_set_impl(const Enum& e) : bits_(mask(e)) {}
template <typename Tag2>
explicit constexpr flag_set_impl(const flag_set_impl<Enum, Tag2>& other)
Expand Down Expand Up @@ -416,6 +421,17 @@ class flag_set
static_assert(flag_set_traits<Enum>::value, "invalid enum for flag_set");

public:
using int_type = typename detail::flag_set_impl<Enum>::int_type;

/// \returns a flag_set based on the given integer value.
/// \requires `T` must be of the same type as `int_type`.
template <typename T>
static constexpr flag_set from_int(T intVal)
{
static_assert(std::is_same<T, int_type>::value, "invalid integer type, lossy conversion");
return flag_set(intVal);
}

//=== constructors/assignment ===//
/// \effects Creates a set where all flags are set to `0`.
/// \group ctor_null
Expand Down Expand Up @@ -609,6 +625,9 @@ class flag_set
}

private:
explicit constexpr flag_set(int_type rawvalue) noexcept : flags_(detail::flag_set_impl<Enum>::from_int(rawvalue))
{}

detail::flag_set_impl<Enum> flags_;

friend detail::get_flag_set_impl;
Expand Down
17 changes: 17 additions & 0 deletions test/flag_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ TEST_CASE("flag_set")
b = test_flags::c;
check_set(b, false, false, true);
}
SECTION("from_int")
{
set a = set::from_int<std::uint8_t>(0b000);
check_set(a, false, false, false);

a = set::from_int<std::uint8_t>(0b001);
check_set(a, true, false, false);

a = set::from_int<std::uint8_t>(0b010);
check_set(a, false, true, false);

a = set::from_int<std::uint8_t>(0b100);
check_set(a, false, false, true);

a = set::from_int<std::uint8_t>(0b111);
check_set(a, true, true, true);
}
SECTION("set")
{
s.set(test_flags::a);
Expand Down