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

Last change on this file since 74 was 74, checked in by Sam Hocevar, 15 years ago
  • Cleaned up stack.hpp and moved it to src/lisp/.
File size: 1.3 KB
RevLine 
[56]1/*
[74]2 *    Abuse - dark 2D side-scrolling platform game
3 *    Copyright (c) 1995 Crack dot Com
[56]4 *
[74]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.
[56]8 */
9
[2]10#ifndef __STACK_HPP_
11#define __STACK_HPP_
[74]12
13#include <stdio.h>
14
[2]15#ifndef NO_LIBS
[74]16#   include "jmalloc.hpp"
[2]17#else
[74]18#   include "fakelib.hpp"
[2]19#endif
20
[74]21// A fixed-size stack class
22template<class T> class grow_stack
[2]23{
[74]24public:
25    T **sdata;
26    long son;
[2]27
[74]28private:
29    long smax;
[2]30
[74]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
[2]71#endif
Note: See TracBrowser for help on using the repository browser.