Bug 1531915 [wpt PR 15578] - Fix flakiness of external/wpt/css/css-position/z-index...
[gecko.git] / mfbt / EnumeratedArray.h
blob17be24f87e2dd75f3fc4467f341d055c7ddfbcdd
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* EnumeratedArray is like Array, but indexed by a typed enum. */
9 #ifndef mozilla_EnumeratedArray_h
10 #define mozilla_EnumeratedArray_h
12 #include "mozilla/Array.h"
13 #include "mozilla/Move.h"
15 namespace mozilla {
17 /**
18 * EnumeratedArray is a fixed-size array container for use when an
19 * array is indexed by a specific enum class.
21 * This provides type safety by guarding at compile time against accidentally
22 * indexing such arrays with unrelated values. This also removes the need
23 * for manual casting when using a typed enum value to index arrays.
25 * Aside from the typing of indices, EnumeratedArray is similar to Array.
27 * Example:
29 * enum class AnimalSpecies {
30 * Cow,
31 * Sheep,
32 * Count
33 * };
35 * EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
37 * headCount[AnimalSpecies::Cow] = 17;
38 * headCount[AnimalSpecies::Sheep] = 30;
41 template <typename IndexType, IndexType SizeAsEnumValue, typename ValueType>
42 class EnumeratedArray {
43 public:
44 static const size_t kSize = size_t(SizeAsEnumValue);
46 private:
47 typedef Array<ValueType, kSize> ArrayType;
49 ArrayType mArray;
51 public:
52 EnumeratedArray() {}
54 template <typename... Args>
55 MOZ_IMPLICIT constexpr EnumeratedArray(Args&&... aArgs)
56 : mArray{std::forward<Args>(aArgs)...} {}
58 explicit EnumeratedArray(const EnumeratedArray& aOther) {
59 for (size_t i = 0; i < kSize; i++) {
60 mArray[i] = aOther.mArray[i];
64 EnumeratedArray(EnumeratedArray&& aOther) {
65 for (size_t i = 0; i < kSize; i++) {
66 mArray[i] = std::move(aOther.mArray[i]);
70 ValueType& operator[](IndexType aIndex) { return mArray[size_t(aIndex)]; }
72 const ValueType& operator[](IndexType aIndex) const {
73 return mArray[size_t(aIndex)];
76 EnumeratedArray& operator=(EnumeratedArray&& aOther) {
77 for (size_t i = 0; i < kSize; i++) {
78 mArray[i] = std::move(aOther.mArray[i]);
80 return *this;
83 typedef typename ArrayType::iterator iterator;
84 typedef typename ArrayType::const_iterator const_iterator;
85 typedef typename ArrayType::reverse_iterator reverse_iterator;
86 typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
88 // Methods for range-based for loops.
89 iterator begin() { return mArray.begin(); }
90 const_iterator begin() const { return mArray.begin(); }
91 const_iterator cbegin() const { return mArray.cbegin(); }
92 iterator end() { return mArray.end(); }
93 const_iterator end() const { return mArray.end(); }
94 const_iterator cend() const { return mArray.cend(); }
96 // Methods for reverse iterating.
97 reverse_iterator rbegin() { return mArray.rbegin(); }
98 const_reverse_iterator rbegin() const { return mArray.rbegin(); }
99 const_reverse_iterator crbegin() const { return mArray.crbegin(); }
100 reverse_iterator rend() { return mArray.rend(); }
101 const_reverse_iterator rend() const { return mArray.rend(); }
102 const_reverse_iterator crend() const { return mArray.crend(); }
105 } // namespace mozilla
107 #endif // mozilla_EnumeratedArray_h