-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
275 lines (214 loc) · 6.93 KB
/
main.cpp
File metadata and controls
275 lines (214 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <vector>
#include <string>
#include <iostream>
using namespace std;
#include <coroutine>
#include <optional>
#include <functional>
#include <memory>
#include "coro.h"
static void call_async_task1(int i, std::function<void(std::error_code, int)> callback) {
std::thread([=] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
callback({}, i + 100);
}).detach();
}
static void call_async_task2(int i, std::function<void(std::error_code, std::string)> callback) {
std::thread([=] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
callback({}, std::to_string(i) + " ack");
}).detach();
}
static void call_async_task3(int i, std::function<void(std::error_code, int)> callback) {
std::thread([=] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
callback({}, i + 300);
}).detach();
}
static co::task<std::string> test_async_to_coro(int pp) {
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " start" << std::endl;
auto&& [ec, x] = co_await co::call_async<std::error_code, int>(
call_async_task1,
50
);
if (ec) {
co_return "error1";
}
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " " << x << std::endl;
auto&& [ec2, y] = co_await co::call_async<std::error_code, std::string>(
call_async_task2,
50
);
if (ec2) {
co_return "error2";
}
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " " << y << std::endl;
if (pp == 100) {
co_return "got 100";
}
co_return "ok";
}
static co::task<std::string> test_coro_wit_exception(int pp) {
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " start" << std::endl;
co_await co::call_async<std::error_code, int>(
call_async_task1,
50
);
throw std::runtime_error{ "throw runtime error" };
co_return "ok";
}
static void call_async_void(std::function<void()> cb) {
std::thread([=] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cb();
}).detach();
}
static void run_immediately(std::function<void()> cb) {
cb();
}
static co::task<void> test_coro_void() {
co_await co::call_async<>(
call_async_void
);
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " test_coro_void " << std::endl;
}
static co::task<int> test_coro_nest() {
auto&& [ec, a] = co_await co::call_async<std::error_code, std::string>(
call_async_task2,
1030
);
if (ec) {
std::cout << __FUNCTION__ << " got error: " << ec.message() << std::endl;
co_return 0;
}
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " a = " << a << std::endl;
co_await co::call_async<>(
call_async_void
);
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " call_async_void " << std::endl;
co_await co::call_async<>(
run_immediately
);
std::cout << __FUNCTION__ << "[" << std::this_thread::get_id() << "]" << " run_immediately " << std::endl;
try {
co_await co::call_coro(
test_coro_wit_exception,
23
);
}
catch (std::runtime_error& ex) {
std::cout << __FUNCTION__ << " got exception 1: " << ex.what() << std::endl;
}
try {
std::string ret = co_await co::call_coro(
test_async_to_coro,
100
);
std::cout << __FUNCTION__ << " got result: " << ret << std::endl;
} catch (std::runtime_error& ex) {
std::cout << __FUNCTION__ << " got exception: " << ex.what() << std::endl;
}
co_return 5;
}
static void call_async_task_refer(std::function<void(const std::string&, std::string)> callback) {
std::string sss = "hello";
std::string bbb = "abc";
callback(sss, bbb);
}
static co::task<void> run_coro_async_refer() {
auto&& [str, str2] = co_await co::call_async<std::string, std::string>(
call_async_task_refer
);
std::cout << str << ", " << str2 << std::endl;
}
static co::task<const std::string&> run_coro_with_refer() {
static std::string xxx = "xxxxxxx";
co_return xxx;
}
class my_class {
public:
co::task<std::string> run_coro(int y) {
co_await co::call_coro(
run_coro_async_refer
);
co_return "returned " + std::to_string(y);
}
co::task<std::string> run_coro_const(int y) const {
co_await co::call_coro(
run_coro_async_refer
);
co_return "returned " + std::to_string(y + x);
}
void call_async_task_refer(std::function<void(const std::string&, std::string)> callback) {
std::string sss = "hello";
std::string bbb = "abc";
callback(sss, bbb);
}
private:
int x = 5;
};
int main() {
auto tt = co::run_coro(
test_coro_nest
);
std::cout << "final result = " << tt.get() << std::endl;
auto xx = co::run_coro(
test_coro_void
);
xx.get();
auto yy = co::run_coro(
run_coro_async_refer
);
yy.get();
auto zz = co::run_coro(
run_coro_with_refer
);
std::cout << zz.get() << std::endl;
my_class cls;
auto kk = co::run_coro(
&my_class::run_coro,
&cls,
20
);
std::cout << kk.get() << std::endl;
std::shared_ptr<my_class> mc =
std::make_shared<my_class>();
auto jj = co::run_coro(
&my_class::run_coro_const,
mc.get(),
20
);
std::cout << jj.get() << std::endl;
std::cout << "cls = " << mc.get() << std::endl;
auto lam = [mc](int y) -> co::task<std::string> {
std::cout << "cls = " << mc.get() << std::endl;
std::string ss = co_await co::call_coro(
&my_class::run_coro_const,
mc.get(),
30
);
auto&& [s3, s4] = co_await co::call_async<std::string, std::string>(
&my_class::call_async_task_refer,
mc.get()
);
co_return "returned " + std::to_string(y) + " " + ss + " " + s3 + " " + s4;
};
auto ll = co::run_coro(
lam,
8
);
std::cout << ll.get() << std::endl;
std::cout << "cls = " << mc.get() << std::endl;
auto lam2 = [mc](this auto self, int y) {
std::cout << "cls = " << self.mc.get() << std::endl;
std::string ss = co::run_coro(
&my_class::run_coro_const,
self.mc.get(),
30
).get();
return "returned " + std::to_string(y) + " " + ss;
};
lam2(23);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return 0;
}