generated from cpp-best-practices/gui_starter_template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_coroutines.hpp
201 lines (167 loc) · 4.88 KB
/
lambda_coroutines.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
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef LAMBDA_COROUTINES_HPP
#define LAMBDA_COROUTINES_HPP
#include <cstddef>
#include <functional>
#include <optional>
#include <utility>
// because these are macros we cannot meaningfully use namespaces
// https://stackoverflow.com/questions/57137351/line-is-not-constexpr-in-msvc
// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
#define LAMBDA_CO_CONCATENATE_(a, b) a##b
#define LAMBDA_CO_CONCATENATE(a, b) LAMBDA_CO_CONCATENATE_(a, b)
#define LAMBDA_CO_CONSTEXPR_LINE long(LAMBDA_CO_CONCATENATE(__LINE__, U))
namespace lambda_coroutines {
// Internal tool for checking lambda coroutine requirements
template<typename ParamType>
constexpr void lambda_co_status_check(ParamType &value)
{
static_assert(std::is_integral_v<ParamType>,
"integral state value required");
static_assert(!std::is_const_v<ParamType>, "mutable lambda required");
static_assert(sizeof(value) >= 4, "state value must be at least 32 bits");
}
#define lambda_co_begin(state_variable) \
::lambda_coroutines::lambda_co_status_check(state_variable); \
auto &coroutine_lambda_internal_state_ref = state_variable; \
switch (state_variable) { \
default: \
case 0:
#define lambda_co_return(return_value) \
} \
return return_value
#define lambda_co_yield(yield_value) \
coroutine_lambda_internal_state_ref = LAMBDA_CO_CONSTEXPR_LINE; \
return yield_value; \
case LAMBDA_CO_CONSTEXPR_LINE:
#define lambda_co_end() }
template<typename Lambda>
[[nodiscard]] constexpr auto range(
Lambda lambda,
std::size_t skip_ = 0,
std::optional<std::size_t> length_ = {},
std::size_t stride_ = 1)
{
struct Range
{
Lambda generator;
std::optional<std::size_t> length;
std::size_t stride;
std::size_t position = 0;
struct Iterator
{
std::reference_wrapper<Lambda> lambda;
std::reference_wrapper<std::size_t> position;
std::optional<std::size_t> length;
std::size_t stride = 1;
bool is_end = false;
[[nodiscard]] constexpr bool has_more() const noexcept
{
if (length && (position.get() >= length.value())) {
return false;
}
return true;
}
constexpr bool increment_position()
{
if (has_more()) {
++position.get();
return true;
} else {
return false;
}
}
constexpr auto &operator++()
{
for (std::size_t i = 1; i < stride; ++i) {
if (increment_position()) {
lambda();
}
}
return *this;
}
constexpr bool operator==(const Iterator &rhs) const
{
// return false;
if (is_end && !rhs.has_more()) {
return true;
}
if (rhs.is_end && !has_more()) {
return true;
}
if (rhs.is_end && is_end) {
return true;
}
return false;
}
constexpr bool operator!=(const Iterator &rhs) const
{
return !(*this == rhs);
}
[[nodiscard]] constexpr decltype(auto) operator*()
{
increment_position();
return lambda();
}
};
[[nodiscard]] constexpr auto begin() noexcept
{
return Iterator{ generator, position, length, stride, false };
}
[[nodiscard]] constexpr auto end() noexcept
{
return Iterator{ generator, position, length, stride, true };
}
};
for (std::size_t i = 0; i < skip_; ++i) {
lambda();
}
return Range{ std::move(lambda), length_, stride_ };
}
template<typename Lambda>
[[nodiscard]] constexpr auto while_has_value(Lambda lambda)
{
struct Range
{
Lambda generator;
struct Iterator
{
std::reference_wrapper<Lambda> lambda;
std::invoke_result_t<Lambda> current_value{};
Iterator(Lambda &lambda_, bool is_end_)
: lambda(lambda_), current_value([&]() {
return is_end_ ? decltype(current_value){} : lambda();
}()) {}
constexpr auto &operator++()
{
current_value = lambda();
return *this;
}
constexpr bool operator==(const Iterator &rhs) const
{
if (!current_value && !rhs.current_value) {
return true;
}
return false;
}
constexpr bool operator!=(const Iterator &rhs) const
{
return !(*this == rhs);
}
[[nodiscard]] constexpr decltype(auto) operator*()
{
return current_value.value();
}
};
[[nodiscard]] constexpr auto begin() noexcept
{
return Iterator{ generator, false };
}
[[nodiscard]] constexpr auto end() noexcept
{
return Iterator{ generator, true };
}
};
return Range{ std::move(lambda) };
}
}// namespace lambda_coroutines
#endif