Bug 1342303 part 2 - Add range-based for loop support to nsCOMArray. r=erahm draft
authorXidorn Quan <me@upsuper.org>
Fri, 24 Feb 2017 21:31:15 +1100
changeset 491808 f9b2050ba67eab12abb2c6f0bf2d6ab648d357ee
parent 491807 f5ee1dba93ced315a0bb0aaaeca06f0f38284cf9
child 491809 2733a9e5c99d0e392ed1cf278707e213f3452107
push id47423
push userxquan@mozilla.com
push dateThu, 02 Mar 2017 08:49:10 +0000
reviewerserahm
bugs1342303
milestone54.0a1
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