| 1 |
// { dg-additional-options "-w" } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for vector" { ! hostedlib } } |
| 3 |
|
| 4 |
#include "coro.h" |
| 5 |
|
| 6 |
#include <vector> |
| 7 |
|
| 8 |
template <typename T> struct promise { |
| 9 |
T _value; |
| 10 |
coro::coroutine_handle<> _continuation = nullptr; |
| 11 |
|
| 12 |
struct final_awaitable { |
| 13 |
bool _has_continuation; |
| 14 |
final_awaitable(bool has_continuation) |
| 15 |
: _has_continuation(has_continuation) {} |
| 16 |
|
| 17 |
bool await_ready() const noexcept { return !_has_continuation; } |
| 18 |
|
| 19 |
template <typename Promise> |
| 20 |
coro::coroutine_handle<> |
| 21 |
await_suspend(coro::coroutine_handle<Promise> coro) noexcept { |
| 22 |
return coro.promise()._continuation; |
| 23 |
} |
| 24 |
|
| 25 |
void await_resume() noexcept {} |
| 26 |
}; |
| 27 |
|
| 28 |
auto get_return_object() noexcept { |
| 29 |
return coro::coroutine_handle<promise>::from_promise(*this); |
| 30 |
} |
| 31 |
|
| 32 |
auto initial_suspend() noexcept { return coro::suspend_always(); } |
| 33 |
|
| 34 |
auto final_suspend() noexcept { |
| 35 |
return final_awaitable(_continuation != nullptr); |
| 36 |
} |
| 37 |
|
| 38 |
void return_value(T value) { _value = value; } |
| 39 |
|
| 40 |
void unhandled_exception() { /*std::terminate();*/ } |
| 41 |
|
| 42 |
}; |
| 43 |
|
| 44 |
template <typename T> struct task { |
| 45 |
using promise_type = promise<T>; |
| 46 |
coro::coroutine_handle<promise<T>> _handle; |
| 47 |
|
| 48 |
task(coro::coroutine_handle<promise<T>> handle) : _handle(handle) {} |
| 49 |
|
| 50 |
bool await_ready() noexcept { return _handle.done(); } |
| 51 |
|
| 52 |
coro::coroutine_handle<> |
| 53 |
await_suspend(coro::coroutine_handle<> handle) noexcept { |
| 54 |
_handle.promise()._continuation = handle; |
| 55 |
return _handle; |
| 56 |
} |
| 57 |
|
| 58 |
T await_resume() noexcept { return _handle.promise()._value; } |
| 59 |
}; |
| 60 |
|
| 61 |
task<std::vector<int>> foo() |
| 62 |
{ |
| 63 |
co_return std::vector<int>(); |
| 64 |
} |
| 65 |
|
| 66 |
task<int> bar() |
| 67 |
{ |
| 68 |
while ((co_await foo()).empty()) { |
| 69 |
} |
| 70 |
co_return 0; |
| 71 |
} |