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
19 changes: 9 additions & 10 deletions include/msg/callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ namespace detail {
* and handling incoming messages.
*/
template <stdx::ct_string Name, typename Msg, match::matcher M,
stdx::callable F, typename... ExtraArgs>
stdx::callable F>
struct callback {
[[nodiscard]] auto is_match(auto const &data) const -> bool {
auto view = typename Msg::view_t{data};
return matcher(view);
}

template <typename... Args>
[[nodiscard]] auto handle(auto const &data,
ExtraArgs const &...args) const -> bool {
Args const &...args) const -> bool {
auto view = typename Msg::view_t{data};
if (matcher(view)) {
CIB_INFO("Incoming message matched [{}], because [{}], executing "
Expand All @@ -54,22 +55,21 @@ struct callback {
using callable_t = F;

template <match::matcher NewM>
using rebind_matcher = callback<Name, Msg, NewM, F, ExtraArgs...>;
using rebind_matcher = callback<Name, Msg, NewM, F>;

constexpr static auto name = Name;
[[no_unique_address]] matcher_t matcher;
[[no_unique_address]] callable_t callable;
};

template <stdx::ct_string Name, typename Msg, typename... ExtraArgs>
struct callback_construct_t {
template <stdx::ct_string Name, typename Msg> struct callback_construct_t {
template <match::matcher M, stdx::callable F>
[[nodiscard]] constexpr auto operator()(M, F &&f) const {
using ::operator and;
using matcher_t =
decltype(match::sum_of_products(M{} and typename Msg::matcher_t{}));
return callback<Name, Msg, matcher_t, std::remove_cvref_t<F>,
ExtraArgs...>{matcher_t{}, std::forward<F>(f)};
return callback<Name, Msg, matcher_t, std::remove_cvref_t<F>>{
matcher_t{}, std::forward<F>(f)};
}

template <msg::matcher_maker M, stdx::callable F>
Expand All @@ -86,7 +86,6 @@ struct callback_construct_t {
};
} // namespace detail

template <stdx::ct_string Name, typename Msg, typename... ExtraArgs>
constexpr inline auto callback =
detail::callback_construct_t<Name, Msg, ExtraArgs...>{};
template <stdx::ct_string Name, typename Msg>
constexpr inline auto callback = detail::callback_construct_t<Name, Msg>{};
} // namespace msg
2 changes: 1 addition & 1 deletion test/msg/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ TEST_CASE("match and dispatch only one callback with uint8_t storage",
}

TEST_CASE("dispatch with extra args", "[handler]") {
auto callback = msg::callback<"cb", msg_defn, int>(
auto callback = msg::callback<"cb", msg_defn>(
id_match<0x80>, [](msg::const_view<msg_defn>, int value) {
dispatched = true;
CHECK(value == 0xcafe);
Expand Down
2 changes: 1 addition & 1 deletion test/msg/handler_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace {
int callback_extra_arg{};

constexpr auto test_callback_extra_args =
msg::callback<"cb", msg_defn, int>(id_match, [](msg_view_t, int i) {
msg::callback<"cb", msg_defn>(id_match, [](msg_view_t, int i) {
callback_success = true;
callback_extra_arg = i;
});
Expand Down
2 changes: 1 addition & 1 deletion test/msg/indexed_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ namespace {
int callback_extra_arg{};

constexpr auto test_callback_extra_args =
msg::callback<"test_callback_extra_args", msg_defn, int>(
msg::callback<"test_callback_extra_args", msg_defn>(
msg::equal_to<test_id_field, 0x80>, [](auto, int i) {
callback_success = true;
callback_extra_arg = i;
Expand Down