Bug 1039521 - nsTArray: Add an AppendElement take taking a r-value reference. r=froydnj
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Fri, 18 Jul 2014 09:43:42 -0400
changeset 195524 db1ceb8f8d9241043418a20bf062b20c044fb567
parent 195523 22af5c3275c8d2566ff04ba7ddf685f4e683b155
child 195525 4d5d939010e5ace0bdcfc5968e3b3e5788e2dc38
push id27184
push userkwierso@gmail.com
push dateWed, 23 Jul 2014 00:39:18 +0000
treeherdermozilla-central@0ad20ad7b70a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1039521
milestone34.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 1039521 - nsTArray: Add an AppendElement take taking a r-value reference. r=froydnj This lets us store UniquePtr's in nsTArrays
xpcom/glue/nsTArray.h
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -6,16 +6,17 @@
 
 #ifndef nsTArray_h__
 #define nsTArray_h__
 
 #include "nsTArrayForwardDeclare.h"
 #include "mozilla/Alignment.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/Move.h"
 #include "mozilla/TypeTraits.h"
 
 #include <string.h>
 
 #include "nsCycleCollectionNoteChild.h"
 #include "nsAlgorithm.h"
 #include "nscore.h"
 #include "nsQuickSort.h"
@@ -463,24 +464,24 @@ public:
     // which zeroes out POD ("plain old data") types such as regular
     // ints.  We don't want that because it can be a performance issue
     // and people don't expect it; nsTArray should work like a regular
     // C/C++ array in this respect.
     new (static_cast<void*>(aE)) E;
   }
   // Invoke the copy-constructor in place.
   template<class A>
-  static inline void Construct(E* aE, const A& aArg)
+  static inline void Construct(E* aE, A &&aArg)
   {
     typedef typename mozilla::RemoveCV<E>::Type E_NoCV;
     typedef typename mozilla::RemoveCV<A>::Type A_NoCV;
     static_assert(!mozilla::IsSame<E_NoCV*, A_NoCV>::value,
                   "For safety, we disallow constructing nsTArray<E> elements "
                   "from E* pointers. See bug 960591.");
-    new (static_cast<void*>(aE)) E(aArg);
+    new (static_cast<void*>(aE)) E(mozilla::Forward<A>(aArg));
   }
   // Invoke the destructor in place.
   static inline void Destruct(E* aE) { aE->~E(); }
 };
 
 // The default comparator used by nsTArray
 template<class A, class B>
 class nsDefaultComparator
@@ -1249,16 +1250,30 @@ public:
 
   // A variation on the AppendElements method defined above.
   template<class Item>
   elem_type* AppendElement(const Item& aItem)
   {
     return AppendElements(&aItem, 1);
   }
 
+  // A variation of AppendElement that takes an r-value reference
+  elem_type* AppendElement(elem_type&& aItem)
+  {
+    if (!Alloc::Successful(this->EnsureCapacity(Length() + 1,
+                                                sizeof(elem_type))))
+      return nullptr;
+    index_type len = Length();
+    elem_type* iter = Elements() + len;
+    nsTArrayElementTraits<elem_type>::Construct(iter, mozilla::Forward<elem_type>(aItem));
+    this->IncrementLength(1);
+    return iter;
+  }
+
+
   // Append new elements without copy-constructing. This is useful to avoid
   // temporaries.
   // @return A pointer to the newly appended elements, or null on OOM.
   elem_type* AppendElements(size_type aCount)
   {
     if (!Alloc::Successful(this->EnsureCapacity(Length() + aCount,
                                                 sizeof(elem_type))))
       return nullptr;