Skip to content

Commit

Permalink
Create a flag_set out of an integral value (#161)
Browse files Browse the repository at this point in the history
Fixes #160.
  • Loading branch information
meikjaeckle authored Apr 22, 2024
1 parent ae6d075 commit fb5eba9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit fb5eba9

Please sign in to comment.