Skip to content

Commit db11ba5

Browse files
committed
refactor: use variant type for result
1 parent 3988cd7 commit db11ba5

3 files changed

Lines changed: 26 additions & 44 deletions

File tree

include/sdbus-c++/Awaitable.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <cstdint>
3434
#include <exception>
3535
#include <memory>
36-
#include <optional>
3736
#include <type_traits>
3837
#include <variant>
3938

@@ -61,8 +60,7 @@ template <typename T>
6160
struct AwaitableData
6261
{
6362
using result_type = std::conditional_t<std::is_void_v<T>, std::monostate, T>;
64-
std::optional<result_type> result;
65-
std::optional<std::exception_ptr> exception;
63+
std::variant<result_type, std::exception_ptr> result;
6664
std::coroutine_handle<> handle;
6765
std::atomic<AwaitableState> status{AwaitableState::NotReady};
6866
};
@@ -89,7 +87,10 @@ template <typename T>
8987
class Awaitable
9088
{
9189
public:
92-
explicit Awaitable(std::shared_ptr<AwaitableData<T>> data) : data_(std::move(data)) {};
90+
explicit Awaitable(std::shared_ptr<AwaitableData<T>> data)
91+
: data_(std::move(data))
92+
{
93+
}
9394

9495
// Called when the coroutine is co_await'ed. Returns true if the coroutine should be suspended.
9596
[[nodiscard]] bool await_ready() const noexcept
@@ -111,19 +112,13 @@ class Awaitable
111112
// Called when the coroutine is resumed. Returns the result or throws the exception.
112113
T await_resume() const
113114
{
114-
if (data_->exception)
115-
{
116-
std::rethrow_exception(*data_->exception);
117-
}
115+
if (auto* exception = std::get_if<std::exception_ptr>(&data_->result); exception != nullptr)
116+
std::rethrow_exception(*exception);
118117

119118
if constexpr (std::is_void_v<T>)
120-
{
121119
return;
122-
}
123120
else
124-
{
125-
return std::move(*data_->result);
126-
}
121+
return std::get<T>(std::move(data_->result));
127122
}
128123

129124
private:

include/sdbus-c++/ConvenienceApiClasses.inl

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -357,34 +357,27 @@ namespace sdbus {
357357
template <typename... Args>
358358
Awaitable<awaitable_return_t<Args...>> AsyncMethodInvoker::getResultAsAwaitable()
359359
{
360+
// awaitable_return_t<Args...> will be void for no D-Bus method return value
361+
// or T for single D-Bus method return value
362+
// or std::tuple<...> for multiple method return values
360363
auto data = std::make_shared<AwaitableData<awaitable_return_t<Args...>>>();
361-
uponReplyInvoke(
362-
[data](std::optional<Error> error, Args... args)
363-
{
364-
if (error)
365-
{
366-
data->exception = std::make_exception_ptr(*std::move(error));
367-
}
364+
365+
uponReplyInvoke([data](std::optional<Error> error, Args... args)
366+
{
367+
if (!error)
368+
if constexpr (!std::is_void_v<awaitable_return_t<Args...>>)
369+
data->result = {std::move(args)...};
368370
else
369-
{
370-
if constexpr (!std::is_void_v<awaitable_return_t<Args...>>)
371-
{
372-
data->result.emplace(std::move(args)...);
373-
}
374-
else
375-
{
376-
data->result = std::monostate{};
377-
}
378-
}
371+
data->result = std::monostate{};
372+
else
373+
data->result = std::make_exception_ptr(*std::move(error));
379374

380-
auto previous = data->status.exchange(AwaitableState::Completed, std::memory_order_acq_rel);
381-
if (previous == AwaitableState::Waiting)
382-
{
383-
data->handle.resume();
384-
}
385-
});
375+
auto previous = data->status.exchange(AwaitableState::Completed, std::memory_order_acq_rel);
376+
if (previous == AwaitableState::Waiting)
377+
data->handle.resume();
378+
});
386379

387-
return Awaitable<awaitable_return_t<Args...>>(data);
380+
return Awaitable(data);
388381
}
389382

390383
/*** ---------------- ***/

src/Proxy.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,13 @@ Awaitable<MethodReply> Proxy::callMethodAsync(const MethodCall& message, uint64_
192192
async_reply_handler asyncReplyCallback = [data](MethodReply reply, std::optional<Error> error) noexcept
193193
{
194194
if (!error)
195-
{
196195
data->result = std::move(reply);
197-
}
198196
else
199-
{
200-
data->exception = std::make_exception_ptr(*std::move(error));
201-
}
197+
data->result = std::make_exception_ptr(*std::move(error));
202198

203199
auto previous = data->status.exchange(AwaitableState::Completed, std::memory_order_acq_rel);
204200
if (previous == AwaitableState::Waiting)
205-
{
206201
data->handle.resume();
207-
}
208202
};
209203

210204
(void)Proxy::callMethodAsync(message, std::move(asyncReplyCallback), timeout);

0 commit comments

Comments
 (0)