-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.hpp
189 lines (165 loc) · 3.74 KB
/
generator.hpp
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
#pragma once
#include <optional>
#include <utility>
#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>
namespace generator {
struct ResumeFinishedGeneratorError : std::logic_error {
ResumeFinishedGeneratorError() :
std::logic_error("Unable to resume a finished generator") {
}
};
namespace __ {
struct _ {
};
} // namespace __
template<
typename Yield,
typename Resume,
typename... Args>
struct Generate;
template<
typename Yield,
typename Resume>
struct Ctx {
template<
typename Yield_,
typename Resume_,
typename... Args_>
friend struct Gen;
private:
bool done = false;
bool yielded = true;
std::optional <Yield> yieldVal;
std::optional <Resume> resumeVal;
std::mutex lock;
std::condition_variable cv;
public:
Resume operator=(Yield &&val) {
return yield(std::move(val));
}
Resume operator=(const Yield &val) {
return yield(val);
}
public:
Resume yield(Yield &&val) {
yieldVal = std::move(val);
return do_yield();
}
Resume yield(const Yield &val) {
yieldVal = val;
return do_yield();
}
private:
Resume do_yield() {
{
std::lock_guard <std::mutex> _(lock);
yielded = true;
}
cv.notify_one();
{
std::unique_lock <std::mutex> _(lock);
if (!done)
cv.wait(_, [this] { return !yielded || done; });
}
if (done) {
throw __::_();
}
return std::move(resumeVal).value();
}
};
template<
typename Yield,
typename Resume,
typename... Args>
struct Gen {
using CtxType = Ctx<Yield, Resume>;
using BodyType = std::function<Yield(
CtxType &,
Args...)>;
public:
Generate(const BodyType &body, Args... args) {
std::mutex mut;
std::unique_lock <std::mutex> _1(mut);
std::condition_variable cv1;
_ = std::thread(
[this, &cv1, &mut, body, args...] {
{
std::lock_guard <std::mutex> _(mut);
}
cv1.notify_one();
{
{
std::unique_lock <std::mutex> _(ctx.lock);
ctx.cv.wait(_, [this] { return !ctx.yielded || ctx.done; });
}
try {
if (!ctx.done) {
auto ret = body(ctx, args...);
ctx.done = true;
ctx.yield(ret);
}
}
catch (__::_) {
}
}
});
cv1.wait(_1);
}
~Generate() {
if (!ctx.done) {
ctx.done = true;
ctx.lock.unlock();
ctx.cv.notify_one();
}
_.value().join();
}
public:
Yield resume() {
ctx.resumeVal = std::nullopt;
return do_resume();
}
Yield resume(Resume &&val) {
ctx.resumeVal = std::move(val);
return do_resume();
}
Yield resume(const Resume &val) {
ctx.resumeVal = val;
return do_resume();
}
bool done() const {
return ctx.done;
}
private:
Yield do_resume() {
if (ctx.done)
throw ResumeFinishedGeneratorError();
{
std::lock_guard <std::mutex> _(ctx.lock);
ctx.yielded = false;
}
ctx.cv.notify_one();
{
std::unique_lock <std::mutex> _(ctx.lock);
ctx.cv.wait(_, [this] { return ctx.yielded; });
}
return std::move(ctx.yieldVal).value();
}
private:
CtxType ctx;
std::optional <std::thread> _;
};
} // namespace generator
#ifndef CXX_GENERATOR__GENERATOR_HPP
#define yield \
_ =
#define generator(Yield, Resume, Body) \
::gen::Gen<Yield, Resume>( \
[](::gen::Ctx<Yield, Resume> &_ ) -> Yield { \
const std::function<Yield()> body = ( Body ); \
return body(); \
} )
#define CXX_GENERATOR__GENERATOR_HPP
#endif //CXX_GENERATOR__GENERATOR_HPP