00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _util_render_stack_h
00029 #define _util_render_stack_h
00030
00031 #include <iostream>
00032
00033 namespace sc {
00034
00035 #define STACK_MAX_STACK_SIZE 20
00036 template <class T>
00037 class Stack {
00038 private:
00039 T objects[STACK_MAX_STACK_SIZE];
00040 int nobjects;
00041 public:
00042 Stack(): nobjects(0) {}
00043 void push(const T&a) {
00044 if (nobjects >= STACK_MAX_STACK_SIZE) {
00045 ExEnv::errn() << "Stack: overflow" << std::endl;
00046 abort();
00047 }
00048 objects[nobjects++] = a;
00049 }
00050 T pop() {
00051 if (!nobjects) {
00052 ExEnv::errn() << "Stack: underflow" << std::endl;
00053 abort();
00054 }
00055 nobjects -= 1;
00056 return objects[nobjects];
00057 }
00058 T top() const {
00059 if (!nobjects) {
00060 ExEnv::errn() << "Stack: underflow" << std::endl;
00061 abort();
00062 }
00063 return objects[nobjects - 1];
00064 }
00065 int n() const { return nobjects; }
00066 T operator[](int i) { return objects[i]; }
00067 void print(std::ostream& os = ExEnv::out0()) {
00068 os << "Stack (depth = " << nobjects << "):" << std::endl;
00069 for (int i=0; i<nobjects; i++) {
00070 os << " object " << i << ":" << std::endl;
00071 objects[i]->print(os);
00072 }
00073 }
00074 };
00075
00076 }
00077
00078 #endif
00079
00080
00081
00082
00083