PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: ElementManager.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_ELEMENT_MANAGER_H__ 00024 #define __PLCORE_ELEMENT_MANAGER_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/String/String.h" 00032 #include "PLCore/Container/Array.h" 00033 #include "PLCore/Container/HashMap.h" 00034 00035 00036 //[-------------------------------------------------------] 00037 //[ Namespace ] 00038 //[-------------------------------------------------------] 00039 namespace PLCore { 00040 00041 00042 //[-------------------------------------------------------] 00043 //[ Forward declarations ] 00044 //[-------------------------------------------------------] 00045 template <class AType> class Element; 00046 00047 00048 //[-------------------------------------------------------] 00049 //[ Classes ] 00050 //[-------------------------------------------------------] 00051 /** 00052 * @brief 00053 * Abstract element manager template 00054 */ 00055 template <class AType> class ElementManager { 00056 00057 00058 //[-------------------------------------------------------] 00059 //[ Friends ] 00060 //[-------------------------------------------------------] 00061 friend class Element<AType>; 00062 00063 00064 //[-------------------------------------------------------] 00065 //[ Public functions ] 00066 //[-------------------------------------------------------] 00067 public: 00068 /** 00069 * @brief 00070 * Returns the name of the manager 00071 * 00072 * @return 00073 * Name of the manager 00074 * 00075 * @note 00076 * - The manager name has no special usage, it's just for completeness 00077 */ 00078 String GetManagerName() const; 00079 00080 /** 00081 * @brief 00082 * Sets the name of the manager 00083 * 00084 * @param[in] sName 00085 * New manager name 00086 * 00087 * @return 00088 * 'true' if all went fine else 'false' 00089 * 00090 * @see 00091 * - GetManagerName() 00092 */ 00093 bool SetManagerName(const String &sName = ""); 00094 00095 /** 00096 * @brief 00097 * Clear the manager, delete all elements 00098 * 00099 * @param[in] bProtectedToo 00100 * Delete protected elements, too? 00101 * 00102 * @return 00103 * 'true' if all went fine, else 'false' 00104 */ 00105 bool Clear(bool bProtectedToo = true); 00106 00107 /** 00108 * @brief 00109 * Creates a new element 00110 * 00111 * @param[in] sName 00112 * Element name, if "" an unused name is set automatically 00113 * 00114 * @return 00115 * Pointer to the created element, a null pointer if there was an error 00116 * 00117 * @note 00118 * - If there's already a element with this name, this element is returned 00119 */ 00120 AType *Create(const String &sName = ""); 00121 00122 /** 00123 * @brief 00124 * Adds a element 00125 * 00126 * @param[in] cElement 00127 * Element to add 00128 * 00129 * @return 00130 * 'true' if all went fine, else 'false' 00131 * (maybe element is already within another manager) 00132 */ 00133 bool Add(AType &cElement); 00134 00135 /** 00136 * @brief 00137 * Unload a element 00138 * 00139 * @param[in] cElement 00140 * Element which should be unloaded 00141 * 00142 * @return 00143 * 'true' if all went fine, else 'false' 00144 * 00145 * @note 00146 * - Protected elements are NOT unloaded! 00147 */ 00148 bool Unload(AType &cElement); 00149 00150 /** 00151 * @brief 00152 * Removes a element 00153 * 00154 * @param[in] cElement 00155 * Element to remove 00156 * 00157 * @return 00158 * 'true' if all went fine, else 'false' (maybe element is not within the manager) 00159 * 00160 * @note 00161 * - This function will only remove the element from the manager, but 00162 * NOT deleting it! 00163 */ 00164 bool Remove(AType &cElement); 00165 00166 /** 00167 * @brief 00168 * Unload unused elements 00169 * 00170 * @return 00171 * Number of unloaded elements 00172 * 00173 * @see 00174 * - Unload() 00175 */ 00176 uint32 UnloadUnused(); 00177 00178 /** 00179 * @brief 00180 * Returns whether unused element are unloaded automatically or not 00181 * 00182 * @return 00183 * 'true' if unused elements are unloaded automatically, else 'false' 00184 */ 00185 bool GetUnloadUnused() const; 00186 00187 /** 00188 * @brief 00189 * Sets whether unused element are unloaded automatically or not 00190 * 00191 * @param[in] bUnloadUnused 00192 * Unload unused elements automatically? 00193 */ 00194 void SetUnloadUnused(bool bUnloadUnused = false); 00195 00196 /** 00197 * @brief 00198 * Get the standard element 00199 * 00200 * @return 00201 * The standard element, a null pointer if there's no such element 00202 * 00203 * @note 00204 * - There should always be a standard element! 00205 * - It is recommended that you protect your standard element 00206 */ 00207 AType *GetStandard() const; 00208 00209 /** 00210 * @brief 00211 * Set the standard element 00212 * 00213 * @param[in] pElement 00214 * Element which should be the standard element, a null pointer to set no such element 00215 * 00216 * @return 00217 * 'true' if all went fine, else 'false' (maybe invalid element) 00218 * 00219 * @see 00220 * - GetStandard() 00221 */ 00222 bool SetStandard(AType *pElement = nullptr); 00223 00224 /** 00225 * @brief 00226 * Returns the number of elements within the manager 00227 * 00228 * @return 00229 * Number of elements within the manager 00230 */ 00231 uint32 GetNumOfElements() const; 00232 00233 00234 //[-------------------------------------------------------] 00235 //[ Public virtual functions ] 00236 //[-------------------------------------------------------] 00237 public: 00238 /** 00239 * @brief 00240 * Initializes the manager 00241 * 00242 * @return 00243 * 'true' if all went fine, else 'false' 00244 */ 00245 virtual bool Init(); 00246 00247 /** 00248 * @brief 00249 * De-initializes the manager 00250 * 00251 * @return 00252 * 'true' if all went fine, else 'false' 00253 */ 00254 virtual bool DeInit(); 00255 00256 /** 00257 * @brief 00258 * Updates the manager 00259 * 00260 * @return 00261 * 'true' if all went fine, else 'false' 00262 */ 00263 virtual bool Update(); 00264 00265 /** 00266 * @brief 00267 * Copy operator 00268 * 00269 * @param[in] cSource 00270 * Source to copy from 00271 * 00272 * @return 00273 * This instance 00274 */ 00275 virtual ElementManager<AType> &operator =(const ElementManager<AType> &cSource); 00276 00277 /** 00278 * @brief 00279 * Returns the element at the given index 00280 * 00281 * @param[in] nIndex 00282 * Index of the element 00283 * 00284 * @return 00285 * The corresponding object, a null pointer if there's no match 00286 */ 00287 virtual AType *GetByIndex(uint32 nIndex = 0) const; 00288 00289 /** 00290 * @brief 00291 * Returns the element with the given name 00292 * 00293 * @param[in] sName 00294 * Element name 00295 * 00296 * @return 00297 * The corresponding element, a null pointer if there's no match 00298 * 00299 * @note 00300 * - You can overload this function to specialize the behavior. This is done 00301 * for instance within SceneContainer to be able to use 'absolute names' 00302 * like 'Root.MyScene.MyNode', too. 00303 */ 00304 virtual AType *GetByName(const String &sName) const; 00305 00306 00307 //[-------------------------------------------------------] 00308 //[ Protected functions ] 00309 //[-------------------------------------------------------] 00310 protected: 00311 /** 00312 * @brief 00313 * Constructor 00314 */ 00315 ElementManager(); 00316 00317 /** 00318 * @brief 00319 * Destructor 00320 */ 00321 virtual ~ElementManager(); 00322 00323 /** 00324 * @brief 00325 * Sets the element name 00326 * 00327 * @param[in] cElement 00328 * Element to set the name 00329 * @param[in] sName 00330 * New element name 00331 * 00332 * @return 00333 * 'true' if all went fine, else 'false' (maybe the name is already used) 00334 * 00335 * @see 00336 * - GetName() 00337 * 00338 * @note 00339 * - You can overload this function if there are 'reserved' names which are NOT 00340 * allowed to be used or if special characters are not allowed. 00341 */ 00342 bool SetElementName(AType &cElement, const String &sName); 00343 00344 00345 //[-------------------------------------------------------] 00346 //[ Protected virtual functions ] 00347 //[-------------------------------------------------------] 00348 protected: 00349 /** 00350 * @brief 00351 * Creates a new element 00352 * 00353 * @param[in] sName 00354 * Element name 00355 * 00356 * @return 00357 * Pointer to the created element, a null pointer if there was an error 00358 * 00359 * @note 00360 * - This function is used inside the function Create(). If AType is an abstract 00361 * class you have to overwrite this function to create an instance of the class. 00362 */ 00363 virtual AType *CreateElement(const String &sName = "") = 0; 00364 00365 00366 //[-------------------------------------------------------] 00367 //[ Protected data ] 00368 //[-------------------------------------------------------] 00369 protected: 00370 String m_sManagerName; /**< Manager name */ 00371 AType *m_pStandardElement; /**< Standard element, can be a null pointer */ 00372 bool m_bUnloadUnused; /**< Unload unused resources? */ 00373 Array<AType*> m_lstElements; /**< Element list */ 00374 HashMap<String, AType*> m_mapElements; /**< Element map */ 00375 00376 00377 }; 00378 00379 00380 //[-------------------------------------------------------] 00381 //[ Namespace ] 00382 //[-------------------------------------------------------] 00383 } // PLCore 00384 00385 00386 //[-------------------------------------------------------] 00387 //[ Implementation ] 00388 //[-------------------------------------------------------] 00389 #include "PLCore/Container/ElementManager.inl" 00390 00391 00392 #endif // __PLCORE_ELEMENT_MANAGER_H__
|