PixelLightAPI  .
Queue.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Queue.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_QUEUE_H__
00024 #define __PLCORE_CONTAINER_QUEUE_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 QueueIterator;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Classes                                               ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    Queue (FIFO -> First In First Out) template
00052 *
00053 *  @remarks
00054 *  @verbatim
00055 *    Simple extendable queue using a linked list.
00056 *    The queue will hold copies of the type used in the queue,
00057 *    so note that your classes should supply a copy (=) operator!
00058 *
00059 *    Usage example:
00060 *    Queue<MyClass> cQueue;  // Create queue
00061 *    MyClass T, T1, T2, T3;  // Test class instances
00062 *    cQueue.Push(T1);        // Push a copy of T1 onto the queue
00063 *    cQueue.Push(T2);        // Push a copy of T2 onto the queue
00064 *    cQueue.Push(T3);        // Push a copy of T3 onto the queue
00065 *    cQueue.Pop(&T);         // Pop last element (T1)
00066 *    cQueue.Pop(&T);         // Pop last element (T2)
00067 *    cQueue.Pop(&T);         // Pop last element (T3)
00068 *  @endverbatim
00069 */
00070 template <class ValueType>
00071 class Queue : public Iterable<ValueType> {
00072 
00073 
00074     //[-------------------------------------------------------]
00075     //[ Friends                                               ]
00076     //[-------------------------------------------------------]
00077     friend class QueueIterator<ValueType>;
00078 
00079 
00080     //[-------------------------------------------------------]
00081     //[ Public functions                                      ]
00082     //[-------------------------------------------------------]
00083     public:
00084         /**
00085         *  @brief
00086         *    Constructor
00087         */
00088         Queue();
00089 
00090         /**
00091         *  @brief
00092         *    Copy constructor
00093         *
00094         *  @param[in] cSource
00095         *    Source queue to copy from
00096         */
00097         Queue(const Queue<ValueType> &cSource);
00098 
00099         /**
00100         *  @brief
00101         *    Destructor
00102         */
00103         ~Queue();
00104 
00105         /**
00106         *  @brief
00107         *    Push an element onto the queue (enqueue)
00108         *
00109         *  @param[in] Element
00110         *    New queue 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 bottom element from the queue (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 queue 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 queue element (oldest element)
00147         */
00148         ValueType Bottom() const;
00149 
00150         /**
00151         *  @brief
00152         *    Returns the number of elements on the queue
00153         *
00154         *  @return
00155         *    Number of queue elements
00156         */
00157         uint32 GetNumOfElements() const;
00158 
00159         /**
00160         *  @brief
00161         *    Copies the data from another queue
00162         *
00163         *  @param[in] cSource
00164         *    Queue to copy from
00165         *
00166         *  @return
00167         *    Reference to this instance
00168         */
00169         Queue<ValueType> &operator =(const Queue<ValueType> &cSource);
00170 
00171         /**
00172         *  @brief
00173         *    Clears the whole queue
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 structures                                    ]
00190     //[-------------------------------------------------------]
00191     private:
00192         /**
00193         *  @brief
00194         *    Internal queue element
00195         */
00196         struct QueueElement {
00197             QueueElement *pNextElement; /**< Pointer to the next element on the queue, can be a null pointer */
00198             ValueType     Data;         /**< The stored data */
00199         };
00200 
00201 
00202     //[-------------------------------------------------------]
00203     //[ Private static data                                   ]
00204     //[-------------------------------------------------------]
00205     private:
00206         static ValueType temp;  /** Temp object */
00207 
00208 
00209     //[-------------------------------------------------------]
00210     //[ Private data                                          ]
00211     //[-------------------------------------------------------]
00212     private:
00213         uint32        m_nNumOfElements; /**< Number of elements on the queue */
00214         QueueElement *m_pTop;           /**< Pointer to the top element (newest element), can be a null pointer */
00215         QueueElement *m_pBottom;        /**< Pointer to the bottom element (oldest element), can be a null pointer */
00216 
00217 
00218 };
00219 
00220 
00221 //[-------------------------------------------------------]
00222 //[ Namespace                                             ]
00223 //[-------------------------------------------------------]
00224 } // PLCore
00225 
00226 
00227 //[-------------------------------------------------------]
00228 //[ Implementation                                        ]
00229 //[-------------------------------------------------------]
00230 #include "PLCore/Container/Queue.inl"
00231 
00232 
00233 #endif // __PLCORE_CONTAINER_QUEUE_H__


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