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