PixelLightAPI  .
Stack.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Stack.h                                        *
00003  *
00004  *  Copyright (C) 2002-2012 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/Container/Iterable.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Forward declarations                                  ]
00042 //[-------------------------------------------------------]
00043 template <class ValueType> class StackIterator;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Classes                                               ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    Stack template (FILO -> First In Last Out)
00052 *
00053 *  @remarks
00054 *  @verbatim
00055 *    Simple extendable stack using a linked list
00056 *    The stack will hold copies of the type used in the stack,
00057 *    so note that your classes should supply a copy (=) operator!
00058 *
00059 *    Usage example:
00060 *    Stack<MyClass> cStack;  // Create stack
00061 *    MyClass T, T1, T2, T3;  // Test class instances
00062 *    cStack.Push(T1);        // Push a copy of T1 onto the stack
00063 *    cStack.Push(T2);        // Push a copy of T2 onto the stack
00064 *    cStack.Push(T3);        // Push a copy of T3 onto the stack
00065 *    cStack.Pop(&T);         // Pop last element (T3)
00066 *    cStack.Pop(&T);         // Pop last element (T2)
00067 *    cStack.Pop(&T);         // Pop last element (T1)
00068 *  @endverbatim
00069 */
00070 template <class ValueType>
00071 class Stack : public Iterable<ValueType> {
00072 
00073 
00074     //[-------------------------------------------------------]
00075     //[ Friends                                               ]
00076     //[-------------------------------------------------------]
00077     friend class StackIterator<ValueType>;
00078 
00079 
00080     //[-------------------------------------------------------]
00081     //[ Public functions                                      ]
00082     //[-------------------------------------------------------]
00083     public:
00084         /**
00085         *  @brief
00086         *    Constructor
00087         */
00088         Stack();
00089 
00090         /**
00091         *  @brief
00092         *    Copy constructor
00093         *
00094         *  @param[in] cSource
00095         *    Source stack to copy from
00096         */
00097         Stack(const Stack<ValueType> &cSource);
00098 
00099         /**
00100         *  @brief
00101         *    Destructor
00102         */
00103         ~Stack();
00104 
00105         /**
00106         *  @brief
00107         *    Push an element onto the stack (enqueue)
00108         *
00109         *  @param[in] Element
00110         *    New stack element
00111         *
00112         *  @return
00113         *    'true' if all went fine, else 'false'
00114         */
00115         bool Push(const ValueType &Element);
00116 
00117         /**
00118         *  @brief
00119         *    Pops the top element from the stack (dequeue)
00120         *
00121         *  @param[out] pElement
00122         *    If not a null pointer, this will receive the popped element
00123         *
00124         *  @return
00125         *    'true' if all went fine, else 'false'
00126         *
00127         *  @note
00128         *    - On error, pElement is NOT touched, so ensure that you take this case into account!
00129         */
00130         bool Pop(ValueType *pElement = nullptr);
00131 
00132         /**
00133         *  @brief
00134         *    Returns the top element without removing it
00135         *
00136         *  @return
00137         *    Top stack element (newest element)
00138         */
00139         ValueType Top() const;
00140 
00141         /**
00142         *  @brief
00143         *    Returns the bottom element without removing it
00144         *
00145         *  @return
00146         *    Bottom stack element (oldest element)
00147         */
00148         ValueType Bottom() const;
00149 
00150         /**
00151         *  @brief
00152         *    Returns the number of elements on the stack
00153         *
00154         *  @return
00155         *    Number of stack elements
00156         */
00157         uint32 GetNumOfElements() const;
00158 
00159         /**
00160         *  @brief
00161         *    Copies the data from another stack
00162         *
00163         *  @param[in] cSource
00164         *    Stack to copy from
00165         *
00166         *  @return
00167         *    Reference to this instance
00168         */
00169         Stack<ValueType> &operator =(const Stack<ValueType> &cSource);
00170 
00171         /**
00172         *  @brief
00173         *    Clears the whole stack
00174         */
00175         void Clear();
00176 
00177 
00178     //[-------------------------------------------------------]
00179     //[ Public virtual Iterable functions                     ]
00180     //[-------------------------------------------------------]
00181     public:
00182         virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override;
00183         virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override;
00184         virtual Iterator<ValueType> GetEndIterator() const override;
00185         virtual ConstIterator<ValueType> GetConstEndIterator() const override;
00186 
00187 
00188     //[-------------------------------------------------------]
00189     //[ Private functions                                     ]
00190     //[-------------------------------------------------------]
00191     private:
00192         /**
00193         *  @brief
00194         *    Push an element to the back of the stack
00195         *
00196         *  @param[in] Element
00197         *    New stack element
00198         *
00199         *  @return
00200         *    'true' if all went fine, else 'false'
00201         */
00202         bool PushBack(const ValueType &Element);
00203 
00204 
00205     //[-------------------------------------------------------]
00206     //[ Private structures                                    ]
00207     //[-------------------------------------------------------]
00208     private:
00209         /**
00210         *  @brief
00211         *    Internal stack element
00212         */
00213         struct StackElement {
00214             StackElement *pNextElement; /**< Pointer to the next element on the stack, can be a null pointer */
00215             ValueType     Data;         /**< The stored data */
00216         };
00217 
00218 
00219     //[-------------------------------------------------------]
00220     //[ Private static data                                   ]
00221     //[-------------------------------------------------------]
00222     private:
00223         static ValueType temp;  /** Temp object */
00224 
00225 
00226     //[-------------------------------------------------------]
00227     //[ Private data                                          ]
00228     //[-------------------------------------------------------]
00229     private:
00230         uint32        m_nNumOfElements; /**< Number of elements on the stack */
00231         StackElement *m_pTop;           /**< Pointer to the top element (newest element), can be a null pointer */
00232         StackElement *m_pBottom;        /**< Pointer to the bottom element (oldest element), can be a null pointer */
00233 
00234 
00235 };
00236 
00237 
00238 //[-------------------------------------------------------]
00239 //[ Namespace                                             ]
00240 //[-------------------------------------------------------]
00241 } // PLCore
00242 
00243 
00244 //[-------------------------------------------------------]
00245 //[ Implementation                                        ]
00246 //[-------------------------------------------------------]
00247 #include "PLCore/Container/Stack.inl"
00248 
00249 
00250 #endif // __PLCORE_CONTAINER_STACK_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:09:01
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported