Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-07-tmpl.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // Check that we correctly operate when the coroutine object is templated.
5
6 #include "../coro.h"
7
8 template <typename T>
9 struct coro1 {
10 struct promise_type;
11 using handle_type = coro::coroutine_handle<coro1::promise_type>;
12 handle_type handle;
13 coro1 () : handle(0) {}
14 coro1 (handle_type _handle)
15 : handle(_handle) {
16 PRINT ("Created coro1 object from handle");
17 }
18 coro1 (const coro1 &) = delete; // no copying
19 coro1 (coro1 &&s) : handle(s.handle) {
20 s.handle = nullptr;
21 PRINT ("Moved coro1");
22 }
23 coro1 &operator = (coro1 &&s) {
24 handle = s.handle;
25 s.handle = nullptr;
26 return *this;
27 }
28 ~coro1() {
29 PRINT ("Destroyed coro1");
30 if ( handle )
31 handle.destroy();
32 }
33
34 struct suspend_never_prt {
35 ~suspend_never_prt() {}
36 bool await_ready() const noexcept { return true; }
37 void await_suspend(handle_type h) const noexcept { PRINT ("susp-never-susp");}
38 void await_resume() const noexcept {PRINT ("susp-never-resume");}
39 };
40
41 struct suspend_always_prt {
42 T x;
43 bool await_ready() const noexcept { return false; }
44 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");}
45 void await_resume() const noexcept {PRINT ("susp-always-resume");}
46 };
47
48 /* This returns the int it was constructed with. */
49 struct suspend_always_intprt {
50 T x;
51 suspend_always_intprt() : x((T)5) { PRINT ("suspend_always_intprt def ctor"); }
52 suspend_always_intprt(T _x) : x(_x)
53 { PRINTF ("suspend_always_intprt ctor with %ld\n", (long)x); }
54 ~suspend_always_intprt() {}
55 bool await_ready() const noexcept { return false; }
56 void await_suspend(coro::coroutine_handle<>) const noexcept { PRINT ("susp-always-susp-int");}
57 int await_resume() const noexcept { PRINT ("susp-always-resume-int"); return x;}
58 };
59
60 struct promise_type {
61 T value;
62 promise_type() { PRINT ("Created Promise"); }
63 ~promise_type() { PRINT ("Destroyed Promise"); }
64
65 coro1 get_return_object() {
66 PRINT ("get_return_object: from handle from promise");
67 return coro1 (handle_type::from_promise (*this));
68 }
69
70 auto initial_suspend() {
71 PRINT ("get initial_suspend ");
72 return suspend_never_prt{};
73 }
74
75 auto final_suspend() noexcept {
76 PRINT ("get final_suspend");
77 return suspend_always_prt{};
78 }
79
80 void return_value (int v) {
81 PRINTF ("return_value () %ld\n", (long) v);
82 value = v;
83 }
84
85 auto await_transform (T v) {
86 PRINTF ("await_transform a T () %ld\n", (long)v);
87 return suspend_always_intprt (v);
88 }
89
90 T get_value () { return value; }
91 void unhandled_exception() { PRINT ("** unhandled exception"); }
92 };
93 };
94
95 /* Valued with an await_transform. */
96 int gX = 2;
97
98 template <typename T>
99 coro1<T> f ()
100 {
101 for (int i = 0; i < 4; ++i)
102 {
103 gX += co_await 10;
104 }
105 co_return gX;
106 }
107
108 int main ()
109 {
110 PRINT ("main: create coro1");
111 auto f_coro = f<int>();
112
113 PRINT ("main: got coro1 - checking gX");
114 if (gX != 2)
115 {
116 PRINTF ("main: gX is wrong : %d, should be 2\n", gX);
117 abort ();
118 }
119 PRINT ("main: gX OK -- looping");
120 do {
121 f_coro.handle.resume();
122 } while (!f_coro.handle.done());
123
124 int y = f_coro.handle.promise().get_value();
125
126 if (y != 42)
127 {
128 PRINTF ("main: y is wrong : %d, should be 42\n", y);
129 abort ();
130 }
131 PRINT ("main: done");
132 return 0;
133 }