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
8 changes: 5 additions & 3 deletions include/msg/detail/indexed_builder_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ struct indexed_builder_base {
ExtraCallbackArgsT...>{new_callbacks};
}

using callback_func_t = void (*)(BaseMsgT const &,
ExtraCallbackArgsT... args);
using callback_func_t = auto (*)(BaseMsgT const &,
ExtraCallbackArgsT... args) -> bool;

template <typename BuilderValue, std::size_t I>
constexpr static auto invoke_callback(BaseMsgT const &data,
ExtraCallbackArgsT... args) {
ExtraCallbackArgsT... args) -> bool {
// FIXME: incomplete message callback invocation...
// 1) bit_cast message argument
constexpr auto cb = IndexSpec{}.apply([&]<typename... Indices>(
Expand All @@ -173,7 +173,9 @@ struct indexed_builder_base {
stdx::ct_string_to_type<cb.name, sc::string_constant>(),
orig_cb.matcher.describe(), cb.matcher.describe());
cb.callable(view, args...);
return true;
}
return false;
}

template <typename BuilderValue, std::size_t... Is>
Expand Down
13 changes: 4 additions & 9 deletions include/msg/detail/indexed_handler_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ constexpr callback_args_t<BaseMsgT, ExtraCallbackArgsT...> callback_args{};
template <typename IndexT, typename CallbacksT, typename BaseMsgT,
typename... ExtraCallbackArgsT>
struct indexed_handler : handler_interface<BaseMsgT, ExtraCallbackArgsT...> {
using callback_func_t = void (*)(BaseMsgT const &,
ExtraCallbackArgsT... args);

IndexT index;
CallbacksT callback_entries;

Expand All @@ -52,16 +49,14 @@ struct indexed_handler : handler_interface<BaseMsgT, ExtraCallbackArgsT...> {
ExtraCallbackArgsT... args) const -> bool final {
auto const callback_candidates = index(msg);

for_each([&](auto i) { callback_entries[i](msg, args...); },
bool handled{};
for_each([&](auto i) { handled |= callback_entries[i](msg, args...); },
callback_candidates);

bool const candidates_found = !callback_candidates.none();

if (not candidates_found) {
if (not handled) {
CIB_ERROR("None of the registered callbacks claimed this message.");
}

return candidates_found;
return handled;
}
};

Expand Down
12 changes: 6 additions & 6 deletions test/msg/indexed_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ TEST_CASE("match output success", "[handler_builder]") {
cib::nexus<test_project> test_nexus{};
test_nexus.init();

cib::service<test_service>->handle(
test_msg_t{"test_id_field"_field = 0x80});
CHECK(cib::service<test_service>->handle(
test_msg_t{"test_id_field"_field = 0x80}));
CAPTURE(log_buffer);
CHECK(log_buffer.find("Incoming message matched") != std::string::npos);
CHECK(log_buffer.find("[TestCallback]") != std::string::npos);
Expand All @@ -81,8 +81,8 @@ TEST_CASE("match output failure", "[handler_builder]") {
cib::nexus<test_project> test_nexus{};
test_nexus.init();

cib::service<test_service>->handle(
test_msg_t{"test_id_field"_field = 0x81});
CHECK(not cib::service<test_service>->handle(
test_msg_t{"test_id_field"_field = 0x81}));
CHECK(log_buffer.find(
"None of the registered callbacks claimed this message") !=
std::string::npos);
Expand Down Expand Up @@ -189,8 +189,8 @@ TEST_CASE("message matching partial index but not callback matcher",

log_buffer.clear();
callback_success = false;
cib::service<partially_indexed_test_service>->handle(test_msg_t{
"test_id_field"_field = 0x80, "test_opcode_field"_field = 2});
CHECK(not cib::service<partially_indexed_test_service>->handle(test_msg_t{
"test_id_field"_field = 0x80, "test_opcode_field"_field = 2}));
CHECK(not callback_success);
}

Expand Down
63 changes: 48 additions & 15 deletions test/msg/indexed_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ using msg_defn =
message<"test_msg", opcode_field, sub_opcode_field, field_3, field_4>;
using test_msg = owning<msg_defn>;

using callback_t = auto (*)(test_msg const &) -> bool;

template <auto N> using bitset = stdx::bitset<N, std::uint32_t>;

bitset<32> callbacks_called{};
Expand All @@ -45,7 +47,7 @@ TEST_CASE("create empty handler", "[indexed_handler]") {
msg::indices{msg::index{
opcode_field{}, lookup::make(CX_VALUE(
lookup::input<std::uint32_t, bitset<32>>{}))}},
std::array<void (*)(test_msg const &), 0>{}};
std::array<callback_t, 0>{}};
}

TEST_CASE("create handler with one index and callback", "[indexed_handler]") {
Expand All @@ -56,8 +58,10 @@ TEST_CASE("create handler with one index and callback", "[indexed_handler]") {
lookup::make(CX_VALUE(lookup::input<std::uint32_t, bitset<32>, 1>{
bitset<32>{}, std::array{lookup::entry{
42u, bitset<32>{stdx::place_bits, 0}}}}))}},
std::array<void (*)(test_msg const &), 1>{
[](test_msg const &) { callbacks_called.set(0); }}};
std::array<callback_t, 1>{[](test_msg const &) {
callbacks_called.set(0);
return true;
}}};

callbacks_called.reset();
CHECK(h.handle(test_msg{"opcode_field"_field = 42}));
Expand Down Expand Up @@ -97,16 +101,42 @@ TEST_CASE("create handler with multiple indices and callbacks",
entry{2u, bitset<32>{stdx::place_bits, 2, 6, 8}},
entry{3u, bitset<32>{stdx::place_bits, 3, 7, 8}},
}}))}},
std::array<void (*)(test_msg const &), 9>{
[](test_msg const &) { callbacks_called.set(0); },
[](test_msg const &) { callbacks_called.set(1); },
[](test_msg const &) { callbacks_called.set(2); },
[](test_msg const &) { callbacks_called.set(3); },
[](test_msg const &) { callbacks_called.set(4); },
[](test_msg const &) { callbacks_called.set(5); },
[](test_msg const &) { callbacks_called.set(6); },
[](test_msg const &) { callbacks_called.set(7); },
[](test_msg const &) { callbacks_called.set(8); }}};
std::array<callback_t, 9>{[](test_msg const &) {
callbacks_called.set(0);
return true;
},
[](test_msg const &) {
callbacks_called.set(1);
return true;
},
[](test_msg const &) {
callbacks_called.set(2);
return true;
},
[](test_msg const &) {
callbacks_called.set(3);
return true;
},
[](test_msg const &) {
callbacks_called.set(4);
return true;
},
[](test_msg const &) {
callbacks_called.set(5);
return true;
},
[](test_msg const &) {
callbacks_called.set(6);
return true;
},
[](test_msg const &) {
callbacks_called.set(7);
return true;
},
[](test_msg const &) {
callbacks_called.set(8);
return true;
}}};

auto const check_msg = [&](std::uint32_t op, std::uint32_t sub_op,
std::size_t callback_index) {
Expand Down Expand Up @@ -151,8 +181,11 @@ TEST_CASE("create handler with extra callback arg", "[indexed_handler]") {
lookup::make(CX_VALUE(lookup::input<std::uint32_t, bitset<32>, 1>{
bitset<32>{}, std::array{lookup::entry{
42u, bitset<32>{stdx::place_bits, 0}}}}))}},
std::array<void (*)(test_msg const &, std::size_t), 1>{
[](test_msg const &, std::size_t i) { callbacks_called.set(i); }}};
std::array<auto (*)(test_msg const &, std::size_t)->bool, 1>{
[](test_msg const &, std::size_t i) {
callbacks_called.set(i);
return true;
}}};

callbacks_called.reset();
CHECK(h.handle(test_msg{"opcode_field"_field = 42}, std::size_t{1}));
Expand Down