2015-03-23 22:06:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* Iterator over contiguous enum values */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements generator functions that create a range to iterate over the values
|
|
|
|
* of a scoped or unscoped enum. Unlike IntegerRange, which can only function on
|
|
|
|
* the underlying integral type, the elements of the generated sequence will
|
|
|
|
* have the type of the enum in question.
|
|
|
|
*
|
|
|
|
* Note that the enum values should be contiguous in the iterated range;
|
|
|
|
* unfortunately there exists no way for EnumeratedRange to enforce this
|
|
|
|
* either dynamically or at compile time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef mozilla_EnumeratedRange_h
|
|
|
|
#define mozilla_EnumeratedRange_h
|
|
|
|
|
2020-01-27 11:01:55 +03:00
|
|
|
#include <limits>
|
2016-07-28 12:48:50 +03:00
|
|
|
#include <type_traits>
|
|
|
|
|
2021-09-15 01:06:22 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-07-02 06:31:07 +03:00
|
|
|
#include "mozilla/ReverseIterator.h"
|
2015-03-23 22:06:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumTypeT>
|
2015-03-23 22:06:00 +03:00
|
|
|
class EnumeratedIterator {
|
|
|
|
public:
|
2016-07-28 12:48:50 +03:00
|
|
|
typedef typename std::underlying_type<EnumTypeT>::type IntTypeT;
|
|
|
|
|
2015-03-23 22:06:00 +03:00
|
|
|
template <typename EnumType>
|
2020-01-27 11:01:47 +03:00
|
|
|
constexpr explicit EnumeratedIterator(EnumType aCurrent)
|
|
|
|
: mCurrent(aCurrent) {}
|
2015-03-23 22:06:00 +03:00
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
explicit EnumeratedIterator(const EnumeratedIterator<EnumType>& aOther)
|
2015-03-23 22:06:00 +03:00
|
|
|
: mCurrent(aOther.mCurrent) {}
|
|
|
|
|
2015-07-02 06:31:07 +03:00
|
|
|
EnumTypeT operator*() const { return mCurrent; }
|
2015-03-23 22:06:00 +03:00
|
|
|
|
|
|
|
/* Increment and decrement operators */
|
|
|
|
|
|
|
|
EnumeratedIterator& operator++() {
|
|
|
|
mCurrent = EnumTypeT(IntTypeT(mCurrent) + IntTypeT(1));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
EnumeratedIterator& operator--() {
|
|
|
|
mCurrent = EnumTypeT(IntTypeT(mCurrent) - IntTypeT(1));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
EnumeratedIterator operator++(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
mCurrent = EnumTypeT(IntTypeT(mCurrent) + IntTypeT(1));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EnumeratedIterator operator--(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
mCurrent = EnumTypeT(IntTypeT(mCurrent) - IntTypeT(1));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comparison operators */
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
friend bool operator==(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2);
|
|
|
|
template <typename EnumType>
|
|
|
|
friend bool operator!=(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2);
|
|
|
|
template <typename EnumType>
|
|
|
|
friend bool operator<(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2);
|
|
|
|
template <typename EnumType>
|
|
|
|
friend bool operator<=(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2);
|
|
|
|
template <typename EnumType>
|
|
|
|
friend bool operator>(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2);
|
|
|
|
template <typename EnumType>
|
|
|
|
friend bool operator>=(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2);
|
2015-03-23 22:06:00 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
EnumTypeT mCurrent;
|
|
|
|
};
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
bool operator==(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2) {
|
2015-03-23 22:06:00 +03:00
|
|
|
return aIter1.mCurrent == aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
bool operator!=(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2) {
|
2015-03-23 22:06:00 +03:00
|
|
|
return aIter1.mCurrent != aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
bool operator<(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2) {
|
2015-03-23 22:06:00 +03:00
|
|
|
return aIter1.mCurrent < aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
bool operator<=(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2) {
|
2015-03-23 22:06:00 +03:00
|
|
|
return aIter1.mCurrent <= aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
bool operator>(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2) {
|
2015-03-23 22:06:00 +03:00
|
|
|
return aIter1.mCurrent > aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
bool operator>=(const EnumeratedIterator<EnumType>& aIter1,
|
|
|
|
const EnumeratedIterator<EnumType>& aIter2) {
|
2015-03-23 22:06:00 +03:00
|
|
|
return aIter1.mCurrent >= aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumTypeT>
|
2015-03-23 22:06:00 +03:00
|
|
|
class EnumeratedRange {
|
|
|
|
public:
|
2016-07-28 12:48:50 +03:00
|
|
|
typedef EnumeratedIterator<EnumTypeT> iterator;
|
|
|
|
typedef EnumeratedIterator<EnumTypeT> const_iterator;
|
2015-03-23 22:06:00 +03:00
|
|
|
typedef ReverseIterator<iterator> reverse_iterator;
|
|
|
|
typedef ReverseIterator<const_iterator> const_reverse_iterator;
|
|
|
|
|
|
|
|
template <typename EnumType>
|
2020-01-27 11:01:47 +03:00
|
|
|
constexpr EnumeratedRange(EnumType aBegin, EnumType aEnd)
|
2015-03-23 22:06:00 +03:00
|
|
|
: mBegin(aBegin), mEnd(aEnd) {}
|
|
|
|
|
|
|
|
iterator begin() const { return iterator(mBegin); }
|
|
|
|
const_iterator cbegin() const { return begin(); }
|
|
|
|
iterator end() const { return iterator(mEnd); }
|
|
|
|
const_iterator cend() const { return end(); }
|
|
|
|
reverse_iterator rbegin() const { return reverse_iterator(mEnd); }
|
|
|
|
const_reverse_iterator crbegin() const { return rbegin(); }
|
|
|
|
reverse_iterator rend() const { return reverse_iterator(mBegin); }
|
|
|
|
const_reverse_iterator crend() const { return rend(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
EnumTypeT mBegin;
|
|
|
|
EnumTypeT mEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
// Enums can have an unsigned underlying type, which makes some of the
|
|
|
|
// comparisons below always true or always false. Temporarily disable
|
|
|
|
// -Wtype-limits to avoid breaking -Werror builds.
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wtype-limits"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create a range to iterate from aBegin to aEnd, exclusive.
|
2016-07-28 12:48:50 +03:00
|
|
|
template <typename EnumType>
|
2020-01-27 11:01:47 +03:00
|
|
|
constexpr detail::EnumeratedRange<EnumType> MakeEnumeratedRange(EnumType aBegin,
|
|
|
|
EnumType aEnd) {
|
2015-03-23 22:06:00 +03:00
|
|
|
MOZ_ASSERT(aBegin <= aEnd, "Cannot generate invalid, unbounded range!");
|
2016-07-28 12:48:50 +03:00
|
|
|
return detail::EnumeratedRange<EnumType>(aBegin, aEnd);
|
2015-03-23 22:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a range to iterate from EnumType(0) to aEnd, exclusive. EnumType(0)
|
|
|
|
// should exist, but note that there is no way for us to ensure that it does!
|
2015-10-20 02:16:19 +03:00
|
|
|
template <typename EnumType>
|
2020-01-27 11:01:47 +03:00
|
|
|
constexpr detail::EnumeratedRange<EnumType> MakeEnumeratedRange(EnumType aEnd) {
|
2016-07-28 12:48:50 +03:00
|
|
|
return MakeEnumeratedRange(EnumType(0), aEnd);
|
2015-03-23 22:06:00 +03:00
|
|
|
}
|
|
|
|
|
2020-01-27 11:01:55 +03:00
|
|
|
// Create a range to iterate from aBegin to aEnd, inclusive.
|
2020-02-11 17:11:06 +03:00
|
|
|
//
|
|
|
|
// NOTE: This internally constructs a value that is one past `aEnd`, so the
|
|
|
|
// enumeration needs to either have a fixed underlying type, or `aEnd + 1` must
|
|
|
|
// be inside the range of the enumeration, in order to not be undefined
|
|
|
|
// behavior.
|
|
|
|
//
|
|
|
|
// See bug 1614512.
|
2020-01-27 11:01:55 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
constexpr detail::EnumeratedRange<EnumType> MakeInclusiveEnumeratedRange(
|
|
|
|
EnumType aBegin, EnumType aEnd) {
|
|
|
|
using EnumUnderlyingType = typename std::underlying_type_t<EnumType>;
|
|
|
|
const auto end = static_cast<EnumUnderlyingType>(aEnd);
|
|
|
|
|
|
|
|
MOZ_ASSERT(end != std::numeric_limits<EnumUnderlyingType>::max(),
|
|
|
|
"aEnd shouldn't overflow!");
|
|
|
|
return MakeEnumeratedRange(aBegin, static_cast<EnumType>(end + 1));
|
|
|
|
}
|
|
|
|
|
2023-06-13 00:10:11 +03:00
|
|
|
template <typename EnumType>
|
|
|
|
constexpr auto MakeInclusiveEnumeratedRange(EnumType aEnd) {
|
|
|
|
return MakeInclusiveEnumeratedRange(EnumType{0}, aEnd);
|
|
|
|
}
|
|
|
|
|
2015-03-23 22:06:00 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_EnumeratedRange_h
|