145
|
1 struct coro1 {
|
|
2 struct promise_type;
|
|
3 using handle_type = coro::coroutine_handle<coro1::promise_type>;
|
|
4 handle_type handle;
|
|
5 coro1 () : handle(0) {}
|
|
6 coro1 (handle_type _handle)
|
|
7 : handle(_handle) {
|
|
8 PRINT("Created coro1 object from handle");
|
|
9 }
|
|
10 coro1 (const coro1 &) = delete; // no copying
|
|
11 coro1 (coro1 &&s) : handle(s.handle) {
|
|
12 s.handle = nullptr;
|
|
13 PRINT("coro1 mv ctor ");
|
|
14 }
|
|
15 coro1 &operator = (coro1 &&s) {
|
|
16 handle = s.handle;
|
|
17 s.handle = nullptr;
|
|
18 PRINT("coro1 op= ");
|
|
19 return *this;
|
|
20 }
|
|
21 ~coro1() {
|
|
22 PRINT("Destroyed coro1");
|
|
23 if ( handle )
|
|
24 handle.destroy();
|
|
25 }
|
|
26
|
|
27 // Some awaitables to use in tests.
|
|
28 // With progress printing for debug.
|
|
29 struct suspend_never_prt {
|
|
30 #ifdef MISSING_AWAIT_READY
|
|
31 #else
|
|
32 bool await_ready() const noexcept { return true; }
|
|
33 #endif
|
|
34 void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp");}
|
|
35 void await_resume() const noexcept { PRINT ("susp-never-resume");}
|
|
36 };
|
|
37
|
|
38 struct suspend_always_prt {
|
|
39 bool await_ready() const noexcept { return false; }
|
|
40 #ifdef MISSING_AWAIT_SUSPEND
|
|
41 #else
|
|
42 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");}
|
|
43 #endif
|
|
44 void await_resume() const noexcept { PRINT ("susp-always-resume");}
|
|
45 ~suspend_always_prt() { PRINT ("susp-always-dtor"); }
|
|
46 };
|
|
47
|
|
48 struct suspend_always_intprt {
|
|
49 int x;
|
|
50 suspend_always_intprt() : x(5) {}
|
|
51 suspend_always_intprt(int __x) : x(__x) {}
|
|
52 ~suspend_always_intprt() {}
|
|
53 bool await_ready() const noexcept { return false; }
|
|
54 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-intprt");}
|
|
55 #ifdef MISSING_AWAIT_RESUME
|
|
56 #else
|
|
57 int await_resume() const noexcept { PRINT ("susp-always-resume-intprt"); return x;}
|
|
58 #endif
|
|
59 };
|
|
60
|
|
61 /* This returns the square of the int that it was constructed with. */
|
|
62 struct suspend_always_longprtsq {
|
|
63 long x;
|
|
64 suspend_always_longprtsq() : x(12L) { PRINT ("suspend_always_longprtsq def ctor"); }
|
|
65 suspend_always_longprtsq(long _x) : x(_x) { PRINTF ("suspend_always_longprtsq ctor with %ld\n", x); }
|
|
66 ~suspend_always_longprtsq() {}
|
|
67 bool await_ready() const noexcept { return false; }
|
|
68 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-longsq");}
|
|
69 long await_resume() const noexcept { PRINT ("susp-always-resume-longsq"); return x * x;}
|
|
70 };
|
|
71
|
|
72 struct suspend_always_intrefprt {
|
|
73 int& x;
|
|
74 suspend_always_intrefprt(int& __x) : x(__x) {}
|
|
75 ~suspend_always_intrefprt() {}
|
|
76 bool await_ready() const noexcept { return false; }
|
|
77 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-intprt");}
|
|
78 int& await_resume() const noexcept { PRINT ("susp-always-resume-intprt"); return x;}
|
|
79 };
|
|
80
|
|
81 struct promise_type {
|
|
82
|
|
83 promise_type() : vv(-1) { PRINT ("Created Promise"); }
|
|
84 promise_type(int __x) : vv(__x) { PRINTF ("Created Promise with %d\n",__x); }
|
|
85 ~promise_type() { PRINT ("Destroyed Promise"); }
|
|
86
|
|
87 auto get_return_object () {
|
|
88 PRINT ("get_return_object: handle from promise");
|
|
89 return handle_type::from_promise (*this);
|
|
90 }
|
|
91
|
|
92 auto initial_suspend () {
|
|
93 PRINT ("get initial_suspend (always)");
|
|
94 return suspend_always_prt{};
|
|
95 }
|
|
96 auto final_suspend () {
|
|
97 PRINT ("get final_suspend (always)");
|
|
98 return suspend_always_prt{};
|
|
99 }
|
|
100
|
|
101 #ifdef USE_AWAIT_TRANSFORM
|
|
102
|
|
103 auto await_transform (int v) {
|
|
104 PRINTF ("await_transform an int () %d\n",v);
|
|
105 return suspend_always_intprt (v);
|
|
106 }
|
|
107
|
|
108 auto await_transform (long v) {
|
|
109 PRINTF ("await_transform a long () %ld\n",v);
|
|
110 return suspend_always_longprtsq (v);
|
|
111 }
|
|
112
|
|
113 #endif
|
|
114
|
|
115 auto yield_value (int v) {
|
|
116 PRINTF ("yield_value (%d)\n", v);
|
|
117 vv = v;
|
|
118 return suspend_always_prt{};
|
|
119 }
|
|
120
|
|
121 #ifdef RETURN_VOID
|
|
122
|
|
123 void return_void () {
|
|
124 PRINT ("return_void ()");
|
|
125 }
|
|
126
|
|
127 #else
|
|
128
|
|
129 void return_value (int v) {
|
|
130 PRINTF ("return_value (%d)\n", v);
|
|
131 vv = v;
|
|
132 }
|
|
133
|
|
134 #endif
|
|
135 void unhandled_exception() { PRINT ("** unhandled exception"); }
|
|
136
|
|
137 int get_value () { return vv; }
|
|
138 private:
|
|
139 int vv;
|
|
140 };
|
|
141
|
|
142 };
|