Bug 1456875 - Add const iterators for LinkedList r=Waldo
authorJon Coppeard <jcoppeard@mozilla.com>
Mon, 30 Apr 2018 10:21:31 +0100
changeset 469812 dded77ad7ac088527f220743cece9aca2f3ff764
parent 469811 45603dbcb3bb9ba2c85cb6f424d44bd65fc1f0fe
child 469813 9263b4c0044716e788fa6f6097c305501cb12faa
push id9179
push userarchaeopteryx@coole-files.de
push dateThu, 03 May 2018 15:28:18 +0000
treeherdermozilla-beta@e6f9ade8bca7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1456875
milestone61.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 1456875 - Add const iterators for LinkedList r=Waldo
mfbt/LinkedList.h
mfbt/tests/TestLinkedList.cpp
--- a/mfbt/LinkedList.h
+++ b/mfbt/LinkedList.h
@@ -410,32 +410,33 @@ private:
   typedef typename Traits::RawType RawType;
   typedef typename Traits::ConstRawType ConstRawType;
   typedef typename Traits::ClientType ClientType;
   typedef typename Traits::ConstClientType ConstClientType;
 
   LinkedListElement<T> sentinel;
 
 public:
+  template <typename Type>
   class Iterator {
-    RawType mCurrent;
+    Type mCurrent;
 
   public:
-    explicit Iterator(RawType aCurrent) : mCurrent(aCurrent) {}
+    explicit Iterator(Type aCurrent) : mCurrent(aCurrent) {}
 
-    RawType operator *() const {
+    Type operator *() const {
       return mCurrent;
     }
 
     const Iterator& operator++() {
       mCurrent = mCurrent->getNext();
       return *this;
     }
 
-    bool operator!=(Iterator& aOther) const {
+    bool operator!=(const Iterator<Type>& aOther) const {
       return mCurrent != aOther.mCurrent;
     }
   };
 
   LinkedList() : sentinel(LinkedListElement<T>::NodeKind::Sentinel) { }
 
   LinkedList(LinkedList<T>&& aOther)
     : sentinel(mozilla::Move(aOther.sentinel))
@@ -530,21 +531,27 @@ public:
     }
   }
 
   /*
    * Allow range-based iteration:
    *
    *     for (MyElementType* elt : myList) { ... }
    */
-  Iterator begin() {
-    return Iterator(getFirst());
+  Iterator<RawType> begin() {
+    return Iterator<RawType>(getFirst());
+  }
+  Iterator<ConstRawType> begin() const {
+    return Iterator<ConstRawType>(getFirst());
   }
-  Iterator end() {
-    return Iterator(nullptr);
+  Iterator<RawType> end() {
+    return Iterator<RawType>(nullptr);
+  }
+  Iterator<ConstRawType> end() const {
+    return Iterator<ConstRawType>(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.
    */
--- a/mfbt/tests/TestLinkedList.cpp
+++ b/mfbt/tests/TestLinkedList.cpp
@@ -110,16 +110,21 @@ TestList()
   for (SomeClass* x : list) {
     x->incr();
   }
 
   MOZ_RELEASE_ASSERT(list.getFirst() == &two);
   MOZ_RELEASE_ASSERT(list.getLast() == &three);
   MOZ_RELEASE_ASSERT(list.getFirst()->mValue == 3);
   MOZ_RELEASE_ASSERT(list.getLast()->mValue == 4);
+
+  const LinkedList<SomeClass>& constList = list;
+  for (const SomeClass* x : constList) {
+    MOZ_RELEASE_ASSERT(x);
+  }
 }
 
 static void
 TestMove()
 {
   auto MakeSomeClass =
     [] (unsigned int aValue) -> SomeClass { return SomeClass(aValue); };