Bug 1212624 - Implement range-based iteration for LinkedList, r=Waldo
☠☠ backed out by 7c0c549a1a10 ☠ ☠
authorSteve Fink <sfink@mozilla.com>
Wed, 07 Oct 2015 14:19:42 -0700
changeset 267758 4f499d30a0e08892a49d38cbf5cfe1cfd94b9b20
parent 267757 069c3bd07ac44c0a86006fe50d5efac125179ca7
child 267759 f73fca35daadf048671f3222c1fa803696956c76
push id66578
push usersfink@mozilla.com
push dateWed, 14 Oct 2015 22:15:16 +0000
treeherdermozilla-inbound@0d7a968d2d64 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1212624
milestone44.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 1212624 - Implement range-based iteration for LinkedList, r=Waldo
mfbt/LinkedList.h
--- a/mfbt/LinkedList.h
+++ b/mfbt/LinkedList.h
@@ -294,16 +294,36 @@ private:
 
 template<typename T>
 class LinkedList
 {
 private:
   LinkedListElement<T> sentinel;
 
 public:
+  class Iterator {
+    T* mCurrent;
+
+  public:
+    explicit Iterator(T* aCurrent) : mCurrent(aCurrent) {}
+
+    T* operator *() const {
+      return mCurrent;
+    }
+
+    const Iterator& operator++() {
+      mCurrent = mCurrent->getNext();
+      return *this;
+    }
+
+    bool operator!=(Iterator& aOther) const {
+      return mCurrent != aOther.mCurrent;
+    }
+  };
+
   LinkedList() : sentinel(LinkedListElement<T>::NODE_KIND_SENTINEL) { }
 
   LinkedList(LinkedList<T>&& aOther)
     : sentinel(mozilla::Move(aOther.sentinel))
   { }
 
   ~LinkedList() { MOZ_ASSERT(isEmpty()); }
 
@@ -379,16 +399,28 @@ public:
   void clear()
   {
     while (popFirst()) {
       continue;
     }
   }
 
   /*
+   * Allow range-based iteration:
+   *
+   *     for (MyElementType* elt : myList) { ... }
+   */
+  Iterator begin() {
+    return Iterator(getFirst());
+  }
+  Iterator end() {
+    return Iterator(nullptr);
+  }
+
+  /*
    * Measures the memory consumption of the list excluding |this|.  Note that
    * it only measures the list elements themselves.  If the list elements
    * contain pointers to other memory blocks, those blocks must be measured
    * separately during a subsequent iteration over the list.
    */
   size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
   {
     size_t n = 0;