match: allow message callbacks
The original match constructor only allowed
sd_bus_message_handler_t functions, which does not allow for
sdbusplus::message::message's to be passed in. Add a
constructor that allows functions of the following type:
void(*)(sdbusplus::message::message&)
Change-Id: Idc006250777c5cc1a5fe48fc411da24339ca165e
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/test/bus/match.cpp b/test/bus/match.cpp
index 8b58c9f..764392a 100644
--- a/test/bus/match.cpp
+++ b/test/bus/match.cpp
@@ -29,8 +29,6 @@
TEST_F(Match, FunctorIs_sd_bus_message_handler_t)
{
- using namespace std::literals;
-
bool triggered = false;
auto trigger = [](sd_bus_message *m, void* context, sd_bus_error* e)
{
@@ -49,3 +47,52 @@
waitForIt(triggered);
ASSERT_TRUE(triggered);
}
+
+TEST_F(Match, FunctorIs_LambdaTakingMessage)
+{
+ bool triggered = false;
+ auto trigger = [&triggered](sdbusplus::message::message& m)
+ {
+ triggered = true;
+ };
+
+ sdbusplus::bus::match_t m{bus, matchRule, trigger};
+ auto m2 = std::move(m); // ensure match is move-safe.
+
+ waitForIt(triggered);
+ ASSERT_FALSE(triggered);
+
+ bus.request_name(busName);
+
+ waitForIt(triggered);
+ ASSERT_TRUE(triggered);
+}
+
+TEST_F(Match, FunctorIs_MemberFunctionTakingMessage)
+{
+
+ class BoolHolder
+ {
+ public:
+ bool triggered = false;
+
+ void callback(sdbusplus::message::message& m)
+ {
+ triggered = true;
+ }
+ };
+ BoolHolder b;
+
+ sdbusplus::bus::match_t m{bus, matchRule,
+ std::bind(std::mem_fn(&BoolHolder::callback),
+ &b, std::placeholders::_1)};
+ auto m2 = std::move(m); // ensure match is move-safe.
+
+ waitForIt(b.triggered);
+ ASSERT_FALSE(b.triggered);
+
+ bus.request_name(busName);
+
+ waitForIt(b.triggered);
+ ASSERT_TRUE(b.triggered);
+}