00001 #ifndef GSGL_DATA_STACK_H 00002 #define GSGL_DATA_STACK_H 00003 00004 // 00005 // $Id: stack.hpp 2 2008-03-01 20:58:50Z kulibali $ 00006 // 00007 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are 00011 // met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, 00014 // this list of conditions and the following disclaimer. 00015 // 00016 // * Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // * Neither the name of the The Periapsis Project nor the names of its 00021 // contributors may be used to endorse or promote products derived from 00022 // this software without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00025 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 // 00036 00037 #include "data/data.hpp" 00038 #include "data/array.hpp" 00039 00040 namespace gsgl 00041 { 00042 00043 namespace data 00044 { 00045 00046 /// The usual FILO stack structure. This should only be used for storing simple types, as items will be copied around in memory. 00047 template <typename T> 00048 class simple_stack 00049 : public simple_array<T> 00050 { 00051 public: 00052 simple_stack(); 00053 simple_stack(const simple_stack & s); 00054 simple_stack & operator= (const simple_stack & s); 00055 virtual ~simple_stack(); 00056 00057 /// \name simple_stack Functionality 00058 /// @{ 00059 const T & top() const; 00060 T & top(); 00061 void push(const T & item); 00062 void pop(); 00063 /// @} 00064 00065 }; // class simple_stack 00066 00067 00068 // implementation 00069 00070 template <typename T> 00071 simple_stack<T>::simple_stack() 00072 : simple_array<T>() 00073 { 00074 } // simple_stack<T>::simple_stack() 00075 00076 00077 template <typename T> 00078 simple_stack<T>::simple_stack(const simple_stack & s) 00079 : simple_array<T>(s) 00080 { 00081 } // simple_stack<T>::simple_stack() 00082 00083 00084 template <typename T> 00085 simple_stack<T> & simple_stack<T>::operator= (const simple_stack & s) 00086 { 00087 *dynamic_cast<simple_array<T> *>(this) = s; 00088 return *this; 00089 } // simple_stack<T>::operator= () 00090 00091 00092 template <typename T> 00093 simple_stack<T>::~simple_stack() 00094 { 00095 } // simple_stack<T>::~simple_stack() 00096 00097 00098 template <typename T> 00099 const T & simple_stack<T>::top() const 00100 { 00101 if (size()) 00102 return item(size()-1); 00103 else 00104 throw memory_exception(__FILE__, __LINE__, L"Stack underflow!"); 00105 } // simple_stack<T>::top() 00106 00107 00108 template <typename T> 00109 T & simple_stack<T>::top() 00110 { 00111 if (size()) 00112 return item(size()-1); 00113 else 00114 throw memory_exception(__FILE__, __LINE__, L"Stack underflow!"); 00115 } // simple_stack<T>::top() 00116 00117 00118 template <typename T> 00119 void simple_stack<T>::push(const T & item) 00120 { 00121 append(item); 00122 } // simple_stack<T>::push() 00123 00124 00125 template <typename T> 00126 void simple_stack<T>::pop() 00127 { 00128 if (size()) 00129 { 00130 resize(size()-1); 00131 } 00132 else 00133 { 00134 throw memory_exception(__FILE__, __LINE__, L"Stack underflow!"); 00135 } 00136 } // simple_stack<T>::pop() 00137 00138 00139 } // namespace data 00140 00141 } // namespace gsgl 00142 00143 #endif