Skip to content

Commit

Permalink
Merge pull request #487 from elbeno/alt-syntax-in-field-matcher
Browse files Browse the repository at this point in the history
✨ Add alt syntax for `in` field matcher
  • Loading branch information
elbeno authored Feb 1, 2024
2 parents 98b9aee + 848dd0d commit b370476
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 8 additions & 6 deletions include/msg/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ template <msg::matcher_maker T> constexpr auto operator not(T) -> mm_not_t<T> {
return {};
}

template <msg::matcher_maker T, msg::matcher_maker U> struct mm_and_t {
template <msg::matcher_maker... Ts> struct mm_and_t {
using is_matcher_maker = void;
template <typename Msg>
constexpr static auto make_matcher() -> match::matcher auto {
return T::template make_matcher<Msg>() and
U::template make_matcher<Msg>();
return (... and Ts::template make_matcher<Msg>());
}
};

Expand All @@ -80,12 +79,11 @@ constexpr auto operator and(T, U) -> mm_and_t<T, U> {
return {};
}

template <msg::matcher_maker T, msg::matcher_maker U> struct mm_or_t {
template <msg::matcher_maker... Ts> struct mm_or_t {
using is_matcher_maker = void;
template <typename Msg>
constexpr static auto make_matcher() -> match::matcher auto {
return T::template make_matcher<Msg>() or
U::template make_matcher<Msg>();
return (... or Ts::template make_matcher<Msg>());
}
};

Expand All @@ -107,6 +105,10 @@ template <typename Name> struct field_name {
return field_value<Name, T>{value};
}

template <auto... Vs>
constexpr static inline auto in =
mm_or_t<matcher_maker<Name, std::equal_to<>, Vs>...>{};

private:
template <auto V>
friend CONSTEVAL auto operator==(field_name, constant_t<V>)
Expand Down
14 changes: 14 additions & 0 deletions test/msg/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,17 @@ TEST_CASE("alternative matcher syntax (multi-field)", "[callback]") {
match::and_t<msg::equal_to_t<id_field, 0x80>,
msg::equal_to_t<field1, 1>>>);
}

TEST_CASE("alternative matcher syntax (value in set)", "[callback]") {
auto callback =
msg::callback<"cb", msg_defn>("id"_field.in<0x80, 0x90>, [] {});
static_assert(std::same_as<typename decltype(callback)::matcher_t,
match::or_t<msg::equal_to_t<id_field, 0x80>,
msg::equal_to_t<id_field, 0x90>>>);
}

TEST_CASE("alternative matcher syntax (value in singleton)", "[callback]") {
auto callback = msg::callback<"cb", msg_defn>("id"_field.in<0x80>, [] {});
static_assert(std::same_as<typename decltype(callback)::matcher_t,
msg::equal_to_t<id_field, 0x80>>);
}

0 comments on commit b370476

Please sign in to comment.