refactor: use optional for passing potential call errors (#396)

This switches from a raw pointer to std::optional type to pass prospective call errors to the client (using std::optional was not possible years back when sdbus-c++ was based on C++14). This makes the API a little clearer, safer, idiomatically more expressive, and removes potential confusion associated with raw pointers (like ownership, lifetime questions, etc.).
This commit is contained in:
Stanislav Angelovič
2024-04-24 20:20:29 +02:00
parent 914d133d10
commit 3de2812a60
12 changed files with 101 additions and 98 deletions
+8 -8
View File
@@ -85,7 +85,7 @@ void TestProxy::onSignalWithoutRegistration(const sdbus::Struct<std::string, sdb
m_gotSignalWithSignature = true;
}
void TestProxy::onDoOperationReply(uint32_t returnValue, const sdbus::Error* error)
void TestProxy::onDoOperationReply(uint32_t returnValue, std::optional<sdbus::Error> error)
{
if (m_DoOperationClientSideAsyncReplyHandler)
m_DoOperationClientSideAsyncReplyHandler(returnValue, error);
@@ -99,7 +99,7 @@ void TestProxy::onPropertiesChanged( const std::string& interfaceName
m_onPropertiesChangedHandler(interfaceName, changedProperties, invalidatedProperties);
}
void TestProxy::installDoOperationClientSideAsyncReplyHandler(std::function<void(uint32_t res, const sdbus::Error* err)> handler)
void TestProxy::installDoOperationClientSideAsyncReplyHandler(std::function<void(uint32_t res, std::optional<sdbus::Error> err)> handler)
{
m_DoOperationClientSideAsyncReplyHandler = std::move(handler);
}
@@ -117,9 +117,9 @@ sdbus::PendingAsyncCall TestProxy::doOperationClientSideAsync(uint32_t param)
return getProxy().callMethodAsync("doOperation")
.onInterface(sdbus::test::INTERFACE_NAME)
.withArguments(param)
.uponReplyInvoke([this](const sdbus::Error* error, uint32_t returnValue)
.uponReplyInvoke([this](std::optional<sdbus::Error> error, uint32_t returnValue)
{
this->onDoOperationReply(returnValue, error);
this->onDoOperationReply(returnValue, std::move(error));
});
}
@@ -143,9 +143,9 @@ void TestProxy::doErroneousOperationClientSideAsync()
{
getProxy().callMethodAsync("throwError")
.onInterface(sdbus::test::INTERFACE_NAME)
.uponReplyInvoke([this](const sdbus::Error* error)
.uponReplyInvoke([this](std::optional<sdbus::Error> error)
{
this->onDoOperationReply(0, error);
this->onDoOperationReply(0, std::move(error));
});
}
@@ -163,9 +163,9 @@ void TestProxy::doOperationClientSideAsyncWithTimeout(const std::chrono::microse
.onInterface(sdbus::test::INTERFACE_NAME)
.withTimeout(timeout)
.withArguments(param)
.uponReplyInvoke([this](const sdbus::Error* error, uint32_t returnValue)
.uponReplyInvoke([this](std::optional<sdbus::Error> error, uint32_t returnValue)
{
this->onDoOperationReply(returnValue, error);
this->onDoOperationReply(returnValue, std::move(error));
});
}