PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Queue.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_QUEUE_H__ 00024 #define __PLCORE_CONTAINER_QUEUE_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 * Queue (FIFO -> First In First Out) template 00046 * 00047 * @remarks 00048 * @verbatim 00049 * Simple extendable queue using a linked list. 00050 * The queue will hold copies of the type used in the queue, 00051 * so note that your classes should supply a copy (=) operator! 00052 * 00053 * Usage example: 00054 * Queue<MyClass> cQueue; // Create queue 00055 * MyClass T, T1, T2, T3; // Test class instances 00056 * cQueue.Push(T1); // Push a copy of T1 onto the queue 00057 * cQueue.Push(T2); // Push a copy of T2 onto the queue 00058 * cQueue.Push(T3); // Push a copy of T3 onto the queue 00059 * cQueue.Pop(&T); // Pop last element (T1) 00060 * cQueue.Pop(&T); // Pop last element (T2) 00061 * cQueue.Pop(&T); // Pop last element (T3) 00062 * @endverbatim 00063 */ 00064 template <class ValueType> 00065 class Queue { 00066 00067 00068 //[-------------------------------------------------------] 00069 //[ Public functions ] 00070 //[-------------------------------------------------------] 00071 public: 00072 /** 00073 * @brief 00074 * Constructor 00075 */ 00076 Queue(); 00077 00078 /** 00079 * @brief 00080 * Copy constructor 00081 * 00082 * @param[in] cSource 00083 * Source queue to copy from 00084 */ 00085 Queue(const Queue<ValueType> &cSource); 00086 00087 /** 00088 * @brief 00089 * Destructor 00090 */ 00091 ~Queue(); 00092 00093 /** 00094 * @brief 00095 * Push an element onto the queue (enqueue) 00096 * 00097 * @param[in] Element 00098 * New queue 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 bottom element from the queue (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 bottom element without removing it 00123 * 00124 * @return 00125 * Bottom queue element 00126 */ 00127 ValueType Bottom() const; 00128 00129 /** 00130 * @brief 00131 * Returns the number of elements on the queue 00132 * 00133 * @return 00134 * Number of queue elements 00135 */ 00136 uint32 GetNumOfElements() const; 00137 00138 /** 00139 * @brief 00140 * Copies the data from another queue 00141 * 00142 * @param[in] cSource 00143 * Queue to copy from 00144 * 00145 * @return 00146 * Reference to this instance 00147 */ 00148 Queue<ValueType> &operator =(const Queue<ValueType> &cSource); 00149 00150 /** 00151 * @brief 00152 * Clears the whole queue 00153 */ 00154 void Clear(); 00155 00156 00157 //[-------------------------------------------------------] 00158 //[ Private structures ] 00159 //[-------------------------------------------------------] 00160 private: 00161 /** 00162 * @brief 00163 * Internal queue element 00164 */ 00165 struct QueueElement { 00166 QueueElement *pNext; /**< Pointer to the next element on the queue, can be a null pointer */ 00167 ValueType Data; /**< The stored data */ 00168 }; 00169 00170 00171 //[-------------------------------------------------------] 00172 //[ Private static data ] 00173 //[-------------------------------------------------------] 00174 private: 00175 static ValueType temp; /** Temp object */ 00176 00177 00178 //[-------------------------------------------------------] 00179 //[ Private data ] 00180 //[-------------------------------------------------------] 00181 private: 00182 uint32 m_nNumOfElements; /**< Number of elements on the queue */ 00183 QueueElement *m_pTop; /**< Pointer to the top element, can be a null pointer */ 00184 QueueElement *m_pBottom; /**< Pointer to the bottom element, can be a null pointer */ 00185 00186 00187 }; 00188 00189 00190 //[-------------------------------------------------------] 00191 //[ Namespace ] 00192 //[-------------------------------------------------------] 00193 } // PLCore 00194 00195 00196 //[-------------------------------------------------------] 00197 //[ Implementation ] 00198 //[-------------------------------------------------------] 00199 #include "PLCore/Container/Queue.inl" 00200 00201 00202 #endif // __PLCORE_CONTAINER_QUEUE_H__
|