From b1429e2a05023b31275882d270dbec1b950a00bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 8 May 2008 16:43:59 +0000 Subject: [PATCH] Added a StackDeleter class that also empties the stack and deletes the items. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25381 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/util/Stack.h | 36 ++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/headers/private/kernel/util/Stack.h b/headers/private/kernel/util/Stack.h index dc832aabf1..855db1fd45 100644 --- a/headers/private/kernel/util/Stack.h +++ b/headers/private/kernel/util/Stack.h @@ -1,12 +1,13 @@ -/* Stack - a template stack class (plus some handy methods) - * - * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de. +/* + * Copyright 2001-2008, Axel Dörfler, axeld@pinc-software.de. * This file may be used under the terms of the MIT License. */ #ifndef KERNEL_UTIL_STACK_H #define KERNEL_UTIL_STACK_H +#include + #include @@ -75,4 +76,33 @@ template class Stack { int32 fMax; }; +template class StackDeleter { +public: + StackDeleter(Stack* stack) + : fStack(stack) + { + } + + ~StackDeleter() + { + if (fStack == NULL) + return; + + T item; + while (fStack->Pop(&item)) { + delete item; + } + + delete fStack; + } + + void Detach() + { + fStack = NULL; + } + +private: + Stack* fStack; +}; + #endif /* KERNEL_UTIL_STACK_H */