-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional.cpp
More file actions
128 lines (106 loc) · 3.1 KB
/
Copy pathoptional.cpp
File metadata and controls
128 lines (106 loc) · 3.1 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
#include <coroutine>
#include <future>
#include <iostream>
#include <optional>
#include <vector>
using std::cout;
using std::endl;
// https://en.cppreference.com/w/cpp/coroutine/coroutine_traits
struct AsCoroutine {};
template <typename T, typename... Args>
struct std::coroutine_traits<std::future<std::optional<T>>, AsCoroutine,
Args...> {
struct promise_type {
std::promise<std::optional<T>> p;
std::future<std::optional<T>> get_return_object() { return p.get_future(); }
auto initial_suspend() { return std::suspend_never{}; }
auto final_suspend() noexcept { return std::suspend_never{}; }
void unhandled_exception() {}
struct Awaiter {
std::optional<T> opt;
explicit Awaiter(const std::optional<T>& opt) : opt(opt) {}
explicit Awaiter(std::optional<T>&& opt) : opt(std::move(opt)) {}
bool await_ready() {
// true: await_resume()
// false: await_suspend()
return opt.has_value();
}
// for "auto XXX = co_yield YYY;"
T await_resume() {
// If with value, return it.
return std::move(opt.value());
}
void await_suspend(std::coroutine_handle<> handle) {
// If nullopt, stop running coroutine.
handle.destroy();
}
};
// co_yield
auto yield_value(const std::optional<T>& opt) {
if (!opt.has_value()) {
p.set_value(std::nullopt); // avoid std::future_error
}
return Awaiter{opt};
}
auto yield_value(std::optional<T>&& opt) {
if (!opt.has_value()) {
p.set_value(std::nullopt); // avoid std::future_error
}
return Awaiter{std::move(opt)};
}
// co_return expr;
// return value is in get_return_object() rather than return_value()
void return_value(T&& value) { p.set_value(std::forward<T>(value)); }
};
};
std::optional<int> get_odd(int i) {
if (i % 2 == 1) {
return i;
} else {
return std::nullopt;
}
}
std::future<std::optional<int>> compute(AsCoroutine, int a, int b) {
int x = co_yield get_odd(a);
cout << "a is valid." << endl;
int y = co_yield get_odd(b);
cout << "b is valid." << endl;
co_return x + y;
}
std::future<std::optional<std::vector<int>>> test1(AsCoroutine) {
// For lvalue and rvalue test.
using v = std::vector<int>;
using ov = std::optional<std::vector<int>>;
co_yield ov(v());
ov lv;
lv.emplace(v());
co_yield lv;
auto x = co_yield ov(v());
co_return x;
co_return v();
v abc;
co_return abc;
co_return co_yield ov({});
}
std::future<std::optional<std::unique_ptr<int>>> test2(AsCoroutine) {
using ou = std::optional<std::unique_ptr<int>>;
// co_yield ou();
// co_yield std::nullopt;
ou lv(std::make_unique<int>(3));
co_yield std::move(lv);
ou lv2(std::make_unique<int>(3));
co_return std::move(lv2.value());
}
int main() {
for (int b : {1, 2}) {
for (int a : {1, 2}) {
cout << "===" << a << " " << b << "===" << endl;
auto c = compute({}, a, b).get();
cout << c.value_or(-1) << endl;
cout << "=========" << endl << endl;
}
}
test1({}).get();
test2({}).get();
return 0;
}