blob: c5b9b19a2d440b95681e8337983f878bc7036ded [file] [log] [blame]
#include <systemd/sd-bus.h>
#include <sdbusplus/bus/match.hpp>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/message.hpp>
namespace sdbusplus::bus::match
{
static sd_bus_slot* makeMatch(sd_bus* bus, const char* _match,
sd_bus_message_handler_t handler, void* context)
{
sd_bus_slot* slot;
int r = sd_bus_add_match(bus, &slot, _match, handler, context);
if (r < 0)
{
throw exception::SdBusError(-r, "sd_bus_match");
}
return slot;
}
match::match(sdbusplus::bus_t& bus, const char* _match,
sd_bus_message_handler_t handler, void* context) :
_slot(makeMatch(get_busp(bus), _match, handler, context))
{}
// The callback is 'noexcept' because it is called from C code (sd-bus).
static int matchCallback(sd_bus_message* m, void* context,
sd_bus_error* /*e*/) noexcept
{
auto c = static_cast<match::callback_t*>(context);
message_t message{m};
(*c)(message);
return 0;
}
match::match(sdbusplus::bus_t& bus, const char* _match, callback_t callback) :
_callback(std::make_unique<callback_t>(std::move(callback))),
_slot(makeMatch(get_busp(bus), _match, matchCallback, _callback.get()))
{}
} // namespace sdbusplus::bus::match