Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/utilities/enumIterator.hpp
40949 views
1
/*
2
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#ifndef SHARE_UTILITIES_ENUMITERATOR_HPP
26
#define SHARE_UTILITIES_ENUMITERATOR_HPP
27
28
#include <type_traits>
29
#include <limits>
30
#include "memory/allStatic.hpp"
31
#include "metaprogramming/enableIf.hpp"
32
#include "metaprogramming/primitiveConversions.hpp"
33
#include "utilities/debug.hpp"
34
35
// Iteration support for enums.
36
//
37
// E is enum type, U is underlying type of E.
38
//
39
// case 1:
40
// enum has sequential enumerators, with E first and E last (inclusive).
41
//
42
// case 2:
43
// enum has sequential values, with U start and U end (exclusive).
44
// This can be mapped onto case 1 by casting start/(end-1).
45
//
46
// case 3:
47
// enum has non-sequential non-duplicate enumerators
48
// Iteration could be supported via array or other sequence of enumerators.
49
// Don't bother.
50
//
51
// case 4:
52
// enum has non-sequential enumerators with duplicate values
53
// Not clear what iteration should mean in this case.
54
// Don't bother trying to figure this out.
55
//
56
//
57
// EnumRange -- defines the range of *one specific* iteration loop.
58
// EnumIterator -- the current point in the iteration loop.
59
60
// Example:
61
//
62
// /* With range-base for (recommended) */
63
// for (auto index : EnumRange<vmSymbolID>{}) {
64
// ....
65
// }
66
//
67
// /* Without range-based for */
68
// constexpr EnumRange<vmSymbolID> vmSymbolsRange{};
69
// using vmSymbolsIterator = EnumIterator<vmSymbolID>;
70
// for (vmSymbolsIterator it = vmSymbolsRange.begin(); it != vmSymbolsRange.end(); ++it) {
71
// vmSymbolID index = *it; ....
72
// }
73
74
// EnumeratorRange is a traits type supporting iteration over the enumerators of T.
75
// Specializations must provide static const data members named "_start" and "_end".
76
// The type of _start and _end must be the underlying type of T.
77
// _start is the inclusive lower bound of values in the range.
78
// _end is the exclusive upper bound of values in the range.
79
// The enumerators of T must have sequential values in that range.
80
template<typename T> struct EnumeratorRange;
81
82
// Helper class for ENUMERATOR_RANGE and ENUMERATOR_VALUE_RANGE.
83
struct EnumeratorRangeImpl : AllStatic {
84
template<typename T> using Underlying = std::underlying_type_t<T>;
85
86
// T not deduced to verify argument is of expected type.
87
template<typename T, typename U, ENABLE_IF(std::is_same<T, U>::value)>
88
static constexpr Underlying<T> start_value(U first) {
89
return static_cast<Underlying<T>>(first);
90
}
91
92
// T not deduced to verify argument is of expected type.
93
template<typename T, typename U, ENABLE_IF(std::is_same<T, U>::value)>
94
static constexpr Underlying<T> end_value(U last) {
95
Underlying<T> value = static_cast<Underlying<T>>(last);
96
assert(value < std::numeric_limits<Underlying<T>>::max(), "end value overflow");
97
return static_cast<Underlying<T>>(value + 1);
98
}
99
};
100
101
// Specialize EnumeratorRange<T>. Start and End must be constant expressions
102
// whose value is convertible to the underlying type of T. They provide the
103
// values of the required _start and _end members respectively.
104
#define ENUMERATOR_VALUE_RANGE(T, Start, End) \
105
template<> struct EnumeratorRange<T> { \
106
static constexpr EnumeratorRangeImpl::Underlying<T> _start{Start}; \
107
static constexpr EnumeratorRangeImpl::Underlying<T> _end{End}; \
108
};
109
110
// Specialize EnumeratorRange<T>. First and Last must be constant expressions
111
// of type T. They determine the values of the required _start and _end members
112
// respectively. _start is the underlying value of First. _end is the underlying
113
// value of Last, plus one.
114
#define ENUMERATOR_RANGE(T, First, Last) \
115
ENUMERATOR_VALUE_RANGE(T, \
116
EnumeratorRangeImpl::start_value<T>(First), \
117
EnumeratorRangeImpl::end_value<T>(Last));
118
119
// An internal helper class for EnumRange and EnumIterator, computing some
120
// additional information based on T and EnumeratorRange<T>, and performing
121
// or supporting various validity checks.
122
template<typename T>
123
class EnumIterationTraits : AllStatic {
124
using RangeType = EnumeratorRange<T>;
125
126
public:
127
// The underlying type for T.
128
using Underlying = std::underlying_type_t<T>;
129
130
// The value of the first enumerator of T.
131
static constexpr Underlying _start = RangeType::_start;
132
133
// The one-past-the-end value for T.
134
static constexpr Underlying _end = RangeType::_end;
135
136
static_assert(_start != _end, "empty range");
137
static_assert(_start <= _end, "invalid range"); // <= so only one failure when ==.
138
139
// Verify value is in [start, end].
140
// The values for start and end default to _start and _end, respectively.
141
// The (deduced) type V is expected to be either T or Underlying.
142
template<typename V>
143
static constexpr void assert_in_range(V value,
144
V start = PrimitiveConversions::cast<V>(_start),
145
V end = PrimitiveConversions::cast<V>(_end)) {
146
assert(start <= value, "out of range");
147
assert(value <= end, "out of range");
148
}
149
150
// Convert an enumerator value to the corresponding underlying type.
151
static constexpr Underlying underlying_value(T value) {
152
return static_cast<Underlying>(value);
153
}
154
155
// Convert a value to the corresponding enumerator.
156
static constexpr T enumerator(Underlying value) {
157
return static_cast<T>(value);
158
}
159
};
160
161
template<typename T>
162
class EnumIterator {
163
using Traits = EnumIterationTraits<T>;
164
165
using Underlying = typename Traits::Underlying;
166
Underlying _value;
167
168
constexpr void assert_in_bounds() const {
169
assert(_value < Traits::_end, "beyond the end");
170
}
171
172
public:
173
using EnumType = T;
174
175
// Return a beyond-the-end iterator.
176
constexpr EnumIterator() : _value(Traits::_end) {}
177
178
// Return an iterator with the indicated value.
179
constexpr explicit EnumIterator(T value) :
180
_value(Traits::underlying_value(value))
181
{
182
Traits::assert_in_range(value);
183
}
184
185
// True if the iterators designate the same enumeration value.
186
constexpr bool operator==(EnumIterator other) const {
187
return _value == other._value;
188
}
189
190
// True if the iterators designate different enumeration values.
191
constexpr bool operator!=(EnumIterator other) const {
192
return _value != other._value;
193
}
194
195
// Return the current value.
196
// precondition: this is not beyond the last enumerator.
197
constexpr T operator*() const {
198
assert_in_bounds();
199
return Traits::enumerator(_value);
200
}
201
202
// Step this iterator to the next value.
203
// precondition: this is not beyond the last enumerator.
204
constexpr EnumIterator& operator++() {
205
assert_in_bounds();
206
++_value;
207
return *this;
208
}
209
210
// Return a copy and step this iterator to the next value.
211
// precondition: this is not beyond the last enumerator.
212
constexpr EnumIterator operator++(int) {
213
assert_in_bounds();
214
EnumIterator result = *this;
215
++_value;
216
return result;
217
}
218
};
219
220
template<typename T>
221
class EnumRange {
222
using Traits = EnumIterationTraits<T>;
223
using Underlying = typename Traits::Underlying;
224
225
Underlying _start;
226
Underlying _end;
227
228
constexpr void assert_not_empty() const {
229
assert(size() > 0, "empty range");
230
}
231
232
public:
233
using EnumType = T;
234
using Iterator = EnumIterator<T>;
235
236
// Default constructor gives the full range.
237
constexpr EnumRange() :
238
EnumRange(Traits::enumerator(Traits::_start)) {}
239
240
// Range from start to the (exclusive) end of the enumerator range.
241
constexpr explicit EnumRange(T start) :
242
EnumRange(start, Traits::enumerator(Traits::_end)) {}
243
244
// Range from start (inclusive) to end (exclusive).
245
// precondition: start <= end.
246
constexpr EnumRange(T start, T end) :
247
_start(Traits::underlying_value(start)),
248
_end(Traits::underlying_value(end))
249
{
250
Traits::assert_in_range(start);
251
Traits::assert_in_range(end);
252
assert(start <= end, "invalid range");
253
}
254
255
// Return an iterator for the start of the range.
256
constexpr Iterator begin() const {
257
return Iterator(Traits::enumerator(_start));
258
}
259
260
// Return an iterator for the end of the range.
261
constexpr Iterator end() const {
262
return Iterator(Traits::enumerator(_end));
263
}
264
265
// Return the number of enumerator values in the range.
266
constexpr size_t size() const {
267
return static_cast<size_t>(_end - _start); // _end is exclusive
268
}
269
270
// Return the first enumerator in the range.
271
// precondition: size() > 0
272
constexpr T first() const {
273
assert_not_empty();
274
return Traits::enumerator(_start);
275
}
276
277
// Return the last enumerator in the range.
278
// precondition: size() > 0
279
constexpr T last() const {
280
assert_not_empty();
281
return Traits::enumerator(_end - 1);
282
}
283
284
// Convert value to a zero-based index into the range [first(), last()].
285
// precondition: first() <= value && value <= last()
286
constexpr size_t index(T value) const {
287
Traits::assert_in_range(value, first(), last());
288
return static_cast<size_t>(Traits::underlying_value(value) - _start);
289
}
290
};
291
292
#endif // SHARE_UTILITIES_ENUMITERATOR_HPP
293
294