Bug 1228641. Add a initializer_list constructor to nsTArray. r=froydnj
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Thu, 07 Jan 2016 10:53:51 -0500
changeset 280343 bf5934ed7c9c909113382c3e2eec9b34a8b18ef6
parent 280342 ea9e5a699b97a579adf0f63cad02d2d4226464c3
child 280344 e8ad04f3a65f8f7e524433473f2dacbc15669d09
push id70405
push userjmuizelaar@mozilla.com
push dateMon, 18 Jan 2016 03:15:32 +0000
treeherdermozilla-inbound@bf5934ed7c9c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1228641
milestone46.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 1228641. Add a initializer_list constructor to nsTArray. r=froydnj This also adds a couple of users of the new pattern.
gfx/layers/Layers.cpp
gfx/layers/apz/test/gtest/TestAsyncPanZoomController.cpp
xpcom/glue/nsTArray.h
--- a/gfx/layers/Layers.cpp
+++ b/gfx/layers/Layers.cpp
@@ -71,18 +71,17 @@ LayerManager::GetLog()
 
 FrameMetrics::ViewID
 LayerManager::GetRootScrollableLayerId()
 {
   if (!mRoot) {
     return FrameMetrics::NULL_SCROLL_ID;
   }
 
-  nsTArray<LayerMetricsWrapper> queue;
-  queue.AppendElement(LayerMetricsWrapper(mRoot));
+  nsTArray<LayerMetricsWrapper> queue = { LayerMetricsWrapper(mRoot) };
   while (queue.Length()) {
     LayerMetricsWrapper layer = queue[0];
     queue.RemoveElementAt(0);
 
     const FrameMetrics& frameMetrics = layer.Metrics();
     if (frameMetrics.IsScrollable()) {
       return frameMetrics.GetScrollId();
     }
@@ -105,18 +104,17 @@ LayerManager::GetRootScrollableLayers(ns
   }
 
   FrameMetrics::ViewID rootScrollableId = GetRootScrollableLayerId();
   if (rootScrollableId == FrameMetrics::NULL_SCROLL_ID) {
     aArray.AppendElement(mRoot);
     return;
   }
 
-  nsTArray<Layer*> queue;
-  queue.AppendElement(mRoot);
+  nsTArray<Layer*> queue = { mRoot };
   while (queue.Length()) {
     Layer* layer = queue[0];
     queue.RemoveElementAt(0);
 
     if (LayerMetricsWrapper::TopmostScrollableMetrics(layer).GetScrollId() == rootScrollableId) {
       aArray.AppendElement(layer);
       continue;
     }
@@ -129,18 +127,17 @@ LayerManager::GetRootScrollableLayers(ns
 
 void
 LayerManager::GetScrollableLayers(nsTArray<Layer*>& aArray)
 {
   if (!mRoot) {
     return;
   }
 
-  nsTArray<Layer*> queue;
-  queue.AppendElement(mRoot);
+  nsTArray<Layer*> queue = { mRoot };
   while (!queue.IsEmpty()) {
     Layer* layer = queue.LastElement();
     queue.RemoveElementAt(queue.Length() - 1);
 
     if (layer->HasScrollableFrameMetrics()) {
       aArray.AppendElement(layer);
       continue;
     }
--- a/gfx/layers/apz/test/gtest/TestAsyncPanZoomController.cpp
+++ b/gfx/layers/apz/test/gtest/TestAsyncPanZoomController.cpp
@@ -885,19 +885,18 @@ TEST_F(APZCPinchTester, Pinch_DefaultGes
 
 TEST_F(APZCPinchGestureDetectorTester, Pinch_UseGestureDetector_NoTouchAction) {
   SCOPED_GFX_PREF(TouchActionEnabled, bool, false);
   DoPinchTest(true);
 }
 
 TEST_F(APZCPinchGestureDetectorTester, Pinch_UseGestureDetector_TouchActionNone) {
   SCOPED_GFX_PREF(TouchActionEnabled, bool, true);
-  nsTArray<uint32_t> behaviors;
-  behaviors.AppendElement(mozilla::layers::AllowedTouchBehavior::NONE);
-  behaviors.AppendElement(mozilla::layers::AllowedTouchBehavior::NONE);
+  nsTArray<uint32_t> behaviors = { mozilla::layers::AllowedTouchBehavior::NONE,
+                                   mozilla::layers::AllowedTouchBehavior::NONE };
   DoPinchTest(false, &behaviors);
 }
 
 TEST_F(APZCPinchGestureDetectorTester, Pinch_UseGestureDetector_TouchActionZoom) {
   SCOPED_GFX_PREF(TouchActionEnabled, bool, true);
   nsTArray<uint32_t> behaviors;
   behaviors.AppendElement(mozilla::layers::AllowedTouchBehavior::PINCH_ZOOM);
   behaviors.AppendElement(mozilla::layers::AllowedTouchBehavior::PINCH_ZOOM);
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -8,16 +8,17 @@
 #define nsTArray_h__
 
 #include "nsTArrayForwardDeclare.h"
 #include "mozilla/Alignment.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/BinarySearch.h"
 #include "mozilla/fallible.h"
+#include "mozilla/InitializerList.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Move.h"
 #include "mozilla/ReverseIterator.h"
 #include "mozilla/TypeTraits.h"
 
 #include <string.h>
 
@@ -862,16 +863,17 @@ public:
   // members, which is obviously wrong.
   //
   // You can pass an nsTArray_Impl<E, OtherAlloc> to this method because
   // nsTArray_Impl<E, X> can be cast to const nsTArray_Impl<E, Y>&.  So the
   // effect on the API is the same as if we'd declared this method as taking
   // |const nsTArray_Impl<E, OtherAlloc>&|.
   explicit nsTArray_Impl(const self_type& aOther) { AppendElements(aOther); }
 
+  explicit nsTArray_Impl(std::initializer_list<E> aIL) { AppendElements(aIL.begin(), aIL.size()); }
   // Allow converting to a const array with a different kind of allocator,
   // Since the allocator doesn't matter for const arrays
   template<typename Allocator>
   operator const nsTArray_Impl<E, Allocator>&() const
   {
     return *reinterpret_cast<const nsTArray_Impl<E, Allocator>*>(this);
   }
   // And we have to do this for our subclasses too
@@ -2096,16 +2098,17 @@ public:
   typedef nsTArray_Impl<E, nsTArrayInfallibleAllocator> base_type;
   typedef nsTArray<E>                                   self_type;
   typedef typename base_type::size_type                 size_type;
 
   nsTArray() {}
   explicit nsTArray(size_type aCapacity) : base_type(aCapacity) {}
   explicit nsTArray(const nsTArray& aOther) : base_type(aOther) {}
   MOZ_IMPLICIT nsTArray(nsTArray&& aOther) : base_type(mozilla::Move(aOther)) {}
+  MOZ_IMPLICIT nsTArray(std::initializer_list<E> aIL) : base_type(aIL) {}
 
   template<class Allocator>
   explicit nsTArray(const nsTArray_Impl<E, Allocator>& aOther)
     : base_type(aOther)
   {
   }
   template<class Allocator>
   MOZ_IMPLICIT nsTArray(nsTArray_Impl<E, Allocator>&& aOther)