forked from alibaba/PhotonLibOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.h
208 lines (190 loc) · 5.21 KB
/
generator.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
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <cassert>
#include <type_traits>
template<typename Derived>
class __CRTP // the Curiously Recurring Template Pattern
{
protected:
// static_assert(std::is_base_of<__CRTP, Derived>::value, "...");
Derived* THIS()
{
return (Derived*)this;
}
const Derived* THIS() const
{
return (const Derived*)this;
}
};
template<typename Derived, typename ValueType, typename IteratorPayload_ = void>
class Generator : public __CRTP<Derived>
{
public:
using __CRTP<Derived>::THIS;
struct IteratorPayloadVoid
{
int _;
IteratorPayloadVoid(const Generator*) { } // for begin()
IteratorPayloadVoid() { } // for end()
};
using IteratorPayload = typename std::conditional<
std::is_same<IteratorPayload_, void>::value,
IteratorPayloadVoid, IteratorPayload_>::type;
struct Iterator : public __CRTP<Iterator>
{
using __CRTP<Iterator>::THIS;
// constructor for begin()
Iterator(const Derived* generator) :
_generator(generator), payload(generator)
{
if (!_generator->has_next(THIS()))
_generator = nullptr;
}
// constructor for end()
Iterator() : _generator(nullptr), payload() { }
void operator++()
{
if (_generator->has_next(THIS())) {
_generator->next(THIS());
i++;
} else {
_generator = nullptr;
}
}
void operator++(int)
{
++(*this);
}
ValueType operator*() const
{
return _generator->get(THIS());
}
bool operator==(const Iterator& rhs)
{
if (_generator) {
return rhs._generator && i == rhs.i;
} else {
return !rhs._generator;
}
}
bool operator!=(const Iterator& rhs)
{
return !(*this == rhs);
}
const Derived* _generator;
int i = 0; // assistant for equality test
IteratorPayload payload;
};
// either form of these functions must be overrided in derived classes
bool has_next(const Iterator* it) const
{
return THIS()->payload_has_next(it->payload);
}
bool payload_has_next(const IteratorPayload&) const
{
assert(false);
return false;
}
void next(Iterator* it) const
{
THIS()->payload_next(it->payload);
}
void payload_next(IteratorPayload&) const
{
assert(false);
}
ValueType get(const Iterator* it) const
{
return THIS()->payload_get(it->payload);
}
ValueType payload_get(const IteratorPayload&) const
{
assert(false);
return ValueType();
}
// optionally overrided in derived classes (if Iterator sub-classed)
Iterator begin() const
{
return Iterator(THIS());
}
Iterator end() const
{
return Iterator();
}
};
//====================================================================
inline void ___example_of_generator____()
{
class example_generator1 : public Generator<example_generator1, int>
{
public:
int n;
example_generator1(int N) : n(N) { }
bool has_next(const Iterator* it) const
{
return it->i < n;
}
void next(Iterator* it) const
{
assert(has_next(it));
// it->i++; // i will be increased automatically
}
int get(const Iterator* it) const
{
return it->i;
}
};
{
int i = 0; (void)i;
for (auto x: example_generator1(10))
{
assert(x == i); i++;
(void)x;
}
}
class example_generator2;
struct IteratorPayload
{
int I = 0;
IteratorPayload(const example_generator2*) { } // for begin()
IteratorPayload() { } // for end()
};
class example_generator2 : public Generator<example_generator2, int, IteratorPayload>
{
public:
int n;
example_generator2(int N) : n(N) { }
bool payload_has_next(const IteratorPayload& payload) const
{
return payload.I < n;
}
void payload_next(IteratorPayload& payload) const
{
assert(payload_has_next(payload));
payload.I++;
}
int payload_get(const IteratorPayload& payload) const
{
return payload.I;
}
};
{
int i = 0; (void)i;
for (auto x: example_generator2(10))
{
assert(x == i); i++;
(void)x;
}
}
}