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
5 changes: 5 additions & 0 deletions include/msg/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ class field_t : public field_spec_t<Name, T, detail::field_size<Ats...>>,
using with_new_type =
field_t<Name, U, detail::with_default<U>, match::always_t, Ats...>;

// ======================================================================
// rename field
template <stdx::ct_string NewName>
using with_new_name = field_t<NewName, T, Default, M, Ats...>;

// ======================================================================
// shift a field
template <auto N, typename Unit = bit_unit>
Expand Down
34 changes: 34 additions & 0 deletions include/msg/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ struct message {
using num_fields_t = std::integral_constant<std::size_t, sizeof...(Fields)>;
template <std::size_t I> using nth_field_t = stdx::nth_t<I, Fields...>;

using name_t = stdx::cts_t<Name>;
using env_t = Env;
using access_t = msg_access<Name, Fields...>;
using default_storage_t = typename access_t::default_storage_t;
Expand Down Expand Up @@ -789,4 +790,37 @@ struct field_locator<Name, Env, Fields...> {

template <stdx::ct_string Name, typename... Ts>
using relaxed_message = typename detail::field_locator<Name, Ts...>::msg_type;

namespace detail {
template <typename Msg> struct replace_fields_q {
template <typename... Fs>
using fn = ::msg::message<Msg::name_t::value, typename Msg::env_t, Fs...>;
};

template <typename Msg, stdx::ct_string OldName, stdx::ct_string NewName>
struct with_renamed_field {
using split_fields = boost::mp11::mp_partition_q<typename Msg::fields_t,
matching_name<OldName>>;

using untouched_fs = boost::mp11::mp_second<split_fields>;
static_assert(boost::mp11::mp_count_if_q<untouched_fs,
matching_name<NewName>>::value ==
0,
"rename_field: New field name already exists in message");

using replaced_fs = boost::mp11::mp_first<split_fields>;
static_assert(not boost::mp11::mp_empty<replaced_fs>::value,
"rename_field: Old field name not found in message");

using new_f = typename boost::mp11::mp_first<
replaced_fs>::template with_new_name<NewName>;

using new_fields = boost::mp11::mp_push_back<untouched_fs, new_f>;
using msg = boost::mp11::mp_apply_q<replace_fields_q<Msg>, new_fields>;
};
} // namespace detail

template <typename Msg, stdx::ct_string OldName, stdx::ct_string NewName>
using rename_field =
typename detail::with_renamed_field<Msg, OldName, NewName>::msg;
} // namespace msg
2 changes: 2 additions & 0 deletions test/msg/fail/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ add_compile_fail_test(message_set_nonexistent_field.cpp LIBRARIES warnings
cib_msg)
add_compile_fail_test(message_uninitialized_field.cpp LIBRARIES warnings
cib_msg)
add_compile_fail_test(rename_field_duplicate.cpp LIBRARIES warnings cib_msg)
add_compile_fail_test(rename_field_not_found.cpp LIBRARIES warnings cib_msg)
add_compile_fail_test(view_upsize.cpp LIBRARIES warnings cib_msg)
15 changes: 15 additions & 0 deletions test/msg/fail/rename_field_duplicate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <msg/message.hpp>

// EXPECT: New field name already exists in message
namespace {
using namespace msg;

using test_field1 =
field<"f1", std::uint32_t>::located<at{0_dw, 31_msb, 24_lsb}>;
using test_field2 =
field<"f2", std::uint32_t>::located<at{0_dw, 23_msb, 16_lsb}>;

using msg_defn = message<"test_msg", test_field1, test_field2>;
} // namespace

auto main() -> int { using M = rename_field<msg_defn, "f1", "f2">; }
13 changes: 13 additions & 0 deletions test/msg/fail/rename_field_not_found.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <msg/message.hpp>

// EXPECT: Old field name not found in message
namespace {
using namespace msg;

using test_field1 =
field<"f1", std::uint32_t>::located<at{0_dw, 31_msb, 24_lsb}>;

using msg_defn = message<"test_msg", test_field1>;
} // namespace

auto main() -> int { using M = rename_field<msg_defn, "f", "F">; }
9 changes: 9 additions & 0 deletions test/msg/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ TEST_CASE("extend message with retyped field", "[message]") {
STATIC_REQUIRE(std::is_same_v<defn, expected_defn>);
}

TEST_CASE("rename field in message", "[message]") {
using base_defn = message<"msg_base", id_field, field1>;
using defn = rename_field<base_defn, "f1", "f1_new">;
using expected_f =
field<"f1_new", std::uint32_t>::located<at{0_dw, 15_msb, 0_lsb}>;
using expected_defn = message<"msg_base", id_field, expected_f>;
STATIC_REQUIRE(std::is_same_v<defn, expected_defn>);
}

TEST_CASE("message equivalence (owning)", "[message]") {
test_msg m1{"f1"_field = 0xba11, "f2"_field = 0x42, "f3"_field = 0xd00d};
test_msg m2{"f1"_field = 0xba11, "f2"_field = 0x42, "f3"_field = 0xd00d};
Expand Down
Loading