Mercurial > hg > CbC > CbC_gcc
comparison gcc/testsuite/g++.dg/coroutines/torture/co-ret-06-template-promise-val-1.C @ 145:1830386684a0
gcc-9.2.0
author | anatofuz |
---|---|
date | Thu, 13 Feb 2020 11:34:05 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
131:84e7813d76e9 | 145:1830386684a0 |
---|---|
1 // { dg-do run } | |
2 | |
3 // Test returning a T. | |
4 // We will use the promise to contain this to avoid having to include | |
5 // additional C++ headers. | |
6 | |
7 #include "../coro.h" | |
8 | |
9 struct suspend_never_prt { | |
10 bool await_ready() const noexcept { return true; } | |
11 void await_suspend(coro::coroutine_handle<>) const noexcept | |
12 { PRINT ("susp-never-susp"); } | |
13 void await_resume() const noexcept { PRINT ("susp-never-resume");} | |
14 }; | |
15 | |
16 /* NOTE: this has a DTOR to test that pathway. */ | |
17 struct suspend_always_prt { | |
18 bool await_ready() const noexcept { return false; } | |
19 void await_suspend(coro::coroutine_handle<>) const noexcept | |
20 { PRINT ("susp-always-susp"); } | |
21 void await_resume() const noexcept { PRINT ("susp-always-resume"); } | |
22 ~suspend_always_prt() { PRINT ("susp-always-DTOR"); } | |
23 }; | |
24 | |
25 template <typename T> | |
26 struct coro1 { | |
27 struct promise_type; | |
28 using handle_type = coro::coroutine_handle<coro1::promise_type>; | |
29 handle_type handle; | |
30 coro1 () : handle(0) {} | |
31 coro1 (handle_type _handle) | |
32 : handle(_handle) { | |
33 PRINT("Created coro1 object from handle"); | |
34 } | |
35 coro1 (const coro1 &) = delete; // no copying | |
36 coro1 (coro1 &&s) : handle(s.handle) { | |
37 s.handle = nullptr; | |
38 PRINT("coro1 mv ctor "); | |
39 } | |
40 coro1 &operator = (coro1 &&s) { | |
41 handle = s.handle; | |
42 s.handle = nullptr; | |
43 PRINT("coro1 op= "); | |
44 return *this; | |
45 } | |
46 ~coro1() { | |
47 PRINT("Destroyed coro1"); | |
48 if ( handle ) | |
49 handle.destroy(); | |
50 } | |
51 | |
52 struct promise_type { | |
53 T value; | |
54 promise_type() { PRINT ("Created Promise"); } | |
55 ~promise_type() { PRINT ("Destroyed Promise"); } | |
56 | |
57 auto get_return_object () { | |
58 PRINT ("get_return_object: handle from promise"); | |
59 return handle_type::from_promise (*this); | |
60 } | |
61 | |
62 auto initial_suspend () const { | |
63 PRINT ("get initial_suspend (always)"); | |
64 return suspend_always_prt{}; | |
65 } | |
66 auto final_suspend () const { | |
67 PRINT ("get final_suspend (always)"); | |
68 return suspend_always_prt{}; | |
69 } | |
70 void return_value (T v) { | |
71 PRINTF ("return_value () %d\n",v); | |
72 value = v; | |
73 } | |
74 T get_value (void) { return value; } | |
75 void unhandled_exception() { PRINT ("** unhandled exception"); } | |
76 }; | |
77 }; | |
78 | |
79 coro1<float> | |
80 f () noexcept | |
81 { | |
82 PRINT ("coro1: about to return"); | |
83 co_return (float) 42; | |
84 } | |
85 | |
86 int main () | |
87 { | |
88 PRINT ("main: create coro1"); | |
89 coro1<float> x = f (); | |
90 PRINT ("main: got coro1 - resuming"); | |
91 if (x.handle.done()) | |
92 abort(); | |
93 x.handle.resume(); | |
94 PRINT ("main: after resume"); | |
95 int y = x.handle.promise().get_value(); | |
96 if ( y != (float)42 ) | |
97 abort (); | |
98 if (!x.handle.done()) | |
99 { | |
100 PRINT ("main: apparently not done..."); | |
101 abort (); | |
102 } | |
103 PRINT ("main: returning"); | |
104 return 0; | |
105 } |