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

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

style: remove trailing spaces, fix copyright statements.

  • Property svn:keywords set to Id
File size: 1.3 KB
RevLine 
[56]1/*
[494]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>
[56]5 *
[494]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.
[56]9 */
10
[2]11#ifndef __STACK_HPP_
12#define __STACK_HPP_
[74]13
14#include <stdio.h>
[129]15#include <stdlib.h>
[74]16
17// A fixed-size stack class
18template<class T> class grow_stack
[124]19{
[74]20public:
21    T **sdata;
22    long son;
[2]23
[74]24private:
25    long smax;
[2]26
[74]27public:
28    grow_stack(int max_size)
29    {
30        smax = max_size;
31        son = 0;
[129]32        sdata = (T **)malloc(sizeof(T *) * smax);
[74]33    }
34
[124]35    void push(T *data)
[74]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    }
[124]45
46    T *pop(long total)
[74]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
[124]57    void clean_up()
58    {
[74]59        if(son != 0)
60            fprintf(stderr, "warning: cleaning up stack and not empty\n");
[129]61        free(sdata);
[74]62        sdata = NULL;
[124]63        son = 0;
[74]64    }
65};
66
[2]67#endif
Note: See TracBrowser for help on using the repository browser.