Bug 989123 - Add EnumeratedArray class to MFBT for plain static arrays indexed by a typed enum - r=Waldo
authorBenoit Jacob <bjacob@mozilla.com>
Fri, 25 Apr 2014 22:34:03 -0400
changeset 180309 3fbf849caa998a92455583c57d2cce3b6705519b
parent 180308 83c0c0d2436d20205eacc9aece839adf491254ad
child 180310 5560662e5eff6751e52f7ef34e804712c918236a
push id42737
push userbjacob@mozilla.com
push dateSat, 26 Apr 2014 02:34:56 +0000
treeherdermozilla-inbound@610d7ccaaa9c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs989123
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 989123 - Add EnumeratedArray class to MFBT for plain static arrays indexed by a typed enum - r=Waldo
mfbt/EnumeratedArray.h
mfbt/moz.build
new file mode 100644
--- /dev/null
+++ b/mfbt/EnumeratedArray.h
@@ -0,0 +1,81 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* EnumeratedArray is like Array, but indexed by a typed enum. */
+
+#ifndef mozilla_EnumeratedArray_h
+#define mozilla_EnumeratedArray_h
+
+#include "mozilla/Array.h"
+#include "mozilla/TypedEnum.h"
+
+namespace mozilla {
+
+/**
+ * EnumeratedArray is a fixed-size array container for use when an
+ * array is indexed by a specific enum class, as currently implemented
+ * by MOZ_BEGIN_ENUM_CLASS.
+ *
+ * This provides type safety by guarding at compile time against accidentally
+ * indexing such arrays with unrelated values. This also removes the need
+ * for manual casting when using a typed enum value to index arrays.
+ *
+ * Aside from the typing of indices, EnumeratedArray is similar to Array.
+ *
+ * Example:
+ *
+ *   MOZ_BEGIN_ENUM_CLASS(AnimalSpecies)
+ *     Cow,
+ *     Sheep,
+ *     Count
+ *   MOZ_END_ENUM_CLASS(AnimalSpecies)
+ *
+ *   EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
+ *
+ *   headCount[AnimalSpecies::Cow] = 17;
+ *   headCount[AnimalSpecies::Sheep] = 30;
+ *
+ */
+template<typename IndexType,
+         MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(IndexType) SizeAsEnumValue,
+         typename ValueType>
+class EnumeratedArray
+{
+  public:
+    static const size_t Size = size_t(SizeAsEnumValue);
+
+  private:
+    Array<ValueType, Size> mArray;
+
+  public:
+    EnumeratedArray() {}
+
+    explicit EnumeratedArray(const EnumeratedArray& aOther)
+    {
+      for (size_t i = 0; i < Size; i++)
+        mArray[i] = aOther.mArray[i];
+    }
+
+    explicit EnumeratedArray(const ValueType (&aOther)[Size])
+    {
+      for (size_t i = 0; i < Size; i++)
+        mArray[i] = aOther[i];
+    }
+
+    ValueType& operator[](IndexType aIndex)
+    {
+      return mArray[size_t(aIndex)];
+    }
+
+    const ValueType& operator[](IndexType aIndex) const
+    {
+      return mArray[size_t(aIndex)];
+    }
+};
+
+} // namespace mozilla
+
+#endif // mozilla_EnumeratedArray_h
--- a/mfbt/moz.build
+++ b/mfbt/moz.build
@@ -23,16 +23,17 @@ EXPORTS.mozilla = [
     'Char16.h',
     'CheckedInt.h',
     'Compiler.h',
     'Compression.h',
     'Constants.h',
     'DebugOnly.h',
     'decimal/Decimal.h',
     'Endian.h',
+    'EnumeratedArray.h',
     'EnumSet.h',
     'FloatingPoint.h',
     'GuardObjects.h',
     'HashFunctions.h',
     'IntegerPrintfMacros.h',
     'IntegerTypeTraits.h',
     'Likely.h',
     'LinkedList.h',