-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.h
More file actions
48 lines (44 loc) · 1.06 KB
/
base.h
File metadata and controls
48 lines (44 loc) · 1.06 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
#include <functional>
#include <optional>
#define YIELD(x) _codePointer = __LINE__; return x; case __LINE__:;
#define GENERATE _Result generate() override {switch (_codePointer) { default:;
#define TERMINATE return std::nullopt;}}
template<typename T>
class Generator
{
protected:
typedef std::optional<T> _Result;
int _codePointer = -1;
private:
class TerminateState {};
class StateIterator
{
std::function<_Result()> func;
_Result value;
public:
StateIterator(std::function<_Result()> f)
: func(f)
, value(func()) {}
StateIterator& operator++() {
value = func();
return *this;
}
T operator*() {
return *value;
}
bool operator!=(const TerminateState&) const
{
return value.has_value();
}
};
virtual _Result generate() = 0;
public:
StateIterator begin()
{
return StateIterator(std::bind(&Generator<T>::generate, this));
}
TerminateState end()
{
return {};
}
};