1a38a92c9SIngo Weinhold/* Stack - a template stack class (plus some handy methods)
2a38a92c9SIngo Weinhold *
3a38a92c9SIngo Weinhold * Copyright 2001-2005, Axel D��rfler, axeld@pinc-software.de.
4a38a92c9SIngo Weinhold * This file may be used under the terms of the MIT License.
5a38a92c9SIngo Weinhold */
6a38a92c9SIngo Weinhold#ifndef _FSSH_STACK_H
7a38a92c9SIngo Weinhold#define _FSSH_STACK_H
8a38a92c9SIngo Weinhold
9a38a92c9SIngo Weinhold
10a38a92c9SIngo Weinhold#include "fssh_defs.h"
11a38a92c9SIngo Weinhold#include "fssh_errors.h"
12a38a92c9SIngo Weinhold
13a38a92c9SIngo Weinhold
14a38a92c9SIngo Weinholdnamespace FSShell {
15a38a92c9SIngo Weinhold
16a38a92c9SIngo Weinhold
17a38a92c9SIngo Weinholdtemplate<class T> class Stack {
18a38a92c9SIngo Weinhold	public:
19a38a92c9SIngo Weinhold		Stack()
20a38a92c9SIngo Weinhold			:
21a38a92c9SIngo Weinhold			fArray(NULL),
22a38a92c9SIngo Weinhold			fUsed(0),
23a38a92c9SIngo Weinhold			fMax(0)
24a38a92c9SIngo Weinhold		{
25a38a92c9SIngo Weinhold		}
26a38a92c9SIngo Weinhold
27a38a92c9SIngo Weinhold		~Stack()
28a38a92c9SIngo Weinhold		{
29a38a92c9SIngo Weinhold			free(fArray);
30a38a92c9SIngo Weinhold		}
31a38a92c9SIngo Weinhold
32a38a92c9SIngo Weinhold		bool IsEmpty() const
33a38a92c9SIngo Weinhold		{
34a38a92c9SIngo Weinhold			return fUsed == 0;
35a38a92c9SIngo Weinhold		}
36a38a92c9SIngo Weinhold
37a38a92c9SIngo Weinhold		void MakeEmpty()
38a38a92c9SIngo Weinhold		{
39a38a92c9SIngo Weinhold			// could also free the memory
40a38a92c9SIngo Weinhold			fUsed = 0;
41a38a92c9SIngo Weinhold		}
42a38a92c9SIngo Weinhold
43a38a92c9SIngo Weinhold		fssh_status_t Push(T value)
44a38a92c9SIngo Weinhold		{
45a38a92c9SIngo Weinhold			if (fUsed >= fMax) {
46a38a92c9SIngo Weinhold				fMax += 16;
47a38a92c9SIngo Weinhold				T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
48a38a92c9SIngo Weinhold				if (newArray == NULL)
49a38a92c9SIngo Weinhold					return FSSH_B_NO_MEMORY;
50a38a92c9SIngo Weinhold
51a38a92c9SIngo Weinhold				fArray = newArray;
52a38a92c9SIngo Weinhold			}
53a38a92c9SIngo Weinhold			fArray[fUsed++] = value;
54a38a92c9SIngo Weinhold			return FSSH_B_OK;
55a38a92c9SIngo Weinhold		}
56a38a92c9SIngo Weinhold
57a38a92c9SIngo Weinhold		bool Pop(T *value)
58a38a92c9SIngo Weinhold		{
59a38a92c9SIngo Weinhold			if (fUsed == 0)
60a38a92c9SIngo Weinhold				return false;
61a38a92c9SIngo Weinhold
62a38a92c9SIngo Weinhold			*value = fArray[--fUsed];
63a38a92c9SIngo Weinhold			return true;
64a38a92c9SIngo Weinhold		}
65a38a92c9SIngo Weinhold
66a38a92c9SIngo Weinhold		T *Array()
67a38a92c9SIngo Weinhold		{
68a38a92c9SIngo Weinhold			return fArray;
69a38a92c9SIngo Weinhold		}
70a38a92c9SIngo Weinhold
71a38a92c9SIngo Weinhold		int32_t CountItems() const
72a38a92c9SIngo Weinhold		{
73a38a92c9SIngo Weinhold			return fUsed;
74a38a92c9SIngo Weinhold		}
75a38a92c9SIngo Weinhold
76a38a92c9SIngo Weinhold	private:
77a38a92c9SIngo Weinhold		T		*fArray;
78a38a92c9SIngo Weinhold		int32_t	fUsed;
79a38a92c9SIngo Weinhold		int32_t	fMax;
80a38a92c9SIngo Weinhold};
81a38a92c9SIngo Weinhold
82a38a92c9SIngo Weinhold}	// namespace FSShell
83a38a92c9SIngo Weinhold
84a38a92c9SIngo Weinholdusing FSShell::Stack;
85a38a92c9SIngo Weinhold
86a38a92c9SIngo Weinhold
87a38a92c9SIngo Weinhold#endif	/* _FSSH_STACK_H */