| 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 |
template <typename _AwaitType> |
| 82 |
struct suspend_always_tmpl_awaiter { |
| 83 |
_AwaitType x; |
| 84 |
suspend_always_tmpl_awaiter(_AwaitType __x) : x(__x) {} |
| 85 |
~suspend_always_tmpl_awaiter() {} |
| 86 |
bool await_ready() const noexcept { return false; } |
| 87 |
void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("suspend_always_tmpl_awaiter");} |
| 88 |
_AwaitType await_resume() const noexcept { PRINT ("suspend_always_tmpl_awaiter"); return x;} |
| 89 |
}; |
| 90 |
|
| 91 |
struct promise_type { |
| 92 |
|
| 93 |
promise_type() : vv(-1) { PRINT ("Created Promise"); } |
| 94 |
promise_type(int __x) : vv(__x) { PRINTF ("Created Promise with %d\n",__x); } |
| 95 |
~promise_type() { PRINT ("Destroyed Promise"); } |
| 96 |
|
| 97 |
auto get_return_object () { |
| 98 |
PRINT ("get_return_object: handle from promise"); |
| 99 |
return handle_type::from_promise (*this); |
| 100 |
} |
| 101 |
|
| 102 |
#ifdef MISSING_INITIAL_SUSPEND |
| 103 |
#else |
| 104 |
auto initial_suspend () { |
| 105 |
PRINT ("get initial_suspend (always)"); |
| 106 |
return suspend_always_prt{}; |
| 107 |
} |
| 108 |
#endif |
| 109 |
#ifdef MISSING_FINAL_SUSPEND |
| 110 |
#else |
| 111 |
auto final_suspend () noexcept { |
| 112 |
PRINT ("get final_suspend (always)"); |
| 113 |
return suspend_always_prt{}; |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
#ifdef USE_AWAIT_TRANSFORM |
| 118 |
|
| 119 |
auto await_transform (int v) { |
| 120 |
PRINTF ("await_transform an int () %d\n",v); |
| 121 |
return suspend_always_intprt (v); |
| 122 |
} |
| 123 |
|
| 124 |
auto await_transform (long v) { |
| 125 |
PRINTF ("await_transform a long () %ld\n",v); |
| 126 |
return suspend_always_longprtsq (v); |
| 127 |
} |
| 128 |
|
| 129 |
#endif |
| 130 |
|
| 131 |
auto yield_value (int v) { |
| 132 |
PRINTF ("yield_value (%d)\n", v); |
| 133 |
vv = v; |
| 134 |
return suspend_always_prt{}; |
| 135 |
} |
| 136 |
|
| 137 |
#ifdef RETURN_VOID |
| 138 |
|
| 139 |
void return_void () { |
| 140 |
PRINT ("return_void ()"); |
| 141 |
} |
| 142 |
|
| 143 |
#else |
| 144 |
|
| 145 |
void return_value (int v) { |
| 146 |
PRINTF ("return_value (%d)\n", v); |
| 147 |
vv = v; |
| 148 |
} |
| 149 |
|
| 150 |
#endif |
| 151 |
void unhandled_exception() { PRINT ("** unhandled exception"); } |
| 152 |
|
| 153 |
int get_value () { return vv; } |
| 154 |
private: |
| 155 |
int vv; |
| 156 |
}; |
| 157 |
|
| 158 |
}; |