1 struct coro1 { 2 struct promise_type; 3 using handle_type = coro::coroutine_handle<coro1::promise_type>; 4 handle_type handle; coro1coro15 coro1 () : handle(0) {} coro1coro16 coro1 (handle_type _handle) 7 : handle(_handle) { 8 PRINT("Created coro1 object from handle"); 9 } 10 coro1 (const coro1 &) = delete; // no copying coro1coro111 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 } ~coro1coro121 ~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 await_suspendcoro1::suspend_never_prt34 void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp");} await_resumecoro1::suspend_never_prt35 void await_resume() const noexcept { PRINT ("susp-never-resume");} 36 }; 37 38 struct suspend_always_prt { await_readycoro1::suspend_always_prt39 bool await_ready() const noexcept { return false; } 40 #ifdef MISSING_AWAIT_SUSPEND 41 #else await_suspendcoro1::suspend_always_prt42 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");} 43 #endif await_resumecoro1::suspend_always_prt44 void await_resume() const noexcept { PRINT ("susp-always-resume");} ~suspend_always_prtcoro1::suspend_always_prt45 ~suspend_always_prt() { PRINT ("susp-always-dtor"); } 46 }; 47 48 struct suspend_always_intprt { 49 int x; suspend_always_intprtcoro1::suspend_always_intprt50 suspend_always_intprt() : x(5) {} suspend_always_intprtcoro1::suspend_always_intprt51 suspend_always_intprt(int __x) : x(__x) {} ~suspend_always_intprtcoro1::suspend_always_intprt52 ~suspend_always_intprt() {} await_readycoro1::suspend_always_intprt53 bool await_ready() const noexcept { return false; } await_suspendcoro1::suspend_always_intprt54 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-intprt");} 55 #ifdef MISSING_AWAIT_RESUME 56 #else await_resumecoro1::suspend_always_intprt57 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; suspend_always_longprtsqcoro1::suspend_always_longprtsq64 suspend_always_longprtsq() : x(12L) { PRINT ("suspend_always_longprtsq def ctor"); } suspend_always_longprtsqcoro1::suspend_always_longprtsq65 suspend_always_longprtsq(long _x) : x(_x) { PRINTF ("suspend_always_longprtsq ctor with %ld\n", x); } ~suspend_always_longprtsqcoro1::suspend_always_longprtsq66 ~suspend_always_longprtsq() {} await_readycoro1::suspend_always_longprtsq67 bool await_ready() const noexcept { return false; } await_suspendcoro1::suspend_always_longprtsq68 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-longsq");} await_resumecoro1::suspend_always_longprtsq69 long await_resume() const noexcept { PRINT ("susp-always-resume-longsq"); return x * x;} 70 }; 71 72 struct suspend_always_intrefprt { 73 int& x; suspend_always_intrefprtcoro1::suspend_always_intrefprt74 suspend_always_intrefprt(int& __x) : x(__x) {} ~suspend_always_intrefprtcoro1::suspend_always_intrefprt75 ~suspend_always_intrefprt() {} await_readycoro1::suspend_always_intrefprt76 bool await_ready() const noexcept { return false; } await_suspendcoro1::suspend_always_intrefprt77 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-intprt");} await_resumecoro1::suspend_always_intrefprt78 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; suspend_always_tmpl_awaitercoro1::suspend_always_tmpl_awaiter84 suspend_always_tmpl_awaiter(_AwaitType __x) : x(__x) {} ~suspend_always_tmpl_awaitercoro1::suspend_always_tmpl_awaiter85 ~suspend_always_tmpl_awaiter() {} await_readycoro1::suspend_always_tmpl_awaiter86 bool await_ready() const noexcept { return false; } await_suspendcoro1::suspend_always_tmpl_awaiter87 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("suspend_always_tmpl_awaiter");} await_resumecoro1::suspend_always_tmpl_awaiter88 _AwaitType await_resume() const noexcept { PRINT ("suspend_always_tmpl_awaiter"); return x;} 89 }; 90 91 struct promise_type { 92 promise_typecoro1::promise_type93 promise_type() : vv(-1) { PRINT ("Created Promise"); } promise_typecoro1::promise_type94 promise_type(int __x) : vv(__x) { PRINTF ("Created Promise with %d\n",__x); } ~promise_typecoro1::promise_type95 ~promise_type() { PRINT ("Destroyed Promise"); } 96 get_return_objectcoro1::promise_type97 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 initial_suspendcoro1::promise_type104 auto initial_suspend () { 105 PRINT ("get initial_suspend (always)"); 106 return suspend_always_prt{}; 107 } 108 #endif 109 #ifdef MISSING_FINAL_SUSPEND 110 #else final_suspendcoro1::promise_type111 auto final_suspend () { 112 PRINT ("get final_suspend (always)"); 113 return suspend_always_prt{}; 114 } 115 #endif 116 117 #ifdef USE_AWAIT_TRANSFORM 118 await_transformcoro1::promise_type119 auto await_transform (int v) { 120 PRINTF ("await_transform an int () %d\n",v); 121 return suspend_always_intprt (v); 122 } 123 await_transformcoro1::promise_type124 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 yield_valuecoro1::promise_type131 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 return_voidcoro1::promise_type139 void return_void () { 140 PRINT ("return_void ()"); 141 } 142 143 #else 144 return_valuecoro1::promise_type145 void return_value (int v) { 146 PRINTF ("return_value (%d)\n", v); 147 vv = v; 148 } 149 150 #endif unhandled_exceptioncoro1::promise_type151 void unhandled_exception() { PRINT ("** unhandled exception"); } 152 get_valuecoro1::promise_type153 int get_value () { return vv; } 154 private: 155 int vv; 156 }; 157 158 }; 159