Source code

Revision control

Other Tools

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/. */
6
7
/* EnumeratedArray is like Array, but indexed by a typed enum. */
8
9
#ifndef mozilla_EnumeratedArray_h
10
#define mozilla_EnumeratedArray_h
11
12
#include <utility>
13
14
#include "mozilla/Array.h"
15
16
namespace mozilla {
17
18
/**
19
* EnumeratedArray is a fixed-size array container for use when an
20
* array is indexed by a specific enum class.
21
*
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.
25
*
26
* Aside from the typing of indices, EnumeratedArray is similar to Array.
27
*
28
* Example:
29
*
30
* enum class AnimalSpecies {
31
* Cow,
32
* Sheep,
33
* Count
34
* };
35
*
36
* EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
37
*
38
* headCount[AnimalSpecies::Cow] = 17;
39
* headCount[AnimalSpecies::Sheep] = 30;
40
*
41
*/
42
template <typename IndexType, IndexType SizeAsEnumValue, typename ValueType>
43
class EnumeratedArray {
44
public:
45
static const size_t kSize = size_t(SizeAsEnumValue);
46
47
private:
48
typedef Array<ValueType, kSize> ArrayType;
49
50
ArrayType mArray;
51
52
public:
53
EnumeratedArray() {}
54
55
template <typename... Args>
56
MOZ_IMPLICIT constexpr EnumeratedArray(Args&&... aArgs)
57
: mArray{std::forward<Args>(aArgs)...} {}
58
59
explicit EnumeratedArray(const EnumeratedArray& aOther) {
60
for (size_t i = 0; i < kSize; i++) {
61
mArray[i] = aOther.mArray[i];
62
}
63
}
64
65
EnumeratedArray(EnumeratedArray&& aOther) {
66
for (size_t i = 0; i < kSize; i++) {
67
mArray[i] = std::move(aOther.mArray[i]);
68
}
69
}
70
71
ValueType& operator[](IndexType aIndex) { return mArray[size_t(aIndex)]; }
72
73
const ValueType& operator[](IndexType aIndex) const {
74
return mArray[size_t(aIndex)];
75
}
76
77
EnumeratedArray& operator=(EnumeratedArray&& aOther) {
78
for (size_t i = 0; i < kSize; i++) {
79
mArray[i] = std::move(aOther.mArray[i]);
80
}
81
return *this;
82
}
83
84
EnumeratedArray& operator=(const EnumeratedArray& aOther) {
85
for (size_t i = 0; i < kSize; i++) {
86
mArray[i] = aOther.mArray[i];
87
}
88
return *this;
89
}
90
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;
95
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(); }
103
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(); }
111
};
112
113
} // namespace mozilla
114
115
#endif // mozilla_EnumeratedArray_h