xref: /haiku/headers/private/media/TStack.h (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 #ifndef _TSTACK_H
2 #define _TSTACK_H
3 /* Stack - a template stack class, does not call any constructors/destructors
4 **
5 ** Copyright 2001 pinc Software. All Rights Reserved.
6 ** This file may be used under the terms of the OpenBeOS License.
7 **
8 ** 2002-03-10 Modified by Marcus Overhagen
9 */
10 
11 template<class T> class Stack {
12 	public:
13 		Stack()
14 			:
15 			fArray(NULL),
16 			fUsed(0),
17 			fMax(0)
18 		{
19 		}
20 
21 		~Stack()
22 		{
23 			if (fArray)
24 				free(fArray);
25 		}
26 
27 		bool Push(const T & value)
28 		{
29 			if (fUsed >= fMax) {
30 				fMax += 16;
31 				T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
32 				if (newArray == NULL)
33 					return false;
34 
35 				fArray = newArray;
36 			}
37 			fArray[fUsed++] = value;
38 			return true;
39 		}
40 
41 		bool Pop(T *value)
42 		{
43 			if (fUsed == 0)
44 				return false;
45 
46 			*value = fArray[--fUsed];
47 			return true;
48 		}
49 
50 		int32 CountItems() const
51 		{
52 			return fUsed;
53 		}
54 
55 	private:
56 		T		*fArray;
57 		int32	fUsed;
58 		int32	fMax;
59 };
60 
61 #endif	/* TSTACK_H */
62