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
@@ -82,12 +82,12 @@ TYPED_TEST(SdbusTestObject, GetsPropertyAsynchronouslyViaPropertiesInterface)
std::promise<std::string> promise;
auto future = promise.get_future();
this->m_proxy->GetAsync(INTERFACE_NAME, "state", [&](const sdbus::Error* err, sdbus::Variant value)
this->m_proxy->GetAsync(INTERFACE_NAME, "state", [&](std::optional<sdbus::Error> err, sdbus::Variant value)
{
if (err == nullptr)
if (!err)
promise.set_value(value.get<std::string>());
else
promise.set_exception(std::make_exception_ptr(*err));
promise.set_exception(std::make_exception_ptr(*std::move(err)));
});
ASSERT_THAT(future.get(), Eq(DEFAULT_STATE_VALUE));
@@ -115,12 +115,12 @@ TYPED_TEST(SdbusTestObject, SetsPropertyAsynchronouslyViaPropertiesInterface)
std::promise<void> promise;
auto future = promise.get_future();
this->m_proxy->SetAsync(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}, [&](const sdbus::Error* err)
this->m_proxy->SetAsync(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}, [&](std::optional<sdbus::Error> err)
{
if (err == nullptr)
if (!err)
promise.set_value();
else
promise.set_exception(std::make_exception_ptr(*err));
promise.set_exception(std::make_exception_ptr(*std::move(err)));
});
ASSERT_NO_THROW(future.get());
@@ -152,12 +152,12 @@ TYPED_TEST(SdbusTestObject, GetsAllPropertiesAsynchronouslyViaPropertiesInterfac
std::promise<std::map<std::string, sdbus::Variant>> promise;
auto future = promise.get_future();
this->m_proxy->GetAllAsync(INTERFACE_NAME, [&](const sdbus::Error* err, std::map<std::string, sdbus::Variant> value)
this->m_proxy->GetAllAsync(INTERFACE_NAME, [&](std::optional<sdbus::Error> err, std::map<std::string, sdbus::Variant> value)
{
if (err == nullptr)
if (!err)
promise.set_value(std::move(value));
else
promise.set_exception(std::make_exception_ptr(*err));
promise.set_exception(std::make_exception_ptr(*std::move(err)));
});
const auto properties = future.get();