Skip to content

Commit da37295

Browse files
committed
feat: use conditional coroutine support in public API
This is to make the public API buildable also under C++17.
1 parent 6b3bdbb commit da37295

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

include/sdbus-c++/Awaitable.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#define SDBUS_CXX_AWAITABLE_H_
3030

3131
#include <atomic>
32+
#if __has_include(<coroutine>)
3233
#include <coroutine>
34+
#endif
3335
#include <cstdint>
3436
#include <exception>
3537
#include <memory>
@@ -61,8 +63,17 @@ struct AwaitableData
6163
{
6264
using result_type = std::conditional_t<std::is_void_v<T>, std::monostate, T>;
6365
std::variant<result_type, std::exception_ptr> result;
66+
#ifdef __cpp_lib_coroutine
6467
std::coroutine_handle<> handle;
68+
#endif // __cpp_lib_coroutine
6569
std::atomic<AwaitableState> status{AwaitableState::NotReady};
70+
71+
void resumeCoroutine()
72+
{
73+
#ifdef __cpp_lib_coroutine
74+
handle.resume();
75+
#endif // __cpp_lib_coroutine
76+
}
6677
};
6778

6879
/********************************************//**
@@ -82,6 +93,9 @@ struct AwaitableData
8293
* instance, such as IProxy::callMethodAsync with with_awaitable_t tag,
8394
* or the .getResultAsAwaitable() methods of the high-level API.
8495
*
96+
* The class represents nothing, i.e. is a simple placeholder class, if the API
97+
* is used as C++17 or with a standard library not supporting coroutines.
98+
*
8599
***********************************************/
86100
template <typename T>
87101
class Awaitable
@@ -92,6 +106,8 @@ class Awaitable
92106
{
93107
}
94108

109+
#ifdef __cpp_lib_coroutine
110+
95111
// Called when the coroutine is co_await'ed. Returns true if the coroutine should be suspended.
96112
[[nodiscard]] bool await_ready() const noexcept
97113
{
@@ -121,6 +137,8 @@ class Awaitable
121137
return std::get<T>(std::move(data_->result));
122138
}
123139

140+
#endif // __cpp_lib_coroutine
141+
124142
private:
125143
std::shared_ptr<AwaitableData<T>> data_;
126144
};

include/sdbus-c++/ConvenienceApiClasses.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ namespace sdbus {
374374

375375
auto previous = data->status.exchange(AwaitableState::Completed, std::memory_order_acq_rel);
376376
if (previous == AwaitableState::Waiting)
377-
data->handle.resume();
377+
data->resumeCoroutine();
378378
});
379379

380380
return Awaitable(data);

src/Proxy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Awaitable<MethodReply> Proxy::callMethodAsync(const MethodCall& message, uint64_
199199

200200
auto previous = data->status.exchange(AwaitableState::Completed, std::memory_order_acq_rel);
201201
if (previous == AwaitableState::Waiting)
202-
data->handle.resume();
202+
data->resumeCoroutine();
203203
};
204204

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

0 commit comments

Comments
 (0)