Bug 1212624 - Implement range-based iteration for LinkedList, r=Waldo

This commit is contained in:
Steve Fink 2015-10-07 14:19:42 -07:00
parent 980ee26e3e
commit 6019468a19

View File

@ -299,6 +299,26 @@ 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)
@ -383,6 +403,18 @@ public:
}
}
/*
* 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