Bug 1342303 part 2 - Add range-based for loop support to nsCOMArray. r=erahm
authorXidorn Quan <me@upsuper.org>
Fri, 24 Feb 2017 21:31:15 +1100
changeset 345710 b1df335e52e42cf261e5dcb5c9f6aeb735875f9d
parent 345709 6de42abcf2c71e8d74312a6587b181ba52fdeca1
child 345711 5460a7ea4e1f85adc2ff1a6eb00ff4da1a8c1178
push id31443
push usercbook@mozilla.com
push dateFri, 03 Mar 2017 12:01:25 +0000
treeherdermozilla-central@31c09bb63b69 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1342303
milestone54.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 1342303 part 2 - Add range-based for loop support to nsCOMArray. r=erahm MozReview-Commit-ID: 7T8Z0PVg2ex
xpcom/ds/nsCOMArray.h
--- a/xpcom/ds/nsCOMArray.h
+++ b/xpcom/ds/nsCOMArray.h
@@ -3,17 +3,19 @@
 /* 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/. */
 
 #ifndef nsCOMArray_h__
 #define nsCOMArray_h__
 
 #include "mozilla/Attributes.h"
+#include "mozilla/ArrayIterator.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/ReverseIterator.h"
 
 #include "nsCycleCollectionNoteChild.h"
 #include "nsTArray.h"
 #include "nsISupports.h"
 
 // See below for the definition of nsCOMArray<T>
 
 // a class that's nsISupports-specific, so that we can contain the
@@ -226,16 +228,22 @@ ImplCycleCollectionTraverse(nsCycleColle
 //
 // This array will accept null as an argument for any object, and will store
 // null in the array. But that also means that methods like ObjectAt() may
 // return null when referring to an existing, but null entry in the array.
 template<class T>
 class nsCOMArray : public nsCOMArray_base
 {
 public:
+  typedef int32_t                                       index_type;
+  typedef mozilla::ArrayIterator<T*, nsCOMArray>        iterator;
+  typedef mozilla::ArrayIterator<const T*, nsCOMArray>  const_iterator;
+  typedef mozilla::ReverseIterator<iterator>            reverse_iterator;
+  typedef mozilla::ReverseIterator<const_iterator>      const_reverse_iterator;
+
   nsCOMArray() {}
   explicit nsCOMArray(int32_t aCount) : nsCOMArray_base(aCount) {}
   explicit nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) {}
   nsCOMArray(nsCOMArray<T>&& aOther) { SwapElements(aOther); }
   ~nsCOMArray() {}
 
   // We have a move assignment operator, but no copy assignment operator.
   nsCOMArray<T>& operator=(nsCOMArray<T> && aOther)
@@ -438,16 +446,32 @@ public:
    * nsCOMArray<nsISomeInterface> array;
    * *length = array.Forget(retval);
    */
   uint32_t Forget(T*** aElements)
   {
     return nsCOMArray_base::Forget(reinterpret_cast<nsISupports***>(aElements));
   }
 
+  // Methods for range-based for loops.
+  iterator begin() { return iterator(*this, 0); }
+  const_iterator begin() const { return const_iterator(*this, 0); }
+  const_iterator cbegin() const { return begin(); }
+  iterator end() { return iterator(*this, Length()); }
+  const_iterator end() const { return const_iterator(*this, Length()); }
+  const_iterator cend() const { return end(); }
+
+  // Methods for reverse iterating.
+  reverse_iterator rbegin() { return reverse_iterator(end()); }
+  const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
+  const_reverse_iterator crbegin() const { return rbegin(); }
+  reverse_iterator rend() { return reverse_iterator(begin()); }
+  const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
+  const_reverse_iterator crend() const { return rend(); }
+
 private:
 
   // don't implement these!
   nsCOMArray<T>& operator=(const nsCOMArray<T>& aOther) = delete;
 };
 
 template<typename T>
 inline void