-
Notifications
You must be signed in to change notification settings - Fork 3
/
Actions.h
306 lines (252 loc) · 7.88 KB
/
Actions.h
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifndef _ACTIONS_H_
#define _ACTIONS_H_
#include <vector>
#include "Overload.h"
#include "Item.h"
template<typename T>
struct Listener {
using Result = T;
virtual ~Listener() = default;
virtual void onFail(std::string cause) = 0;
virtual void onResult(T result) = 0;
};
template<typename T>
struct DummyListener : Listener<T> {
void onFail(std::string cause) override {}
void onResult(T result) override {}
};
template<typename T>
using SharedListener = std::shared_ptr<Listener<T>>;
template<typename T>
class Promise : public Listener<T> {
SharedListener<T> next;
public:
void onFail(std::string cause) override {
if (!next) throw std::logic_error("broken chain");
next->onFail(std::move(cause));
}
void onResult(T result) override {
if (!next) throw std::logic_error("broken chain");
next->onResult(std::move(result));
}
void listen(SharedListener<T> x) {
if (next) throw std::logic_error("already listened");
next = std::move(x);
}
template<typename Fn>
auto map(std::weak_ptr<std::monostate> alive, Fn fn) {
using To = std::invoke_result_t<Fn, T>;
auto result(std::make_shared<Promise<To>>());
struct Impl : Listener<T> {
SharedListener<To> to;
std::weak_ptr<std::monostate> alive;
Fn fn;
Impl(SharedListener<To> to, std::weak_ptr<std::monostate> alive, Fn fn)
:to(std::move(to)), alive(std::move(alive)), fn(std::move(fn)) {}
void onFail(std::string cause) override {
to->onFail(std::move(cause));
}
void onResult(T result) override {
if (alive.expired())
to->onFail("node died");
else
to->onResult(fn(std::move(result)));
}
};
listen(std::make_shared<Impl>(result, std::move(alive), std::move(fn)));
return result;
}
template<typename To>
auto mapTo(To value) {
auto result(std::make_shared<Promise<To>>());
struct Impl : Listener<T> {
SharedListener<To> to;
To value;
Impl(SharedListener<To> to, To value)
:to(std::move(to)), value(std::move(value)) {}
void onFail(std::string cause) override {
to->onFail(std::move(cause));
}
void onResult(T) override {
to->onResult(std::move(value));
}
};
listen(std::make_shared<Impl>(result, std::move(value)));
return result;
}
template<typename Fn>
auto then(std::weak_ptr<std::monostate> alive, Fn fn) {
using To = typename std::invoke_result_t<Fn, T>::element_type::Result;
auto result(std::make_shared<Promise<To>>());
struct Impl : Listener<T> {
SharedListener<To> to;
std::weak_ptr<std::monostate> alive;
Fn fn;
Impl(SharedListener<To> to, std::weak_ptr<std::monostate> alive, Fn fn)
:to(std::move(to)), alive(std::move(alive)), fn(std::move(fn)) {}
void onFail(std::string cause) override {
to->onFail(std::move(cause));
}
void onResult(T result) override {
if (alive.expired())
to->onFail("node died");
else
fn(std::move(result))->listen(std::move(to));
}
};
listen(std::make_shared<Impl>(result, std::move(alive), std::move(fn)));
return result;
}
template<typename Fn>
std::shared_ptr<Promise<T>> finally(std::weak_ptr<std::monostate> alive, Fn fn) {
auto result(std::make_shared<Promise<T>>());
struct Impl : Listener<T> {
SharedListener<T> to;
std::weak_ptr<std::monostate> alive;
Fn fn;
Impl(SharedListener<T> to, std::weak_ptr<std::monostate> alive, Fn fn)
:to(std::move(to)), alive(std::move(alive)), fn(std::move(fn)) {}
void onFail(std::string cause) override {
if (!alive.expired())
fn();
to->onFail(std::move(cause));
}
void onResult(T result) override {
if (!alive.expired())
fn();
to->onResult(std::move(result));
}
};
listen(std::make_shared<Impl>(result, std::move(alive), std::move(fn)));
return result;
}
static std::shared_ptr<Promise<std::vector<T>>> all(const std::vector<std::shared_ptr<Promise<T>>> &xs) {
if (xs.empty()) throw std::runtime_error("waiting for nothing");
struct Context {
std::shared_ptr<Promise<std::vector<T>>> to{std::make_shared<Promise<std::vector<T>>>()};
std::variant<std::vector<T>, std::string> result;
size_t nRemaining;
explicit Context(size_t nTotal)
:result(std::in_place_type<std::vector<T>>, nTotal), nRemaining(nTotal) {}
void decreaseRemaining() {
if (!--nRemaining) {
std::visit(Overload{
[this](std::vector<T> &xs) {
to->onResult(std::move(xs));
},
[this](std::string &x) {
to->onFail(std::move(x));
}
}, result);
}
}
void onResult(size_t which, T &x) {
std::visit(Overload{
[&](std::vector<T> &xs) {
xs[which] = std::move(x);
},
[](std::string &) {}
}, result);
decreaseRemaining();
}
void onFail(std::string &reason) {
std::visit(Overload{
[&](std::vector<T> &xs) {
result = std::move(reason);
},
[&](std::string &x) {
x += "; " + reason;
}
}, result);
decreaseRemaining();
}
};
struct Impl : Listener<T> {
std::shared_ptr<Context> context;
size_t index;
Impl(const std::shared_ptr<Context> &context, size_t index)
:context(context), index(index) {}
void onFail(std::string cause) override {
context->onFail(cause);
}
void onResult(T result) override {
context->onResult(index, result);
}
};
auto context(std::make_shared<Context>(xs.size()));
for (size_t i = 0; i < xs.size(); ++i) {
xs[i]->listen(std::make_shared<Impl>(context, i));
}
return context->to;
}
};
template<typename T>
using SharedPromise = std::shared_ptr<Promise<T>>;
template<typename F>
inline SharedPromise<std::monostate> scheduleTrivialPromise(F &dispatcher) {
auto result(std::make_shared<Promise<std::monostate>>());
dispatcher([result]() { result->onResult({}); });
return result;
}
template<typename T, typename F>
inline SharedPromise<T> scheduleFailingPromise(F &dispatcher, std::string reason) {
auto result(std::make_shared<Promise<T>>());
dispatcher([result, reason(std::move(reason))]() { result->onFail(std::move(reason)); });
return result;
}
namespace Actions {
enum {
bottom = 0, down = 0, yn = 0,
top = 1, up = 1, yp = 1,
back = 2, north = 2, zn = 2,
front = 3, south = 3, zp = 3,
right = 4, west = 4, xn = 4,
left = 5, east = 5, xp = 5
};
struct Base : Listener<SValue> {
virtual void dump(STable&) = 0;
};
template<typename T>
struct Impl : Base, Promise<T> {
void onFail(std::string cause) override {
Promise<T>::onFail(std::move(cause));
}
};
struct ImplUnit : Impl<std::monostate> {
void onResult(SValue) override {
Promise<std::monostate>::onResult(std::monostate());
}
};
struct Print : ImplUnit {
std::string text;
uint32_t color{0xffffffu};
double beep{-1.};
void dump(STable&) override;
};
struct List : Impl<std::vector<SharedItemStack>> {
std::string inv;
int side;
void dump(STable&) override;
void onResult(SValue) override;
};
struct ListME : Impl<std::vector<SharedItemStack>> {
std::string inv;
void dump(STable&) override;
void onResult(SValue) override;
};
struct XferME : ImplUnit {
std::string me, inv;
SValue filter;
std::vector<SValue> args;
int size, entry;
void dump(STable&) override;
};
struct Call : Impl<SValue> {
std::string inv, fn;
std::vector<SValue> args;
void dump(STable&) override;
void onResult(SValue) override;
};
}
using SharedAction = std::shared_ptr<Actions::Base>;
#endif