source: abuse/trunk/src/lisp/stack.h @ 496

Last change on this file since 496 was 494, checked in by Sam Hocevar, 10 years ago

style: remove trailing spaces, fix copyright statements.

  • Property svn:keywords set to Id
File size: 1.3 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#ifndef __STACK_HPP_
12#define __STACK_HPP_
13
14#include <stdio.h>
15#include <stdlib.h>
16
17// A fixed-size stack class
18template<class T> class grow_stack
19{
20public:
21    T **sdata;
22    long son;
23
24private:
25    long smax;
26
27public:
28    grow_stack(int max_size)
29    {
30        smax = max_size;
31        son = 0;
32        sdata = (T **)malloc(sizeof(T *) * smax);
33    }
34
35    void push(T *data)
36    {
37        if(son >= smax)
38        {
39            lbreak("error: stack overflow (%ld >= %ld)\n", son, smax);
40            exit(1);
41        }
42        sdata[son] = data;
43        son++;
44    }
45
46    T *pop(long total)
47    {
48        if (total > son)
49        {
50            lbreak("error: stack underflow\n");
51            exit(1);
52        }
53        son -= total;
54        return sdata[son];
55    }
56
57    void clean_up()
58    {
59        if(son != 0)
60            fprintf(stderr, "warning: cleaning up stack and not empty\n");
61        free(sdata);
62        sdata = NULL;
63        son = 0;
64    }
65};
66
67#endif
Note: See TracBrowser for help on using the repository browser.