forked from infinit/elle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.hxx
175 lines (152 loc) · 3.95 KB
/
Generator.hxx
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
#include <type_traits>
namespace elle
{
namespace reactor
{
/*----.
| End |
`----*/
template <typename T>
Generator<T>::End::End(Generator<T> const& g)
: elle::Error(elle::sprintf("%s exhausted", g))
{}
/*-------------.
| Construction |
`-------------*/
template <typename T>
template <typename Driver>
Generator<T>::Generator(Driver driver)
{
using Signature = std::function<auto (yielder const&) -> void>;
static_assert(std::is_constructible<Signature, Driver>::value, "");
ELLE_LOG_COMPONENT("elle.reactor.Generator");
auto yield = [this] (T elt) { this->_results.put(std::move(elt)); };
this->_thread.reset(
new Thread("generator",
[this, driver, yield]
{
try
{
driver(yield);
}
catch (...)
{
ELLE_TRACE("%s: handle exception: %s",
this, elle::exception_string());
this->_exception = std::current_exception();
}
this->_results.put({});
}));
}
template <typename T>
Generator<T>::Generator(Generator<T>&& generator)
: _results(std::move(generator._results))
, _thread(std::move(generator._thread))
{}
template <typename T>
Generator<T>::~Generator()
{
ELLE_LOG_COMPONENT("elle.reactor.Generator");
ELLE_TRACE("%s: destruct", this);
}
/*--------.
| Content |
`--------*/
template <typename T>
T
Generator<T>::iterator::operator *() const
{
assert(!this->_fetch);
return std::move(this->_value.get());
}
/*---------.
| Iterator |
`---------*/
template <typename T>
Generator<T>::iterator::iterator()
: _generator(nullptr)
, _fetch(true)
{}
template <typename T>
Generator<T>::iterator::iterator(Generator<T>& generator)
: _generator(&generator)
, _fetch(true)
{}
template <typename T>
bool
Generator<T>::iterator::operator !=(iterator const& other) const
{
assert(other._generator == nullptr);
if (this->_fetch)
{
this->_value = this->_generator->_results.get();
this->_fetch = false;
}
if (this->_value)
return true;
else if (this->_generator->_exception)
std::rethrow_exception(this->_generator->_exception);
else
return false;
}
template <typename T>
bool
Generator<T>::iterator::operator ==(iterator const& other) const
{
return !(*this != other);
}
template <typename T>
typename Generator<T>::iterator&
Generator<T>::iterator::operator ++()
{
this->_fetch = true;
this->_value.reset();
return *this;
}
template <typename T>
void
Generator<T>::iterator::operator ++(int)
{
++*this;
}
template <typename T>
T
Generator<T>::next()
{
if (auto res = this->_results.get())
return *res;
else
throw End(*this);
}
template <typename T>
Generator<T>
generator(std::function<void (yielder<T> const&)> const& driver)
{
return Generator<T>(driver);
}
template <typename T>
typename Generator<T>::iterator
Generator<T>::begin()
{
return typename Generator<T>::iterator(*this);
}
template <typename T>
typename Generator<T>::iterator
Generator<T>::end()
{
return typename Generator<T>::iterator();
}
template <typename T>
typename Generator<T>::const_iterator
Generator<T>::begin() const
{
return typename Generator<T>::iterator(elle::unconst(*this));
}
template <typename T>
typename Generator<T>::const_iterator
Generator<T>::end() const
{
return typename Generator<T>::iterator();
}
}
}