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
14 #include "mozilla/Array.h"
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.
30 * enum class AnimalSpecies {
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
{
45 static const size_t kSize
= size_t(SizeAsEnumValue
);
48 typedef Array
<ValueType
, kSize
> ArrayType
;
55 template <typename
... Args
>
56 MOZ_IMPLICIT
constexpr EnumeratedArray(Args
&&... aArgs
)
57 : mArray
{std::forward
<Args
>(aArgs
)...} {}
59 explicit EnumeratedArray(const EnumeratedArray
& aOther
) {
60 for (size_t i
= 0; i
< kSize
; i
++) {
61 mArray
[i
] = aOther
.mArray
[i
];
65 EnumeratedArray(EnumeratedArray
&& aOther
) {
66 for (size_t i
= 0; i
< kSize
; i
++) {
67 mArray
[i
] = std::move(aOther
.mArray
[i
]);
71 ValueType
& operator[](IndexType aIndex
) { return mArray
[size_t(aIndex
)]; }
73 const ValueType
& operator[](IndexType aIndex
) const {
74 return mArray
[size_t(aIndex
)];
77 EnumeratedArray
& operator=(EnumeratedArray
&& aOther
) {
78 for (size_t i
= 0; i
< kSize
; i
++) {
79 mArray
[i
] = std::move(aOther
.mArray
[i
]);
84 EnumeratedArray
& operator=(const EnumeratedArray
& aOther
) {
85 for (size_t i
= 0; i
< kSize
; i
++) {
86 mArray
[i
] = aOther
.mArray
[i
];
91 typedef typename
ArrayType::iterator iterator
;
92 typedef typename
ArrayType::const_iterator const_iterator
;
93 typedef typename
ArrayType::reverse_iterator reverse_iterator
;
94 typedef typename
ArrayType::const_reverse_iterator const_reverse_iterator
;
96 // Methods for range-based for loops.
97 iterator
begin() { return mArray
.begin(); }
98 const_iterator
begin() const { return mArray
.begin(); }
99 const_iterator
cbegin() const { return mArray
.cbegin(); }
100 iterator
end() { return mArray
.end(); }
101 const_iterator
end() const { return mArray
.end(); }
102 const_iterator
cend() const { return mArray
.cend(); }
104 // Methods for reverse iterating.
105 reverse_iterator
rbegin() { return mArray
.rbegin(); }
106 const_reverse_iterator
rbegin() const { return mArray
.rbegin(); }
107 const_reverse_iterator
crbegin() const { return mArray
.crbegin(); }
108 reverse_iterator
rend() { return mArray
.rend(); }
109 const_reverse_iterator
rend() const { return mArray
.rend(); }
110 const_reverse_iterator
crend() const { return mArray
.crend(); }
113 } // namespace mozilla
115 #endif // mozilla_EnumeratedArray_h