refactor: let callbacks take message objects by value (#367)

Signatures of callbacks async_reply_handler, signal_handler, message_handler and property_set_callback were modified to take input message objects by value, as opposed to non-const ref.

The callee assumes ownership of the message. This API is more idiomatic, more expressive, cleaner and safer. Move semantics is used to pass messages to the callback handlers. In some cases, this also improves performance.
This commit is contained in:
Stanislav Angelovič
2023-10-24 21:56:47 +02:00
parent 9412940d1e
commit 6f35f00fcf
10 changed files with 37 additions and 35 deletions

View File

@@ -119,10 +119,10 @@ std::future<MethodReply> Proxy::callMethod(const MethodCall& message, uint64_t t
auto promise = std::make_shared<std::promise<MethodReply>>();
auto future = promise->get_future();
async_reply_handler asyncReplyCallback = [promise = std::move(promise)](MethodReply& reply, const Error* error) noexcept
async_reply_handler asyncReplyCallback = [promise = std::move(promise)](MethodReply reply, const Error* error) noexcept
{
if (error == nullptr)
promise->set_value(reply);
promise->set_value(std::move(reply));
else
promise->set_exception(std::make_exception_ptr(*error));
};
@@ -230,12 +230,12 @@ int Proxy::sdbus_async_reply_handler(sd_bus_message *sdbusMessage, void *userDat
const auto* error = sd_bus_message_get_error(sdbusMessage);
if (error == nullptr)
{
asyncCallData->callback(message, nullptr);
asyncCallData->callback(std::move(message), nullptr);
}
else
{
Error exception(error->name, error->message);
asyncCallData->callback(message, &exception);
asyncCallData->callback(std::move(message), &exception);
}
}, retError);
@@ -250,7 +250,7 @@ int Proxy::sdbus_signal_handler(sd_bus_message *sdbusMessage, void *userData, sd
auto message = Message::Factory::create<Signal>(sdbusMessage, &signalData->proxy.connection_->getSdBusInterface());
auto ok = invokeHandlerAndCatchErrors([&](){ signalData->callback(message); }, retError);
auto ok = invokeHandlerAndCatchErrors([&](){ signalData->callback(std::move(message)); }, retError);
return ok ? 0 : -1;
}