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 1145a20d2c
commit 6c6500432f

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 */