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