Bug 1121269 - Add an AutoCleanLinkedList template that removes and deletes elements upon destruction. r=Waldo

This commit is contained in:
Mike Hommey 2015-02-04 10:33:05 +09:00
parent fe917e8ec3
commit 837ac3f6f8

View File

@ -55,6 +55,10 @@
* }
* };
*
* Additionally, the class AutoCleanLinkedList<T> is a LinkedList<T> that will
* remove and delete each element still within itself upon destruction. Note
* that because each element is deleted, elements must have been allocated
* using |new|.
*/
#ifndef mozilla_LinkedList_h
@ -478,6 +482,18 @@ private:
LinkedList(const LinkedList<T>& aOther) = delete;
};
template <typename T>
class AutoCleanLinkedList : public LinkedList<T>
{
public:
~AutoCleanLinkedList()
{
while (T* element = this->popFirst()) {
delete element;
}
}
};
} /* namespace mozilla */
#endif /* __cplusplus */