openmohaa/code/qcommon/stack.h

165 lines
3.1 KiB
C
Raw Permalink Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// stack.h : Generic Stack object.
#ifndef __STACK_H__
#define __STACK_H__
#if defined(GAME_DLL)
//
// game dll specific defines
//
2023-07-05 21:23:39 +02:00
# include "g_local.h"
2023-07-05 21:23:39 +02:00
# define STACK_Error gi.Error
# define STACK_DPrintf gi.DPrintf
# define STACK_WDPrintf(text) gi.DPrintf(text)
#elif defined(CGAME_DLL)
//
// cgame dll specific defines
//
2023-07-05 21:23:39 +02:00
# include "cg_local.h"
2023-07-05 21:23:39 +02:00
# define STACK_Error cgi.Error
# define STACK_DPrintf cgi.DPrintf
# define STACK_WDPrintf(text) cgi.DPrintf(text)
#else
//
// client specific defines
//
2023-07-05 21:23:39 +02:00
# define STACK_Error Com_Error
# define STACK_DPrintf Com_DPrintf
# define STACK_WDPrintf(text) Com_DPrintf(text)
#endif
2023-07-05 21:23:39 +02:00
template<class Type>
2016-03-27 11:49:47 +02:00
class StackNode
{
public:
2023-07-05 21:23:39 +02:00
Type data;
StackNode *next;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
StackNode(Type d);
2016-03-27 11:49:47 +02:00
};
2023-07-05 21:23:39 +02:00
template<class Type>
inline StackNode<Type>::StackNode(Type d)
: data(d)
2016-03-27 11:49:47 +02:00
{
2023-07-05 21:23:39 +02:00
next = NULL;
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
2016-03-27 11:49:47 +02:00
class Stack
{
private:
2023-07-05 21:23:39 +02:00
StackNode<Type> *head;
2016-03-27 11:49:47 +02:00
public:
2023-07-05 21:23:39 +02:00
Stack();
~Stack<Type>();
void Clear(void);
qboolean Empty(void);
void Push(Type data);
Type Pop(void);
Type Head(void);
2016-03-27 11:49:47 +02:00
};
2023-07-05 21:23:39 +02:00
template<class Type>
2016-03-27 11:49:47 +02:00
inline Stack<Type>::Stack()
{
2023-07-05 21:23:39 +02:00
head = NULL;
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
2016-03-27 11:49:47 +02:00
inline Stack<Type>::~Stack()
{
2023-07-05 21:23:39 +02:00
Clear();
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
inline void Stack<Type>::Clear(void)
2016-03-27 11:49:47 +02:00
{
2023-07-05 21:23:39 +02:00
while (!Empty()) {
Pop();
}
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
inline qboolean Stack<Type>::Empty(void)
2016-03-27 11:49:47 +02:00
{
2023-07-05 21:23:39 +02:00
if (head == NULL) {
return true;
}
return false;
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
inline void Stack<Type>::Push(Type data)
2016-03-27 11:49:47 +02:00
{
2023-07-05 21:23:39 +02:00
StackNode<Type> *tmp;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
tmp = new StackNode<Type>(data);
if (!tmp) {
assert(NULL);
STACK_Error(ERR_DROP, "Stack::Push : Out of memory");
}
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
tmp->next = head;
head = tmp;
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
inline Type Stack<Type>::Pop(void)
2016-03-27 11:49:47 +02:00
{
2023-07-05 21:23:39 +02:00
Type ret;
StackNode<Type> *node;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
if (!head) {
return NULL;
}
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
node = head;
ret = node->data;
head = node->next;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
delete node;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
return ret;
2016-03-27 11:49:47 +02:00
}
2023-07-05 21:23:39 +02:00
template<class Type>
inline Type Stack<Type>::Head(void)
2016-03-27 11:49:47 +02:00
{
2023-07-05 21:23:39 +02:00
if (!head) {
return NULL;
}
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
return head->data;
2016-03-27 11:49:47 +02:00
}
#endif /* stack.h */