Bug 1310547 - Add removeAndGetNext/Previous methods to LinkedList (r=froydnj)

This commit is contained in:
Bill McCloskey 2016-10-24 12:49:21 -07:00
Родитель c0d229e060
Коммит 4c8471eb15
2 изменённых файлов: 43 добавлений и 0 удалений

Просмотреть файл

@ -250,6 +250,30 @@ public:
Traits::exitList(this); Traits::exitList(this);
} }
/*
* Remove this element from the list containing it. Returns a pointer to the
* element that follows this element (before it was removed). This method
* asserts if the element does not belong to a list.
*/
ClientType removeAndGetNext()
{
ClientType r = getNext();
remove();
return r;
}
/*
* Remove this element from the list containing it. Returns a pointer to the
* previous element in the containing list (before the removal). This method
* asserts if the element does not belong to a list.
*/
ClientType removeAndGetPrevious()
{
ClientType r = getPrevious();
remove();
return r;
}
/* /*
* Identical to remove(), but also asserts in debug builds that this element * Identical to remove(), but also asserts in debug builds that this element
* is in aList. * is in aList.

Просмотреть файл

@ -156,6 +156,24 @@ TestMove()
list3.clear(); list3.clear();
} }
static void
TestRemoveAndGet()
{
LinkedList<SomeClass> list;
SomeClass one(1), two(2), three(3);
list.insertBack(&one);
list.insertBack(&two);
list.insertBack(&three);
{ unsigned int check[] { 1, 2, 3 }; CheckListValues(list, check); }
MOZ_RELEASE_ASSERT(two.removeAndGetNext() == &three);
{ unsigned int check[] { 1, 3 }; CheckListValues(list, check); }
MOZ_RELEASE_ASSERT(three.removeAndGetPrevious() == &one);
{ unsigned int check[] { 1 }; CheckListValues(list, check); }
}
struct PrivateClass : private LinkedListElement<PrivateClass> { struct PrivateClass : private LinkedListElement<PrivateClass> {
friend class mozilla::LinkedList<PrivateClass>; friend class mozilla::LinkedList<PrivateClass>;
friend class mozilla::LinkedListElement<PrivateClass>; friend class mozilla::LinkedListElement<PrivateClass>;
@ -244,6 +262,7 @@ main()
TestList(); TestList();
TestPrivate(); TestPrivate();
TestMove(); TestMove();
TestRemoveAndGet();
TestRefPtrList(); TestRefPtrList();
return 0; return 0;
} }