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 <stdlib.h> 10 11 #include <SupportDefs.h> 12 13 14 template<class T> class Stack 15 { 16 public: 17 Stack() 18 : 19 fArray(NULL), 20 fUsed(0), 21 fMax(0) 22 { 23 } 24 25 ~Stack() 26 { 27 if (fArray) 28 free(fArray); 29 } 30 31 status_t Push(T value) 32 { 33 if (fUsed >= fMax) 34 { 35 fMax += 16; 36 fArray = (T *)realloc(fArray,fMax * sizeof(T)); 37 if (fArray == NULL) 38 return B_NO_MEMORY; 39 } 40 fArray[fUsed++] = value; 41 return B_OK; 42 } 43 44 bool Pop(T *value) 45 { 46 if (fUsed == 0) 47 return false; 48 49 *value = fArray[--fUsed]; 50 return true; 51 } 52 53 private: 54 T *fArray; 55 int32 fUsed; 56 int32 fMax; 57 }; 58 59 #endif /* STACK_H */ 60