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"
15 #include "EnumTypeTraits.h"
20 * EnumeratedArray is a fixed-size array container for use when an
21 * array is indexed by a specific enum class.
23 * This provides type safety by guarding at compile time against accidentally
24 * indexing such arrays with unrelated values. This also removes the need
25 * for manual casting when using a typed enum value to index arrays.
27 * Aside from the typing of indices, EnumeratedArray is similar to Array.
31 * enum class AnimalSpecies {
37 * EnumeratedArray<AnimalSpecies, int, AnimalSpecies::Count> headCount;
39 * headCount[AnimalSpecies::Cow] = 17;
40 * headCount[AnimalSpecies::Sheep] = 30;
42 * If the enum class has contiguous values and provides a specialization of
43 * mozilla::MaxContiguousEnumValue then the size will be calculated as the max
46 template <typename Enum
, typename ValueType
,
47 size_t Size
= ContiguousEnumSize
<Enum
>::value
>
48 class EnumeratedArray
{
50 static_assert(UnderlyingValue(MinContiguousEnumValue
<Enum
>::value
) == 0,
51 "All indexes would need to be corrected if min != 0");
53 using ArrayType
= Array
<ValueType
, Size
>;
58 EnumeratedArray() = default;
60 template <typename
... Args
>
61 MOZ_IMPLICIT
constexpr EnumeratedArray(Args
&&... aArgs
)
62 : mArray
{std::forward
<Args
>(aArgs
)...} {}
64 ValueType
& operator[](Enum aIndex
) { return mArray
[size_t(aIndex
)]; }
66 const ValueType
& operator[](Enum aIndex
) const {
67 return mArray
[size_t(aIndex
)];
70 using iterator
= typename
ArrayType::iterator
;
71 using const_iterator
= typename
ArrayType::const_iterator
;
72 using reverse_iterator
= typename
ArrayType::reverse_iterator
;
73 using const_reverse_iterator
= typename
ArrayType::const_reverse_iterator
;
75 // Methods for range-based for loops.
76 iterator
begin() { return mArray
.begin(); }
77 const_iterator
begin() const { return mArray
.begin(); }
78 const_iterator
cbegin() const { return mArray
.cbegin(); }
79 iterator
end() { return mArray
.end(); }
80 const_iterator
end() const { return mArray
.end(); }
81 const_iterator
cend() const { return mArray
.cend(); }
83 // Methods for reverse iterating.
84 reverse_iterator
rbegin() { return mArray
.rbegin(); }
85 const_reverse_iterator
rbegin() const { return mArray
.rbegin(); }
86 const_reverse_iterator
crbegin() const { return mArray
.crbegin(); }
87 reverse_iterator
rend() { return mArray
.rend(); }
88 const_reverse_iterator
rend() const { return mArray
.rend(); }
89 const_reverse_iterator
crend() const { return mArray
.crend(); }
92 } // namespace mozilla
94 #endif // mozilla_EnumeratedArray_h