Path: blob/master/src/hotspot/share/utilities/enumIterator.hpp
40949 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_UTILITIES_ENUMITERATOR_HPP25#define SHARE_UTILITIES_ENUMITERATOR_HPP2627#include <type_traits>28#include <limits>29#include "memory/allStatic.hpp"30#include "metaprogramming/enableIf.hpp"31#include "metaprogramming/primitiveConversions.hpp"32#include "utilities/debug.hpp"3334// Iteration support for enums.35//36// E is enum type, U is underlying type of E.37//38// case 1:39// enum has sequential enumerators, with E first and E last (inclusive).40//41// case 2:42// enum has sequential values, with U start and U end (exclusive).43// This can be mapped onto case 1 by casting start/(end-1).44//45// case 3:46// enum has non-sequential non-duplicate enumerators47// Iteration could be supported via array or other sequence of enumerators.48// Don't bother.49//50// case 4:51// enum has non-sequential enumerators with duplicate values52// Not clear what iteration should mean in this case.53// Don't bother trying to figure this out.54//55//56// EnumRange -- defines the range of *one specific* iteration loop.57// EnumIterator -- the current point in the iteration loop.5859// Example:60//61// /* With range-base for (recommended) */62// for (auto index : EnumRange<vmSymbolID>{}) {63// ....64// }65//66// /* Without range-based for */67// constexpr EnumRange<vmSymbolID> vmSymbolsRange{};68// using vmSymbolsIterator = EnumIterator<vmSymbolID>;69// for (vmSymbolsIterator it = vmSymbolsRange.begin(); it != vmSymbolsRange.end(); ++it) {70// vmSymbolID index = *it; ....71// }7273// EnumeratorRange is a traits type supporting iteration over the enumerators of T.74// Specializations must provide static const data members named "_start" and "_end".75// The type of _start and _end must be the underlying type of T.76// _start is the inclusive lower bound of values in the range.77// _end is the exclusive upper bound of values in the range.78// The enumerators of T must have sequential values in that range.79template<typename T> struct EnumeratorRange;8081// Helper class for ENUMERATOR_RANGE and ENUMERATOR_VALUE_RANGE.82struct EnumeratorRangeImpl : AllStatic {83template<typename T> using Underlying = std::underlying_type_t<T>;8485// T not deduced to verify argument is of expected type.86template<typename T, typename U, ENABLE_IF(std::is_same<T, U>::value)>87static constexpr Underlying<T> start_value(U first) {88return static_cast<Underlying<T>>(first);89}9091// T not deduced to verify argument is of expected type.92template<typename T, typename U, ENABLE_IF(std::is_same<T, U>::value)>93static constexpr Underlying<T> end_value(U last) {94Underlying<T> value = static_cast<Underlying<T>>(last);95assert(value < std::numeric_limits<Underlying<T>>::max(), "end value overflow");96return static_cast<Underlying<T>>(value + 1);97}98};99100// Specialize EnumeratorRange<T>. Start and End must be constant expressions101// whose value is convertible to the underlying type of T. They provide the102// values of the required _start and _end members respectively.103#define ENUMERATOR_VALUE_RANGE(T, Start, End) \104template<> struct EnumeratorRange<T> { \105static constexpr EnumeratorRangeImpl::Underlying<T> _start{Start}; \106static constexpr EnumeratorRangeImpl::Underlying<T> _end{End}; \107};108109// Specialize EnumeratorRange<T>. First and Last must be constant expressions110// of type T. They determine the values of the required _start and _end members111// respectively. _start is the underlying value of First. _end is the underlying112// value of Last, plus one.113#define ENUMERATOR_RANGE(T, First, Last) \114ENUMERATOR_VALUE_RANGE(T, \115EnumeratorRangeImpl::start_value<T>(First), \116EnumeratorRangeImpl::end_value<T>(Last));117118// An internal helper class for EnumRange and EnumIterator, computing some119// additional information based on T and EnumeratorRange<T>, and performing120// or supporting various validity checks.121template<typename T>122class EnumIterationTraits : AllStatic {123using RangeType = EnumeratorRange<T>;124125public:126// The underlying type for T.127using Underlying = std::underlying_type_t<T>;128129// The value of the first enumerator of T.130static constexpr Underlying _start = RangeType::_start;131132// The one-past-the-end value for T.133static constexpr Underlying _end = RangeType::_end;134135static_assert(_start != _end, "empty range");136static_assert(_start <= _end, "invalid range"); // <= so only one failure when ==.137138// Verify value is in [start, end].139// The values for start and end default to _start and _end, respectively.140// The (deduced) type V is expected to be either T or Underlying.141template<typename V>142static constexpr void assert_in_range(V value,143V start = PrimitiveConversions::cast<V>(_start),144V end = PrimitiveConversions::cast<V>(_end)) {145assert(start <= value, "out of range");146assert(value <= end, "out of range");147}148149// Convert an enumerator value to the corresponding underlying type.150static constexpr Underlying underlying_value(T value) {151return static_cast<Underlying>(value);152}153154// Convert a value to the corresponding enumerator.155static constexpr T enumerator(Underlying value) {156return static_cast<T>(value);157}158};159160template<typename T>161class EnumIterator {162using Traits = EnumIterationTraits<T>;163164using Underlying = typename Traits::Underlying;165Underlying _value;166167constexpr void assert_in_bounds() const {168assert(_value < Traits::_end, "beyond the end");169}170171public:172using EnumType = T;173174// Return a beyond-the-end iterator.175constexpr EnumIterator() : _value(Traits::_end) {}176177// Return an iterator with the indicated value.178constexpr explicit EnumIterator(T value) :179_value(Traits::underlying_value(value))180{181Traits::assert_in_range(value);182}183184// True if the iterators designate the same enumeration value.185constexpr bool operator==(EnumIterator other) const {186return _value == other._value;187}188189// True if the iterators designate different enumeration values.190constexpr bool operator!=(EnumIterator other) const {191return _value != other._value;192}193194// Return the current value.195// precondition: this is not beyond the last enumerator.196constexpr T operator*() const {197assert_in_bounds();198return Traits::enumerator(_value);199}200201// Step this iterator to the next value.202// precondition: this is not beyond the last enumerator.203constexpr EnumIterator& operator++() {204assert_in_bounds();205++_value;206return *this;207}208209// Return a copy and step this iterator to the next value.210// precondition: this is not beyond the last enumerator.211constexpr EnumIterator operator++(int) {212assert_in_bounds();213EnumIterator result = *this;214++_value;215return result;216}217};218219template<typename T>220class EnumRange {221using Traits = EnumIterationTraits<T>;222using Underlying = typename Traits::Underlying;223224Underlying _start;225Underlying _end;226227constexpr void assert_not_empty() const {228assert(size() > 0, "empty range");229}230231public:232using EnumType = T;233using Iterator = EnumIterator<T>;234235// Default constructor gives the full range.236constexpr EnumRange() :237EnumRange(Traits::enumerator(Traits::_start)) {}238239// Range from start to the (exclusive) end of the enumerator range.240constexpr explicit EnumRange(T start) :241EnumRange(start, Traits::enumerator(Traits::_end)) {}242243// Range from start (inclusive) to end (exclusive).244// precondition: start <= end.245constexpr EnumRange(T start, T end) :246_start(Traits::underlying_value(start)),247_end(Traits::underlying_value(end))248{249Traits::assert_in_range(start);250Traits::assert_in_range(end);251assert(start <= end, "invalid range");252}253254// Return an iterator for the start of the range.255constexpr Iterator begin() const {256return Iterator(Traits::enumerator(_start));257}258259// Return an iterator for the end of the range.260constexpr Iterator end() const {261return Iterator(Traits::enumerator(_end));262}263264// Return the number of enumerator values in the range.265constexpr size_t size() const {266return static_cast<size_t>(_end - _start); // _end is exclusive267}268269// Return the first enumerator in the range.270// precondition: size() > 0271constexpr T first() const {272assert_not_empty();273return Traits::enumerator(_start);274}275276// Return the last enumerator in the range.277// precondition: size() > 0278constexpr T last() const {279assert_not_empty();280return Traits::enumerator(_end - 1);281}282283// Convert value to a zero-based index into the range [first(), last()].284// precondition: first() <= value && value <= last()285constexpr size_t index(T value) const {286Traits::assert_in_range(value, first(), last());287return static_cast<size_t>(Traits::underlying_value(value) - _start);288}289};290291#endif // SHARE_UTILITIES_ENUMITERATOR_HPP292293294