145
|
1 // { dg-do run }
|
|
2
|
|
3 // GRO differs from the eventual return type.
|
|
4
|
|
5 # include "../coro.h"
|
|
6
|
|
7 struct coro1 {
|
|
8 struct promise_type;
|
|
9 using handle_type = coro::coroutine_handle<coro1::promise_type>;
|
|
10 handle_type handle;
|
|
11 coro1 () : handle(0) {}
|
|
12 coro1 (handle_type _handle)
|
|
13 : handle(_handle) {
|
|
14 PRINT("Created coro1 object from handle");
|
|
15 }
|
|
16 coro1 (const coro1 &) = delete; // no copying
|
|
17 coro1 (coro1 &&s) : handle(s.handle) {
|
|
18 s.handle = nullptr;
|
|
19 PRINT("coro1 mv ctor ");
|
|
20 }
|
|
21 coro1 &operator = (coro1 &&s) {
|
|
22 handle = s.handle;
|
|
23 s.handle = nullptr;
|
|
24 PRINT("coro1 op= ");
|
|
25 return *this;
|
|
26 }
|
|
27 ~coro1() {
|
|
28 PRINT("Destroyed coro1");
|
|
29 if ( handle )
|
|
30 handle.destroy();
|
|
31 }
|
|
32
|
|
33 struct suspend_never_prt {
|
|
34 bool await_ready() const noexcept { return true; }
|
|
35 void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp");}
|
|
36 void await_resume() const noexcept { PRINT ("susp-never-resume");}
|
|
37 ~suspend_never_prt() {};
|
|
38 };
|
|
39
|
|
40 struct suspend_always_prt {
|
|
41 bool await_ready() const noexcept { return false; }
|
|
42 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");}
|
|
43 void await_resume() const noexcept { PRINT ("susp-always-resume");}
|
|
44 };
|
|
45
|
|
46 struct promise_type {
|
|
47 promise_type() { PRINT ("Created Promise"); }
|
|
48 ~promise_type() { PRINT ("Destroyed Promise"); }
|
|
49
|
|
50 auto get_return_object () {
|
|
51 PRINT ("get_return_object: handle from promise");
|
|
52 return handle_type::from_promise (*this);
|
|
53 }
|
|
54 auto initial_suspend () {
|
|
55 PRINT ("get initial_suspend (always)");
|
|
56 return suspend_always_prt{};
|
|
57 }
|
|
58 auto final_suspend () {
|
|
59 PRINT ("get final_suspend (always)");
|
|
60 return suspend_always_prt{};
|
|
61 }
|
|
62 void return_void () {
|
|
63 PRINT ("return_void ()");
|
|
64 }
|
|
65 void unhandled_exception() { PRINT ("** unhandled exception"); }
|
|
66 };
|
|
67 };
|
|
68
|
|
69 struct coro1
|
|
70 f () noexcept
|
|
71 {
|
|
72 PRINT ("coro1: about to return");
|
|
73 co_return;
|
|
74 }
|
|
75
|
|
76 int main ()
|
|
77 {
|
|
78 PRINT ("main: create coro1");
|
|
79 struct coro1 x = f ();
|
|
80 PRINT ("main: got coro1 - resuming");
|
|
81 if (x.handle.done())
|
|
82 abort();
|
|
83 x.handle.resume();
|
|
84 PRINT ("main: after resume");
|
|
85 if (!x.handle.done())
|
|
86 {
|
|
87 PRINT ("main: apparently not done...");
|
|
88 abort ();
|
|
89 }
|
|
90 PRINT ("main: returning");
|
|
91 return 0;
|
|
92 }
|