| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for vector" { ! hostedlib } } |
| 3 |
|
| 4 |
// using non-trivial types in the coro. |
| 5 |
|
| 6 |
# include "../coro.h" |
| 7 |
|
| 8 |
#include <vector> |
| 9 |
#include <string> |
| 10 |
|
| 11 |
template <typename T> |
| 12 |
struct looper { |
| 13 |
|
| 14 |
struct promise_type { |
| 15 |
T value; |
| 16 |
promise_type() { PRINT ("Created Promise"); } |
| 17 |
~promise_type() { PRINT ("Destroyed Promise"); } |
| 18 |
|
| 19 |
auto get_return_object () { |
| 20 |
PRINT ("get_return_object: handle from promise"); |
| 21 |
return handle_type::from_promise (*this); |
| 22 |
} |
| 23 |
|
| 24 |
auto initial_suspend () { |
| 25 |
PRINT ("get initial_suspend (always)"); |
| 26 |
return suspend_always_prt{}; |
| 27 |
} |
| 28 |
|
| 29 |
auto final_suspend () noexcept { |
| 30 |
PRINT ("get final_suspend (always)"); |
| 31 |
return suspend_always_prt{}; |
| 32 |
} |
| 33 |
|
| 34 |
void return_value (T v) { |
| 35 |
PRINTF ("return_value () %s\n", v.c_str()); |
| 36 |
value = v; |
| 37 |
} |
| 38 |
|
| 39 |
auto yield_value (T v) { |
| 40 |
PRINTF ("yield_value () %s and suspend always\n", v.c_str()); |
| 41 |
value = v; |
| 42 |
return suspend_always_prt{}; |
| 43 |
} |
| 44 |
|
| 45 |
T get_value (void) { return value; } |
| 46 |
|
| 47 |
void unhandled_exception() { PRINT ("** unhandled exception"); } |
| 48 |
}; |
| 49 |
|
| 50 |
using handle_type = coro::coroutine_handle<looper::promise_type>; |
| 51 |
handle_type handle; |
| 52 |
|
| 53 |
looper () : handle(0) {} |
| 54 |
looper (handle_type _handle) |
| 55 |
: handle(_handle) { |
| 56 |
PRINT("Created coro1 object from handle"); |
| 57 |
} |
| 58 |
looper (const looper &) = delete; // no copying |
| 59 |
looper (looper &&s) : handle(s.handle) { |
| 60 |
s.handle = nullptr; |
| 61 |
PRINT("looper mv ctor "); |
| 62 |
} |
| 63 |
looper &operator = (looper &&s) { |
| 64 |
handle = s.handle; |
| 65 |
s.handle = nullptr; |
| 66 |
PRINT("looper op= "); |
| 67 |
return *this; |
| 68 |
} |
| 69 |
~looper() { |
| 70 |
PRINT("Destroyed coro1"); |
| 71 |
if ( handle ) |
| 72 |
handle.destroy(); |
| 73 |
} |
| 74 |
|
| 75 |
struct suspend_never_prt { |
| 76 |
bool await_ready() const noexcept { return true; } |
| 77 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp"); } |
| 78 |
void await_resume() const noexcept { PRINT ("susp-never-resume");} |
| 79 |
}; |
| 80 |
|
| 81 |
/* NOTE: this has a DTOR to test that pathway. */ |
| 82 |
struct suspend_always_prt { |
| 83 |
bool await_ready() const noexcept { return false; } |
| 84 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp"); } |
| 85 |
void await_resume() const noexcept { PRINT ("susp-always-resume"); } |
| 86 |
~suspend_always_prt() { PRINT ("susp-always-DTOR"); } |
| 87 |
}; |
| 88 |
|
| 89 |
}; |
| 90 |
|
| 91 |
int gX ; |
| 92 |
|
| 93 |
struct mycounter |
| 94 |
{ |
| 95 |
mycounter () : v(0) { PRINT ("mycounter CTOR"); } |
| 96 |
~mycounter () { gX = 6174; PRINT ("mycounter DTOR"); } |
| 97 |
int value () { return v; } |
| 98 |
void incr () { v++; } |
| 99 |
int v; |
| 100 |
}; |
| 101 |
|
| 102 |
template <typename T> |
| 103 |
looper<T> with_ctorable_state (std::vector<T> d) noexcept |
| 104 |
{ |
| 105 |
std::vector<T> loc; |
| 106 |
unsigned lim = d.size()-1; |
| 107 |
mycounter c; |
| 108 |
for (unsigned i = 0; i < lim ; ++i) |
| 109 |
{ |
| 110 |
loc.push_back(d[i]); |
| 111 |
c.incr(); |
| 112 |
PRINTF ("f: about to yield value %d \n", i); |
| 113 |
co_yield loc[i]; |
| 114 |
} |
| 115 |
loc.push_back(d[lim]); |
| 116 |
|
| 117 |
PRINT ("f: done"); |
| 118 |
co_return loc[lim]; |
| 119 |
} |
| 120 |
|
| 121 |
int main () |
| 122 |
{ |
| 123 |
PRINT ("main: create looper"); |
| 124 |
std::vector<std::string> input = {"first", "the", "quick", "reddish", "fox", "done" }; |
| 125 |
auto f_coro = with_ctorable_state<std::string> (input); |
| 126 |
|
| 127 |
PRINT ("main: got looper - resuming (1)"); |
| 128 |
if (f_coro.handle.done()) |
| 129 |
abort(); |
| 130 |
|
| 131 |
f_coro.handle.resume(); |
| 132 |
std::string s = f_coro.handle.promise().get_value(); |
| 133 |
if ( s != "first" ) |
| 134 |
abort (); |
| 135 |
|
| 136 |
PRINTF ("main: got : %s\n", s.c_str()); |
| 137 |
unsigned check = 1; |
| 138 |
do { |
| 139 |
f_coro.handle.resume(); |
| 140 |
s = f_coro.handle.promise().get_value(); |
| 141 |
if (s != input[check++]) |
| 142 |
abort (); |
| 143 |
PRINTF ("main: got : %s\n", s.c_str()); |
| 144 |
} while (!f_coro.handle.done()); |
| 145 |
|
| 146 |
if ( s != "done" ) |
| 147 |
abort (); |
| 148 |
|
| 149 |
PRINT ("main: should be done"); |
| 150 |
if (!f_coro.handle.done()) |
| 151 |
{ |
| 152 |
PRINT ("main: apparently not done..."); |
| 153 |
abort (); |
| 154 |
} |
| 155 |
|
| 156 |
if (gX != 6174) |
| 157 |
{ |
| 158 |
PRINT ("main: apparently we didn't run mycounter DTOR..."); |
| 159 |
abort (); |
| 160 |
} |
| 161 |
PRINT ("main: returning"); |
| 162 |
return 0; |
| 163 |
} |