Bumping manifests a=b2g-bump
[gecko.git] / mfbt / EnumeratedArray.h
blob7e8e89275feef09dc9bf506be30595f4b62d4856
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/TypedEnum.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, as currently implemented
20 * by MOZ_BEGIN_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 * MOZ_BEGIN_ENUM_CLASS(AnimalSpecies)
31 * Cow,
32 * Sheep,
33 * Count
34 * MOZ_END_ENUM_CLASS(AnimalSpecies)
36 * EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
38 * headCount[AnimalSpecies::Cow] = 17;
39 * headCount[AnimalSpecies::Sheep] = 30;
42 template<typename IndexType,
43 MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(IndexType) SizeAsEnumValue,
44 typename ValueType>
45 class EnumeratedArray
47 public:
48 static const size_t kSize = size_t(SizeAsEnumValue);
50 private:
51 Array<ValueType, kSize> mArray;
53 public:
54 EnumeratedArray() {}
56 explicit EnumeratedArray(const EnumeratedArray& aOther)
58 for (size_t i = 0; i < kSize; i++) {
59 mArray[i] = aOther.mArray[i];
63 ValueType& operator[](IndexType aIndex)
65 return mArray[size_t(aIndex)];
68 const ValueType& operator[](IndexType aIndex) const
70 return mArray[size_t(aIndex)];
74 } // namespace mozilla
76 #endif // mozilla_EnumeratedArray_h