Files
haiku-beta6/headers/private/media/TStack.h
T

62 lines
955 B
C++
Raw Normal View History

2002-10-04 00:42:45 +00:00
#ifndef _TSTACK_H
#define _TSTACK_H
/* Stack - a template stack class, does not call any constructors/destructors
2002-10-03 21:45:43 +00:00
**
** Copyright 2001 pinc Software. All Rights Reserved.
** This file may be used under the terms of the OpenBeOS License.
2002-10-04 00:42:45 +00:00
**
** 2002-03-10 Modified by Marcus Overhagen
2002-10-03 21:45:43 +00:00
*/
template<class T> class Stack {
public:
Stack()
:
fArray(NULL),
fUsed(0),
fMax(0)
{
}
~Stack()
{
if (fArray)
free(fArray);
}
2002-10-04 00:42:45 +00:00
bool Push(const T & value)
2002-10-03 21:45:43 +00:00
{
if (fUsed >= fMax) {
fMax += 16;
2002-10-04 00:42:45 +00:00
T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
2002-10-03 21:45:43 +00:00
if (newArray == NULL)
2002-10-04 00:42:45 +00:00
return false;
2002-10-03 21:45:43 +00:00
fArray = newArray;
}
fArray[fUsed++] = value;
2002-10-04 00:42:45 +00:00
return true;
2002-10-03 21:45:43 +00:00
}
bool Pop(T *value)
{
if (fUsed == 0)
return false;
*value = fArray[--fUsed];
return true;
}
2002-10-04 00:42:45 +00:00
int32 CountItems() const
{
return fUsed;
}
2002-10-03 21:45:43 +00:00
private:
T *fArray;
int32 fUsed;
int32 fMax;
};
2002-10-04 00:42:45 +00:00
#endif /* TSTACK_H */