OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
stack.h
1/*
2===========================================================================
3Copyright (C) 2015 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// stack.h : Generic Stack object.
24
25#ifndef __STACK_H__
26#define __STACK_H__
27
28#if defined(GAME_DLL)
29//
30// game dll specific defines
31//
32# include "g_local.h"
33
34# define STACK_Error gi.Error
35# define STACK_DPrintf gi.DPrintf
36# define STACK_WDPrintf(text) gi.DPrintf(text)
37
38#elif defined(CGAME_DLL)
39//
40// cgame dll specific defines
41//
42# include "cg_local.h"
43
44# define STACK_Error cgi.Error
45# define STACK_DPrintf cgi.DPrintf
46# define STACK_WDPrintf(text) cgi.DPrintf(text)
47
48#else
49
50//
51// client specific defines
52//
53# define STACK_Error Com_Error
54# define STACK_DPrintf Com_DPrintf
55# define STACK_WDPrintf(text) Com_DPrintf(text)
56#endif
57
58template<class Type>
59class StackNode
60{
61public:
62 Type data;
63 StackNode *next;
64
65 StackNode(Type d);
66};
67
68template<class Type>
69inline StackNode<Type>::StackNode(Type d)
70 : data(d)
71{
72 next = NULL;
73}
74
75template<class Type>
76class Stack
77{
78private:
79 StackNode<Type> *head;
80
81public:
82 Stack();
83 ~Stack<Type>();
84 void Clear(void);
85 qboolean Empty(void);
86 void Push(Type data);
87 Type Pop(void);
88 Type Head(void);
89};
90
91template<class Type>
92inline Stack<Type>::Stack()
93{
94 head = NULL;
95}
96
97template<class Type>
98inline Stack<Type>::~Stack()
99{
100 Clear();
101}
102
103template<class Type>
104inline void Stack<Type>::Clear(void)
105{
106 while (!Empty()) {
107 Pop();
108 }
109}
110
111template<class Type>
112inline qboolean Stack<Type>::Empty(void)
113{
114 if (head == NULL) {
115 return true;
116 }
117 return false;
118}
119
120template<class Type>
121inline void Stack<Type>::Push(Type data)
122{
123 StackNode<Type> *tmp;
124
125 tmp = new StackNode<Type>(data);
126 if (!tmp) {
127 assert(NULL);
128 STACK_Error(ERR_DROP, "Stack::Push : Out of memory");
129 }
130
131 tmp->next = head;
132 head = tmp;
133}
134
135template<class Type>
136inline Type Stack<Type>::Pop(void)
137{
138 Type ret;
139 StackNode<Type> *node;
140
141 if (!head) {
142 return NULL;
143 }
144
145 node = head;
146 ret = node->data;
147 head = node->next;
148
149 delete node;
150
151 return ret;
152}
153
154template<class Type>
155inline Type Stack<Type>::Head(void)
156{
157 if (!head) {
158 return NULL;
159 }
160
161 return head->data;
162}
163
164#endif /* stack.h */
Definition stack.h:33
Definition stack.h:49