| 1 |
// { dg-do run { target c++17 } } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
#include "../coro.h" |
| 5 |
|
| 6 |
class resumable { |
| 7 |
public: |
| 8 |
struct promise_type; |
| 9 |
using coro_handle = std::coroutine_handle<promise_type>; |
| 10 |
resumable(coro_handle handle) : handle_(handle) { } |
| 11 |
resumable(resumable&) = delete; |
| 12 |
resumable(resumable&&) = delete; |
| 13 |
~resumable() { handle_.destroy(); } |
| 14 |
coro_handle handle_; |
| 15 |
}; |
| 16 |
|
| 17 |
struct resumable::promise_type { |
| 18 |
using coro_handle = std::coroutine_handle<promise_type>; |
| 19 |
int used; |
| 20 |
auto get_return_object() { |
| 21 |
return coro_handle::from_promise(*this); |
| 22 |
} |
| 23 |
auto initial_suspend() { return std::suspend_never(); } |
| 24 |
auto final_suspend() noexcept { return std::suspend_always(); } |
| 25 |
void return_value(int x) {used = x;} |
| 26 |
void unhandled_exception() {} |
| 27 |
|
| 28 |
struct TestAwaiter { |
| 29 |
int recent_test; |
| 30 |
TestAwaiter(int test) : recent_test{test} {} |
| 31 |
bool await_ready() { return false; } |
| 32 |
void await_suspend(std::coroutine_handle<promise_type>) {} |
| 33 |
int await_resume() { |
| 34 |
return recent_test; |
| 35 |
} |
| 36 |
auto operator co_await() { |
| 37 |
return *this; |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
struct TestAwaiterCH :TestAwaiter { |
| 42 |
TestAwaiterCH(int test) : TestAwaiter(test) {}; |
| 43 |
}; |
| 44 |
|
| 45 |
struct TestAwaiterCHCH :TestAwaiterCH { |
| 46 |
TestAwaiterCHCH(int test) : TestAwaiterCH(test) {}; |
| 47 |
|
| 48 |
resumable foo(){ |
| 49 |
int x = co_await *this; |
| 50 |
co_return x; |
| 51 |
} |
| 52 |
}; |
| 53 |
}; |
| 54 |
|
| 55 |
struct TestP { |
| 56 |
resumable::promise_type::TestAwaiterCHCH tp = resumable::promise_type::TestAwaiterCHCH(6); |
| 57 |
}; |
| 58 |
|
| 59 |
resumable foo1(int t){ |
| 60 |
int x = co_await resumable::promise_type::TestAwaiterCH(t); |
| 61 |
co_return x; |
| 62 |
} |
| 63 |
|
| 64 |
resumable foo2(){ |
| 65 |
struct TestP TP; |
| 66 |
int x = co_await TP.tp; |
| 67 |
co_return x; |
| 68 |
} |
| 69 |
|
| 70 |
resumable foo3(){ |
| 71 |
int x = co_await TestP{}.tp; |
| 72 |
co_return x; |
| 73 |
} |
| 74 |
|
| 75 |
int main(){ |
| 76 |
auto t = resumable::promise_type::TestAwaiterCHCH(4); |
| 77 |
resumable res = t.foo(); |
| 78 |
while (!res.handle_.done()) |
| 79 |
res.handle_.resume(); |
| 80 |
if (res.handle_.promise().used != 4) |
| 81 |
abort(); |
| 82 |
|
| 83 |
resumable res1 = foo1(5); |
| 84 |
while (!res1.handle_.done()) |
| 85 |
res1.handle_.resume(); |
| 86 |
if (res1.handle_.promise().used != 5) |
| 87 |
abort(); |
| 88 |
|
| 89 |
resumable res2 = foo2(); |
| 90 |
while (!res2.handle_.done()) |
| 91 |
res2.handle_.resume(); |
| 92 |
if (res2.handle_.promise().used != 6) |
| 93 |
abort(); |
| 94 |
|
| 95 |
resumable res3 = foo2(); |
| 96 |
while (!res3.handle_.done()) |
| 97 |
res3.handle_.resume(); |
| 98 |
if (res3.handle_.promise().used != 6) |
| 99 |
abort(); |
| 100 |
} |