Bug 1499346 [wpt PR 13540] - Use a common blank reference for wpt/css., a=testonly
[gecko.git] / mfbt / EnumeratedArray.h
blobf0f71a3d79b86002dd783ee2d6f8255982900240
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,
42 IndexType SizeAsEnumValue,
43 typename ValueType>
44 class EnumeratedArray
46 public:
47 static const size_t kSize = size_t(SizeAsEnumValue);
49 private:
50 typedef Array<ValueType, kSize> ArrayType;
52 ArrayType mArray;
54 public:
55 EnumeratedArray() {}
57 template <typename... Args>
58 MOZ_IMPLICIT EnumeratedArray(Args&&... aArgs)
59 : mArray{std::forward<Args>(aArgs)...}
62 explicit EnumeratedArray(const EnumeratedArray& aOther)
64 for (size_t i = 0; i < kSize; i++) {
65 mArray[i] = aOther.mArray[i];
69 EnumeratedArray(EnumeratedArray&& aOther)
71 for (size_t i = 0; i < kSize; i++) {
72 mArray[i] = std::move(aOther.mArray[i]);
76 ValueType& operator[](IndexType aIndex)
78 return mArray[size_t(aIndex)];
81 const ValueType& operator[](IndexType aIndex) const
83 return mArray[size_t(aIndex)];
86 EnumeratedArray& operator =(EnumeratedArray&& aOther)
88 for (size_t i = 0; i < kSize; i++) {
89 mArray[i] = std::move(aOther.mArray[i]);
91 return *this;
94 typedef typename ArrayType::iterator iterator;
95 typedef typename ArrayType::const_iterator const_iterator;
96 typedef typename ArrayType::reverse_iterator reverse_iterator;
97 typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
99 // Methods for range-based for loops.
100 iterator begin() { return mArray.begin(); }
101 const_iterator begin() const { return mArray.begin(); }
102 const_iterator cbegin() const { return mArray.cbegin(); }
103 iterator end() { return mArray.end(); }
104 const_iterator end() const { return mArray.end(); }
105 const_iterator cend() const { return mArray.cend(); }
107 // Methods for reverse iterating.
108 reverse_iterator rbegin() { return mArray.rbegin(); }
109 const_reverse_iterator rbegin() const { return mArray.rbegin(); }
110 const_reverse_iterator crbegin() const { return mArray.crbegin(); }
111 reverse_iterator rend() { return mArray.rend(); }
112 const_reverse_iterator rend() const { return mArray.rend(); }
113 const_reverse_iterator crend() const { return mArray.crend(); }
116 } // namespace mozilla
118 #endif // mozilla_EnumeratedArray_h