PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Iterator.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_ITERATOR_H__ 00024 #define __PLCORE_CONTAINER_ITERATOR_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Namespace ] 00030 //[-------------------------------------------------------] 00031 namespace PLCore { 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Forward declarations ] 00036 //[-------------------------------------------------------] 00037 template <class ValueType> class IteratorImpl; 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Classes ] 00042 //[-------------------------------------------------------] 00043 /** 00044 * @brief 00045 * Iterator class 00046 * 00047 * @remarks 00048 * If an iterator is used, the iterable (for instance 'Map', 'Container' or 'Heap') the iterator 00049 * is operating on should not be manipulated by adding/removing new elements because this may 00050 * produce undefined behavior! Is highly recommended to keep the iterator in the smallest possible 00051 * local scope and to never keep an iterator as for instance pointer over a long time. 00052 * 00053 * @verbatim 00054 * Usage example: 00055 * Array<String> lstNames; // Iterable container 00056 * lstNames.Add("Lenny"); // Add "Lenny" to the iterable container 00057 * lstNames.Add("Barny"); // Add "Barny" to the iterable container 00058 * lstNames.Add("Homer"); // Add "Homer" to the iterable container 00059 * Iterator<String> cIterator = lstNames.GetIterator(); // Get an iterator instance 00060 * while (cIterator.HasNext()) // Is there a next element? 00061 * String sName = cIterator.Next(); // Return the next element, in this case, returns "Lenny", then "Barny" and then "Homer" 00062 * @endverbatim 00063 * 00064 * @note 00065 * - As the class same indicates, this is an implementation of the iterator design pattern 00066 * - An iterator is similar to a "for each loop" 00067 */ 00068 template <class ValueType> 00069 class Iterator { 00070 00071 00072 //[-------------------------------------------------------] 00073 //[ Public functions ] 00074 //[-------------------------------------------------------] 00075 public: 00076 /** 00077 * @brief 00078 * Constructor 00079 * 00080 * @param[in] cIteratorImpl 00081 * Reference to the iterator specific implementation 00082 */ 00083 Iterator(IteratorImpl<ValueType> &cIteratorImpl); 00084 00085 /** 00086 * @brief 00087 * Copy constructor 00088 * 00089 * @param[in] cIterator 00090 * Source to copy from 00091 */ 00092 Iterator(const Iterator<ValueType> &cIterator); 00093 00094 /** 00095 * @brief 00096 * Destructor 00097 */ 00098 ~Iterator<ValueType>(); 00099 00100 /** 00101 * @brief 00102 * Copy operator 00103 * 00104 * @param[in] cIterator 00105 * Source to copy from 00106 * 00107 * @return 00108 * Reference to this instance 00109 */ 00110 Iterator<ValueType> &operator =(const Iterator<ValueType> &cIterator); 00111 00112 /** 00113 * @brief 00114 * Checks whether the iterator can return a next element 00115 * 00116 * @return 00117 * 'true' if the iterator can return a next element, else 'false' 00118 */ 00119 bool HasNext() const; 00120 00121 /** 00122 * @brief 00123 * Returns the next element 00124 * 00125 * @return 00126 * Reference to the next element, reference to the 'Null'-object on error 00127 */ 00128 ValueType &Next(); 00129 00130 /** 00131 * @brief 00132 * Returns the next element 00133 * 00134 * @return 00135 * Reference to the next element, reference to the 'Null'-object on error 00136 */ 00137 ValueType &operator ++(); 00138 00139 /** 00140 * @brief 00141 * Checks whether the iterator can return a previous element 00142 * 00143 * @return 00144 * 'true' if the iterator can return a previous element, else 'false' 00145 */ 00146 bool HasPrevious() const; 00147 00148 /** 00149 * @brief 00150 * Returns the previous element 00151 * 00152 * @return 00153 * Reference to the previous element, reference to the 'Null'-object on error 00154 */ 00155 ValueType &Previous(); 00156 00157 /** 00158 * @brief 00159 * Returns the previous element 00160 * 00161 * @return 00162 * Reference to the previous element, reference to the 'Null'-object on error 00163 */ 00164 ValueType &operator --(); 00165 00166 00167 //[-------------------------------------------------------] 00168 //[ Private functions ] 00169 //[-------------------------------------------------------] 00170 private: 00171 /** 00172 * @brief 00173 * Checks whether the current internal iterator implementation is 'shared' and splits it if so 00174 */ 00175 void UniqueImplementation(); 00176 00177 00178 //[-------------------------------------------------------] 00179 //[ Private data ] 00180 //[-------------------------------------------------------] 00181 private: 00182 IteratorImpl<ValueType> *m_pIteratorImpl; /**< Pointer to the iterator specific implementation, always valid */ 00183 00184 00185 }; 00186 00187 00188 //[-------------------------------------------------------] 00189 //[ Namespace ] 00190 //[-------------------------------------------------------] 00191 } // PLCore 00192 00193 00194 //[-------------------------------------------------------] 00195 //[ Implementation ] 00196 //[-------------------------------------------------------] 00197 #include "PLCore/Container/Iterator.inl" 00198 00199 00200 #endif // __PLCORE_CONTAINER_ITERATOR_H__
|