PixelLightAPI  .
Stack.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Stack.h                                        *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_CONTAINER_STACK_H__
00024 #define __PLCORE_CONTAINER_STACK_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/PLCore.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Classes                                               ]
00042 //[-------------------------------------------------------]
00043 /**
00044 *  @brief
00045 *    Stack template (FILO -> First In Last Out)
00046 *
00047 *  @remarks
00048 *  @verbatim
00049 *    Simple extendable stack using a linked list
00050 *    The stack will hold copies of the type used in the stack,
00051 *    so note that your classes should supply a copy (=) operator!
00052 *
00053 *    Usage example:
00054 *    Stack<MyClass> cStack;  // Create stack
00055 *    MyClass T, T1, T2, T3;  // Test class instances
00056 *    cStack.Push(T1);        // Push a copy of T1 onto the stack
00057 *    cStack.Push(T2);        // Push a copy of T2 onto the stack
00058 *    cStack.Push(T3);        // Push a copy of T3 onto the stack
00059 *    cStack.Pop(&T);         // Pop last element (T3)
00060 *    cStack.Pop(&T);         // Pop last element (T2)
00061 *    cStack.Pop(&T);         // Pop last element (T1)
00062 *  @endverbatim
00063 */
00064 template <class ValueType>
00065 class Stack {
00066 
00067 
00068     //[-------------------------------------------------------]
00069     //[ Public functions                                      ]
00070     //[-------------------------------------------------------]
00071     public:
00072         /**
00073         *  @brief
00074         *    Constructor
00075         */
00076         Stack();
00077 
00078         /**
00079         *  @brief
00080         *    Copy constructor
00081         *
00082         *  @param[in] cSource
00083         *    Source stack to copy from
00084         */
00085         Stack(const Stack<ValueType> &cSource);
00086 
00087         /**
00088         *  @brief
00089         *    Destructor
00090         */
00091         ~Stack();
00092 
00093         /**
00094         *  @brief
00095         *    Push an element onto the stack (enqueue)
00096         *
00097         *  @param[in] Element
00098         *    New stack element
00099         *
00100         *  @return
00101         *    'true' if all went fine, else 'false'
00102         */
00103         bool Push(const ValueType &Element);
00104 
00105         /**
00106         *  @brief
00107         *    Pops the top element from the stack (dequeue)
00108         *
00109         *  @param[out] pElement
00110         *    If not a null pointer, this will receive the popped element
00111         *
00112         *  @return
00113         *    'true' if all went fine, else 'false'
00114         *
00115         *  @note
00116         *    - On error, pElement is NOT touched, so ensure that you take this case into account!
00117         */
00118         bool Pop(ValueType *pElement = nullptr);
00119 
00120         /**
00121         *  @brief
00122         *    Returns the top element without removing it
00123         *
00124         *  @return
00125         *    Top stack element
00126         */
00127         ValueType Top() const;
00128 
00129         /**
00130         *  @brief
00131         *    Returns the number of elements on the stack
00132         *
00133         *  @return
00134         *    Number of stack elements
00135         */
00136         uint32 GetNumOfElements() const;
00137 
00138         /**
00139         *  @brief
00140         *    Copies the data from another stack
00141         *
00142         *  @param[in] cSource
00143         *    Stack to copy from
00144         *
00145         *  @return
00146         *    Reference to this instance
00147         */
00148         Stack<ValueType> &operator =(const Stack<ValueType> &cSource);
00149 
00150         /**
00151         *  @brief
00152         *    Clears the whole stack
00153         */
00154         void Clear();
00155 
00156 
00157     //[-------------------------------------------------------]
00158     //[ Private functions                                     ]
00159     //[-------------------------------------------------------]
00160     private:
00161         /**
00162         *  @brief
00163         *    Push an element to the back of the stack
00164         *
00165         *  @param[in] Element
00166         *    New stack element
00167         *
00168         *  @return
00169         *    'true' if all went fine, else 'false'
00170         */
00171         bool PushBack(const ValueType &Element);
00172 
00173 
00174     //[-------------------------------------------------------]
00175     //[ Private structures                                    ]
00176     //[-------------------------------------------------------]
00177     private:
00178         /**
00179         *  @brief
00180         *    Internal stack element
00181         */
00182         struct StackElement {
00183             StackElement *pNext;    /**< Pointer to the next element on the stack, can be a null pointer */
00184             ValueType     Data;     /**< The stored data */
00185         };
00186 
00187 
00188     //[-------------------------------------------------------]
00189     //[ Private static data                                   ]
00190     //[-------------------------------------------------------]
00191     private:
00192         static ValueType temp;  /** Temp object */
00193 
00194 
00195     //[-------------------------------------------------------]
00196     //[ Private data                                          ]
00197     //[-------------------------------------------------------]
00198     private:
00199         uint32        m_nNumOfElements; /**< Number of elements on the stack */
00200         StackElement *m_pTop;           /**< Pointer to the top element, can be a null pointer */
00201         StackElement *m_pBottom;        /**< Pointer to the bottom element, can be a null pointer */
00202 
00203 
00204 };
00205 
00206 
00207 //[-------------------------------------------------------]
00208 //[ Namespace                                             ]
00209 //[-------------------------------------------------------]
00210 } // PLCore
00211 
00212 
00213 //[-------------------------------------------------------]
00214 //[ Implementation                                        ]
00215 //[-------------------------------------------------------]
00216 #include "PLCore/Container/Stack.inl"
00217 
00218 
00219 #endif // __PLCORE_CONTAINER_STACK_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:51:03
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported