1#ifndef STACK_H 2#define STACK_H 3/* Stack - a template stack class 4** 5** Copyright 2001 pinc Software. All Rights Reserved. 6** This file may be used under the terms of the OpenBeOS License. 7*/ 8 9 10#include <SupportDefs.h> 11 12 13template<class T> class Stack { 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 fMax += 16; 33 T *newArray = (T *)realloc(fArray,fMax * sizeof(T)); 34 if (newArray == NULL) 35 return B_NO_MEMORY; 36 37 fArray = newArray; 38 } 39 fArray[fUsed++] = value; 40 return B_OK; 41 } 42 43 bool Pop(T *value) 44 { 45 if (fUsed == 0) 46 return false; 47 48 *value = fArray[--fUsed]; 49 return true; 50 } 51 52 private: 53 T *fArray; 54 int32 fUsed; 55 int32 fMax; 56}; 57 58#endif /* STACK_H */ 59