Skip to content
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
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));
}