Skip to content

Commit

Permalink
✅ Add error for a message with duplicate field names
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeno committed Dec 11, 2023
1 parent 41a60da commit 7b86c90
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ add_versioned_package(
OPTIONS
"FMT_INSTALL OFF"
"FMT_OS OFF")
add_versioned_package("gh:intel/cpp-std-extensions#698200c")
add_versioned_package("gh:intel/cpp-std-extensions#495e387")
add_versioned_package("gh:intel/cpp-baremetal-concurrency#8d49b6d")

add_library(cib INTERFACE)
Expand Down
12 changes: 11 additions & 1 deletion include/msg/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <stdx/tuple.hpp>
#include <stdx/tuple_algorithms.hpp>

#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/list.hpp>
#include <boost/mp11/set.hpp>

#include <algorithm>
#include <cstdint>
#include <iterator>
Expand Down Expand Up @@ -93,8 +97,9 @@ template <typename... Fields> struct storage_size {
std::max({std::size_t{}, Fields::template extent_in<T>()...});
};

template <typename F> using name_for = typename F::name_t;

template <stdx::ct_string Name, typename... Fields> class message_access {
template <typename F> using name_for = typename F::name_t;
using FieldsTuple =
decltype(stdx::make_indexed_tuple<name_for>(Fields{}...));

Expand Down Expand Up @@ -201,6 +206,11 @@ template <stdx::ct_string Name, typename... Fields> struct message {
using default_span_t = typename access_t::default_span_t;
using default_const_span_t = typename access_t::default_const_span_t;

static_assert(
boost::mp11::mp_is_set<boost::mp11::mp_transform<
detail::name_for, boost::mp11::mp_list<Fields...>>>::value,
"Message contains fields with duplicate names");

template <typename T> struct base {
constexpr auto as_derived() const -> T const & {
return static_cast<T const &>(*this);
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ add_compile_fail_test(msg/fail/impossible_match_field.cpp LIBRARIES warnings
cib)
add_compile_fail_test(msg/fail/field_location.cpp LIBRARIES warnings cib)
add_compile_fail_test(msg/fail/field_size.cpp LIBRARIES warnings cib)
add_compile_fail_test(msg/fail/message_dup_fieldnames.cpp LIBRARIES warnings
cib)
16 changes: 16 additions & 0 deletions test/msg/fail/message_dup_fieldnames.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <msg/field.hpp>
#include <msg/message.hpp>

// EXPECT: Message contains fields with duplicate names
namespace {
using namespace msg;

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

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

auto main() -> int { [[maybe_unused]] msg_defn m{}; }

0 comments on commit 7b86c90

Please sign in to comment.