Main Page Class Hierarchy Compound List File List Compound Members Related Pages

# stack.h

    00001 //
    00002 // stack.h
    00003 //
    00004 // Copyright (C) 1996 Limit Point Systems, Inc.
    00005 //
    00006 // Author: Curtis Janssen <cljanss@limitpt.com>
    00007 // Maintainer: LPS
    00008 //
    00009 // This file is part of the SC Toolkit.
    00010 //
    00011 // The SC Toolkit is free software; you can redistribute it and/or modify
    00012 // it under the terms of the GNU Library General Public License as published by
    00013 // the Free Software Foundation; either version 2, or (at your option)
    00014 // any later version.
    00015 //
    00016 // The SC Toolkit is distributed in the hope that it will be useful,
    00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00019 // GNU Library General Public License for more details.
    00020 //
    00021 // You should have received a copy of the GNU Library General Public License
    00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
    00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
    00024 //
    00025 // The U.S. Government is granted a limited license as per AL 91-7.
    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 // Local Variables:
    00081 // mode: c++
    00082 // c-file-style: "CLJ"
    00083 // End: