Bug 1309466 - Add support for initializer_list to Array and EnumeratedArray.
authorJames Cheng <jacheng@mozilla.com>
Wed, 12 Oct 2016 15:08:47 +0800
changeset 867487 cdf1c106c5cee07046a1c6f00d1a6e1733debf2d
parent 865793 7ae377917236b7e6111146aa9fb4c073c0efc7f4
child 867488 e82963ef5da873aa2b04efc54e37338e06f39a94
push id149435
push userjacheng@mozilla.com
push dateThu, 13 Oct 2016 07:32:32 +0000
treeherdertry@e82963ef5da8 [default view] [failures only]
bugs1309466
milestone52.0a1
Bug 1309466 - Add support for initializer_list to Array and EnumeratedArray. MozReview-Commit-ID: 9sD9Rg4qsy1
mfbt/Array.h
mfbt/EnumeratedArray.h
--- a/mfbt/Array.h
+++ b/mfbt/Array.h
@@ -6,28 +6,37 @@
 
 /* A compile-time constant-length array with bounds-checking assertions. */
 
 #ifndef mozilla_Array_h
 #define mozilla_Array_h
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/InitializerList.h"
+#include "mozilla/Move.h"
 #include "mozilla/ReverseIterator.h"
 
 #include <stddef.h>
 
 namespace mozilla {
 
 template<typename T, size_t Length>
 class Array
 {
   T mArr[Length];
 
 public:
+  Array() = default;
+
+  template <typename... Args>
+  Array(Args&&... aArgs)
+    : mArr{mozilla::Forward<Args>(aArgs)...}
+  {}
+
   T& operator[](size_t aIndex)
   {
     MOZ_ASSERT(aIndex < Length);
     return mArr[aIndex];
   }
 
   const T& operator[](size_t aIndex) const
   {
--- a/mfbt/EnumeratedArray.h
+++ b/mfbt/EnumeratedArray.h
@@ -49,16 +49,21 @@ public:
 private:
   typedef Array<ValueType, kSize> ArrayType;
 
   ArrayType mArray;
 
 public:
   EnumeratedArray() {}
 
+  template <typename... Args>
+  EnumeratedArray(Args&&... aArgs)
+    : mArray(mozilla::Forward<Args>(aArgs)...)
+  {}
+
   explicit EnumeratedArray(const EnumeratedArray& aOther)
   {
     for (size_t i = 0; i < kSize; i++) {
       mArray[i] = aOther.mArray[i];
     }
   }
 
   EnumeratedArray(EnumeratedArray&& aOther)