Skip to content

Commit

Permalink
Merge pull request #603 from elbeno/field-fitting
Browse files Browse the repository at this point in the history
✨ Add `can_hold` to `field`
  • Loading branch information
bdeane-intel authored Aug 27, 2024
2 parents 924cab6 + 066e932 commit 0c464e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/msg/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ template <bits_locator... BLs> struct field_locator_t {
template <typename T> constexpr static auto extent_in() -> std::size_t {
return std::max({std::size_t{}, BLs::template extent_in<T>()...});
}

constexpr static auto size = (std::size_t{} + ... + BLs::size);
};
} // namespace detail

Expand Down Expand Up @@ -451,6 +453,10 @@ class field_t : public field_spec_t<Name, T, detail::field_size<Ats...>>,
return format("{}: 0x{:x}"_sc, spec_t::name, v);
}

constexpr static auto can_hold(value_type v) -> bool {
return locator_t::size >= static_cast<std::size_t>(stdx::bit_width(v));
}

// ======================================================================
// default construction values
template <T V>
Expand Down
15 changes: 15 additions & 0 deletions test/msg/field_insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,18 @@ TEST_CASE("trivially_copyable field type ", "[field insert]") {
F::insert(data, custom_t{17});
CHECK(17 == stdx::bit_cast<custom_t>(data[0]).v);
}

TEST_CASE("value fits in field", "[field insert]") {
using F = field<"", std::uint32_t>::located<at{0_dw, 3_msb, 0_lsb}>;
static_assert(F::can_hold(15));
CHECK(F::can_hold(15));
CHECK(not F::can_hold(16));
}

TEST_CASE("value fits in split field", "[field insert]") {
using F = field<"", std::uint32_t>::located<at{0_dw, 1_msb, 0_lsb},
at{0_dw, 7_msb, 6_lsb}>;
static_assert(F::can_hold(15));
CHECK(F::can_hold(15));
CHECK(not F::can_hold(16));
}

0 comments on commit 0c464e5

Please sign in to comment.