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 GetPointerAt(int32 index, T **value) 42 { 43 if (index < 0 || index >= fUsed) 44 return false; 45 *value = &(fArray[index]); 46 return true; 47 } 48 49 bool Pop(T *value) 50 { 51 if (fUsed == 0) 52 return false; 53 54 *value = fArray[--fUsed]; 55 return true; 56 } 57 58 int32 CountItems() const 59 { 60 return fUsed; 61 } 62 63 private: 64 T *fArray; 65 int32 fUsed; 66 int32 fMax; 67 }; 68 69 #endif /* TSTACK_H */ 70