1 #ifndef STACK_H 2 #define STACK_H 3 /* Stack - a template stack class 4 ** 5 ** Copyright 2001 pinc Software. All Rights Reserved. 6 */ 7 8 9 #include <SupportDefs.h> 10 11 12 template<class T> class Stack 13 { 14 public: 15 Stack() 16 : 17 fArray(NULL), 18 fUsed(0), 19 fMax(0) 20 { 21 } 22 23 ~Stack() 24 { 25 if (fArray) 26 free(fArray); 27 } 28 29 status_t Push(T value) 30 { 31 if (fUsed >= fMax) 32 { 33 fMax += 16; 34 fArray = (T *)realloc(fArray,fMax * sizeof(T)); 35 if (fArray == NULL) 36 return B_NO_MEMORY; 37 } 38 fArray[fUsed++] = value; 39 return B_OK; 40 } 41 42 bool Pop(T *value) 43 { 44 if (fUsed == 0) 45 return false; 46 47 *value = fArray[--fUsed]; 48 return true; 49 } 50 51 private: 52 T *fArray; 53 int32 fUsed; 54 int32 fMax; 55 }; 56 57 #endif /* STACK_H */ 58