Bug 758396 - Let MFBT LinkedList allow const - r=jlebar
--- a/mfbt/LinkedList.h
+++ b/mfbt/LinkedList.h
@@ -120,25 +120,33 @@ public:
/*
* Get the next element in the list, or NULL if this is the last element in
* the list.
*/
T* getNext()
{
return next->asT();
}
+ const T* getNext() const
+ {
+ return next->asT();
+ }
/*
* Get the previous element in the list, or NULL if this is the first element
* in the list.
*/
T* getPrevious()
{
return prev->asT();
}
+ const T* getPrevious() const
+ {
+ return prev->asT();
+ }
/*
* Insert elem after this element in the list. |this| must be part of a
* linked list when you call setNext(); otherwise, this method will assert.
*/
void setNext(T* elem)
{
MOZ_ASSERT(isInList());
@@ -168,17 +176,17 @@ public:
next->prev = prev;
next = this;
prev = this;
}
/*
* Return true if |this| part is of a linked list, and false otherwise.
*/
- bool isInList()
+ bool isInList() const
{
MOZ_ASSERT((next == this) == (prev == this));
return next != this;
}
private:
LinkedListElement& operator=(const LinkedList<T>& other) MOZ_DELETE;
LinkedListElement(const LinkedList<T>& other) MOZ_DELETE;
@@ -203,16 +211,23 @@ private:
*/
T* asT()
{
if (isSentinel)
return NULL;
return static_cast<T*>(this);
}
+ const T* asT() const
+ {
+ if (isSentinel)
+ return NULL;
+
+ return static_cast<const T*>(this);
+ }
/*
* Insert elem after this element, but don't check that this element is in
* the list. This is called by LinkedList::insertFront().
*/
void setNextUnsafe(T* elem)
{
LinkedListElement *listElem = static_cast<LinkedListElement*>(elem);
@@ -274,24 +289,32 @@ public:
/*
* Get the first element of the list, or NULL if the list is empty.
*/
T* getFirst()
{
return sentinel.getNext();
}
+ const T* getFirst() const
+ {
+ return sentinel.getNext();
+ }
/*
* Get the last element of the list, or NULL if the list is empty.
*/
T* getLast()
{
return sentinel.getPrevious();
}
+ const T* getLast() const
+ {
+ return sentinel.getPrevious();
+ }
/*
* Get and remove the first element of the list. If the list is empty,
* return NULL.
*/
T* popFirst()
{
T* ret = sentinel.getNext();
@@ -312,17 +335,17 @@ public:
static_cast<LinkedListElement<T>*>(ret)->remove();
return ret;
}
/*
* Return true if the list is empty, or false otherwise.
*/
- bool isEmpty()
+ bool isEmpty() const
{
return !sentinel.isInList();
}
/*
* Remove all the elements from the list.
*
* This runs in time linear to the list's length, because we have to mark
@@ -333,62 +356,62 @@ public:
while (popFirst())
continue;
}
/*
* In a debug build, make sure that the list is sane (no cycles, consistent
* next/prev pointers, only one sentinel). Has no effect in release builds.
*/
- void debugAssertIsSane()
+ void debugAssertIsSane() const
{
#ifdef DEBUG
/*
* Check for cycles in the forward singly-linked list using the
* tortoise/hare algorithm.
*/
- for (LinkedListElement<T>* slow = sentinel.next,
- * fast1 = sentinel.next->next,
- * fast2 = sentinel.next->next->next;
+ for (const LinkedListElement<T>* slow = sentinel.next,
+ * fast1 = sentinel.next->next,
+ * fast2 = sentinel.next->next->next;
slow != sentinel && fast1 != sentinel && fast2 != sentinel;
slow = slow->next,
fast1 = fast2->next,
fast2 = fast1->next) {
MOZ_ASSERT(slow != fast1);
MOZ_ASSERT(slow != fast2);
}
/* Check for cycles in the backward singly-linked list. */
- for (LinkedListElement<T>* slow = sentinel.prev,
- * fast1 = sentinel.prev->prev,
- * fast2 = sentinel.prev->prev->prev;
+ for (const LinkedListElement<T>* slow = sentinel.prev,
+ * fast1 = sentinel.prev->prev,
+ * fast2 = sentinel.prev->prev->prev;
slow != sentinel && fast1 != sentinel && fast2 != sentinel;
slow = slow->prev,
fast1 = fast2->prev,
fast2 = fast1->prev) {
MOZ_ASSERT(slow != fast1);
MOZ_ASSERT(slow != fast2);
}
/*
* Check that |sentinel| is the only node in the list with
* isSentinel == true.
*/
- for (LinkedListElement<T>* elem = sentinel.next;
+ for (const LinkedListElement<T>* elem = sentinel.next;
elem != sentinel;
elem = elem->next) {
MOZ_ASSERT(!elem->isSentinel);
}
/* Check that the next/prev pointers match up. */
- LinkedListElement<T>* prev = sentinel;
- LinkedListElement<T>* cur = sentinel.next;
+ const LinkedListElement<T>* prev = sentinel;
+ const LinkedListElement<T>* cur = sentinel.next;
do {
MOZ_ASSERT(cur->prev == prev);
MOZ_ASSERT(prev->next == cur);
prev = cur;
cur = cur->next;
} while (cur != sentinel);
#endif /* ifdef DEBUG */