source: abuse/trunk/src/lisp/stack.hpp @ 91

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