Bug 1857095 [wpt PR 42347] - [FedCM] Add a simple wpt test for the signin status...
[gecko.git] / mfbt / EnumeratedArray.h
blobf6edff48750770a4b138aa9672a10c9734c4e83f
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 <utility>
14 #include "mozilla/Array.h"
16 namespace mozilla {
18 /**
19 * EnumeratedArray is a fixed-size array container for use when an
20 * array is indexed by a specific enum class.
22 * This provides type safety by guarding at compile time against accidentally
23 * indexing such arrays with unrelated values. This also removes the need
24 * for manual casting when using a typed enum value to index arrays.
26 * Aside from the typing of indices, EnumeratedArray is similar to Array.
28 * Example:
30 * enum class AnimalSpecies {
31 * Cow,
32 * Sheep,
33 * Count
34 * };
36 * EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
38 * headCount[AnimalSpecies::Cow] = 17;
39 * headCount[AnimalSpecies::Sheep] = 30;
42 template <typename IndexType, IndexType SizeAsEnumValue, typename ValueType>
43 class EnumeratedArray {
44 public:
45 static const size_t kSize = size_t(SizeAsEnumValue);
47 private:
48 typedef Array<ValueType, kSize> ArrayType;
50 ArrayType mArray;
52 public:
53 EnumeratedArray() = default;
55 template <typename... Args>
56 MOZ_IMPLICIT constexpr EnumeratedArray(Args&&... aArgs)
57 : mArray{std::forward<Args>(aArgs)...} {}
59 ValueType& operator[](IndexType aIndex) { return mArray[size_t(aIndex)]; }
61 const ValueType& operator[](IndexType aIndex) const {
62 return mArray[size_t(aIndex)];
65 typedef typename ArrayType::iterator iterator;
66 typedef typename ArrayType::const_iterator const_iterator;
67 typedef typename ArrayType::reverse_iterator reverse_iterator;
68 typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
70 // Methods for range-based for loops.
71 iterator begin() { return mArray.begin(); }
72 const_iterator begin() const { return mArray.begin(); }
73 const_iterator cbegin() const { return mArray.cbegin(); }
74 iterator end() { return mArray.end(); }
75 const_iterator end() const { return mArray.end(); }
76 const_iterator cend() const { return mArray.cend(); }
78 // Methods for reverse iterating.
79 reverse_iterator rbegin() { return mArray.rbegin(); }
80 const_reverse_iterator rbegin() const { return mArray.rbegin(); }
81 const_reverse_iterator crbegin() const { return mArray.crbegin(); }
82 reverse_iterator rend() { return mArray.rend(); }
83 const_reverse_iterator rend() const { return mArray.rend(); }
84 const_reverse_iterator crend() const { return mArray.crend(); }
87 } // namespace mozilla
89 #endif // mozilla_EnumeratedArray_h