ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

stack.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_STACK_H
00024 #define COMMON_STACK_H
00025 
00026 #include "common/scummsys.h"
00027 #include "common/array.h"
00028 
00029 namespace Common {
00030 
00034 template<class T, uint MAX_SIZE = 10>
00035 class FixedStack {
00036 public:
00037     typedef uint size_type;
00038 
00039     FixedStack<T, MAX_SIZE>() : _size(0) {}
00040 
00041     bool empty() const {
00042         return _size <= 0;
00043     }
00044 
00045     void clear() {
00046         _size = 0;
00047     }
00048 
00049     void push(const T &x) {
00050         assert(_size < MAX_SIZE);
00051         _stack[_size++] = x;
00052     }
00053 
00054     const T &top() const {
00055         assert(_size > 0);
00056         return _stack[_size - 1];
00057     }
00058 
00059     T &top() {
00060         assert(_size > 0);
00061         return _stack[_size - 1];
00062     }
00063 
00064     T pop() {
00065         T tmp = top();
00066         --_size;
00067         return tmp;
00068     }
00069 
00070     size_type size() const {
00071         return _size;
00072     }
00073 
00074     T &operator[](size_type i) {
00075         assert(i < MAX_SIZE);
00076         return _stack[i];
00077     }
00078 
00079     const T &operator[](size_type i) const {
00080         assert(i < MAX_SIZE);
00081         return _stack[i];
00082     }
00083 
00084 protected:
00085     T         _stack[MAX_SIZE];
00086     size_type _size;
00087 };
00088 
00089 
00093 template<class T>
00094 class Stack {
00095 private:
00096     Array<T> _stack;
00097 
00098 public:
00099     typedef typename Array<T>::size_type size_type;
00100 
00101     Stack<T>() {}
00102     Stack<T>(const Array<T> &stackContent) : _stack(stackContent) {}
00103 
00104     bool empty() const {
00105         return _stack.empty();
00106     }
00107 
00108     void clear() {
00109         _stack.clear();
00110     }
00111 
00112     void push(const T &x) {
00113         _stack.push_back(x);
00114     }
00115 
00116     T &top() {
00117         return _stack.back();
00118     }
00119 
00120     const T &top() const {
00121         return _stack.back();
00122     }
00123 
00124     T pop() {
00125         T tmp = _stack.back();
00126         _stack.pop_back();
00127         return tmp;
00128     }
00129 
00130     size_type size() const {
00131         return _stack.size();
00132     }
00133 
00134     T &operator[](size_type i) {
00135         return _stack[i];
00136     }
00137 
00138     const T &operator[](size_type i) const {
00139         return _stack[i];
00140     }
00141 };
00142 
00143 } // End of namespace Common
00144 
00145 #endif


Generated on Sat Oct 19 2019 05:00:59 for ResidualVM by doxygen 1.7.1
curved edge   curved edge