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