PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: ResourceManager.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_RESOURCE_MANAGER_H__ 00024 #define __PLCORE_RESOURCE_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 #include "PLCore/Base/Event/Event.h" 00035 00036 00037 //[-------------------------------------------------------] 00038 //[ Namespace ] 00039 //[-------------------------------------------------------] 00040 namespace PLCore { 00041 00042 00043 //[-------------------------------------------------------] 00044 //[ Forward declarations ] 00045 //[-------------------------------------------------------] 00046 template <class AType> class Resource; 00047 00048 00049 //[-------------------------------------------------------] 00050 //[ Classes ] 00051 //[-------------------------------------------------------] 00052 /** 00053 * @brief 00054 * Abstract resource manager template 00055 */ 00056 template <class AType> class ResourceManager { 00057 00058 00059 //[-------------------------------------------------------] 00060 //[ Friends ] 00061 //[-------------------------------------------------------] 00062 friend class Resource<AType>; 00063 00064 00065 //[-------------------------------------------------------] 00066 //[ Events ] 00067 //[-------------------------------------------------------] 00068 public: 00069 PLCore::Event<AType&> EventResourceRemoved; /**< Resource removed event, removed resource as parameter */ 00070 00071 00072 //[-------------------------------------------------------] 00073 //[ Public functions ] 00074 //[-------------------------------------------------------] 00075 public: 00076 /** 00077 * @brief 00078 * Returns the name of the manager 00079 * 00080 * @return 00081 * Name of the manager 00082 * 00083 * @note 00084 * - The manager name has no special usage, it's just for completeness 00085 */ 00086 String GetManagerName() const; 00087 00088 /** 00089 * @brief 00090 * Sets the name of the manager 00091 * 00092 * @param[in] sName 00093 * New manager name 00094 * 00095 * @return 00096 * 'true' if all went fine else 'false' 00097 * 00098 * @see 00099 * - GetManagerName() 00100 */ 00101 bool SetManagerName(const String &sName = ""); 00102 00103 /** 00104 * @brief 00105 * Clear the manager, delete all resources 00106 * 00107 * @param[in] bProtectedToo 00108 * Delete protected resources, too? 00109 * 00110 * @return 00111 * 'true' if all went fine, else 'false' 00112 */ 00113 bool Clear(bool bProtectedToo = true); 00114 00115 /** 00116 * @brief 00117 * Creates a new resource 00118 * 00119 * @param[in] sName 00120 * Resource name, if "" an unused name is set automatically 00121 * 00122 * @return 00123 * Pointer to the created resource, a null pointer if there was an error 00124 * 00125 * @note 00126 * - If there's already a resource with this name, this resource is returned 00127 */ 00128 AType *Create(const String &sName = ""); 00129 00130 /** 00131 * @brief 00132 * Unload a resource 00133 * 00134 * @param[in] cResource 00135 * Resource which should be unloaded 00136 * 00137 * @return 00138 * 'true' if all went fine, else 'false' 00139 * 00140 * @note 00141 * - Protected resources are NOT unloaded! 00142 */ 00143 bool Unload(AType &cResource); 00144 00145 /** 00146 * @brief 00147 * Unload unused resources 00148 * 00149 * @return 00150 * Number of unloaded resources 00151 * 00152 * @see 00153 * - Unload() 00154 */ 00155 uint32 UnloadUnused(); 00156 00157 /** 00158 * @brief 00159 * Returns whether unused resource are unloaded automatically or not 00160 * 00161 * @return 00162 * 'true' if unused resources are unloaded automatically, else 'false' 00163 */ 00164 bool GetUnloadUnused() const; 00165 00166 /** 00167 * @brief 00168 * Sets whether unused resource are unloaded automatically or not 00169 * 00170 * @param[in] bUnloadUnused 00171 * Unload unused resources automatically? 00172 */ 00173 void SetUnloadUnused(bool bUnloadUnused = false); 00174 00175 /** 00176 * @brief 00177 * Get the standard resource 00178 * 00179 * @return 00180 * The standard resource, a null pointer if there's no such resource 00181 * 00182 * @note 00183 * - There should always be a standard resource! 00184 * - It is recommended that you protect your standard resource 00185 */ 00186 AType *GetStandard() const; 00187 00188 /** 00189 * @brief 00190 * Set the standard resource 00191 * 00192 * @param[in] pResource 00193 * Resource which should be the standard resource, a null pointer to set no such resource 00194 * 00195 * @return 00196 * 'true' if all went fine, else 'false' (maybe invalid resource) 00197 * 00198 * @see 00199 * - GetStandard() 00200 */ 00201 bool SetStandard(AType *pResource = nullptr); 00202 00203 /** 00204 * @brief 00205 * Returns the number of resources within the manager 00206 * 00207 * @return 00208 * Number of resources within the manager 00209 */ 00210 uint32 GetNumOfElements() const; 00211 00212 00213 //[-------------------------------------------------------] 00214 //[ Public virtual ResourceManager functions ] 00215 //[-------------------------------------------------------] 00216 public: 00217 /** 00218 * @brief 00219 * Loads a resource 00220 * 00221 * @param[in] sFilename 00222 * Filename of the resource to load 00223 * 00224 * @return 00225 * Pointer to the loaded resource, a null pointer on error (maybe the resource can't be load) 00226 * 00227 * @remarks 00228 * The resource name is set to the given filename. If there's already a resource with this 00229 * (file)name, this resource is returned instead creating a new one. This function is virtual to 00230 * enable derived managers to add some more features like automatic resource creation instead of 00231 * loading a resource. In this case, 'filenames' normally begin with 'Create ' to indicate automatic 00232 * resource creation. 00233 */ 00234 virtual AType *LoadResource(const String &sFilename); 00235 00236 /** 00237 * @brief 00238 * Initializes the manager 00239 * 00240 * @return 00241 * 'true' if all went fine, else 'false' 00242 */ 00243 virtual bool Init(); 00244 00245 /** 00246 * @brief 00247 * De-initializes the manager 00248 * 00249 * @return 00250 * 'true' if all went fine, else 'false' 00251 */ 00252 virtual bool DeInit(); 00253 00254 /** 00255 * @brief 00256 * Updates the manager 00257 * 00258 * @return 00259 * 'true' if all went fine, else 'false' 00260 */ 00261 virtual bool Update(); 00262 00263 /** 00264 * @brief 00265 * Copy operator 00266 * 00267 * @param[in] cSource 00268 * Source to copy from 00269 * 00270 * @return 00271 * This instance 00272 */ 00273 virtual ResourceManager<AType> &operator =(const ResourceManager<AType> &cSource); 00274 00275 /** 00276 * @brief 00277 * Returns the resource at the given index 00278 * 00279 * @param[in] nIndex 00280 * Index of the resource 00281 * 00282 * @return 00283 * The corresponding object, a null pointer if there's no match 00284 */ 00285 virtual AType *GetByIndex(uint32 nIndex = 0) const; 00286 00287 /** 00288 * @brief 00289 * Returns the index of the given resource 00290 * 00291 * @param[in] cResource 00292 * The resource for which the index should be retrieved 00293 * 00294 * @return 00295 * The index position, <0 on failure (e.g. the resource is no member of this manager) 00296 */ 00297 virtual int GetIndex(AType &cResource) const; 00298 00299 /** 00300 * @brief 00301 * Moves a resource within the resource list 00302 * 00303 * @param[in] nFromIndex 00304 * The index of the resource which should be moved 00305 * @param[in] nToIndex 00306 * The index to which the resource should be moved 00307 * 00308 * @note 00309 * - This methods assumes, that both index values are within 0 and GetNumOfElements()-1 00310 */ 00311 virtual void MoveResource(uint32 nFromIndex, uint32 nToIndex); 00312 00313 /** 00314 * @brief 00315 * Returns the resource with the given name 00316 * 00317 * @param[in] sName 00318 * Resource name 00319 * 00320 * @return 00321 * The corresponding resource, a null pointer if there's no match 00322 * 00323 * @note 00324 * - You can overload this function to specialize the behavior. This is done 00325 * for instance within SceneContainer to be able to use 'absolute names' 00326 * like 'Root.MyScene.MyNode', too. 00327 */ 00328 virtual AType *GetByName(const String &sName) const; 00329 00330 00331 //[-------------------------------------------------------] 00332 //[ Protected functions ] 00333 //[-------------------------------------------------------] 00334 protected: 00335 /** 00336 * @brief 00337 * Constructor 00338 */ 00339 ResourceManager(); 00340 00341 /** 00342 * @brief 00343 * Destructor 00344 */ 00345 virtual ~ResourceManager(); 00346 00347 /** 00348 * @brief 00349 * Sets the resource name 00350 * 00351 * @param[in] cResource 00352 * Resource to set the name 00353 * @param[in] sName 00354 * New resource name 00355 * 00356 * @return 00357 * 'true' if all went fine, else 'false' (maybe the name is already used) 00358 * 00359 * @see 00360 * - GetName() 00361 * 00362 * @note 00363 * - You can overload this function if there are 'reserved' names which are NOT 00364 * allowed to be used or if special characters are not allowed 00365 */ 00366 bool SetResourceName(AType &cResource, const String &sName); 00367 00368 00369 //[-------------------------------------------------------] 00370 //[ Protected virtual ResourceManager functions ] 00371 //[-------------------------------------------------------] 00372 protected: 00373 /** 00374 * @brief 00375 * Creates a new resource 00376 * 00377 * @param[in] sName 00378 * Resource name 00379 * 00380 * @return 00381 * Pointer to the created resource, a null pointer if there was an error 00382 * 00383 * @note 00384 * - This function is used inside the function Create(). If AType is an abstract 00385 * class you have to overwrite this function to create an instance of the class. 00386 */ 00387 virtual AType *CreateResource(const String &sName = "") = 0; 00388 00389 00390 //[-------------------------------------------------------] 00391 //[ Protected data ] 00392 //[-------------------------------------------------------] 00393 protected: 00394 String m_sManagerName; /**< Manager name */ 00395 AType *m_pStandardResource; /**< Standard resource, can be a null pointer */ 00396 bool m_bUnloadUnused; /**< Unload unused resources? */ 00397 Array<AType*> m_lstResources; /**< Resource list */ 00398 HashMap<String, AType*> m_mapResources; /**< Resource map */ 00399 00400 00401 //[-------------------------------------------------------] 00402 //[ Private functions ] 00403 //[-------------------------------------------------------] 00404 private: 00405 /** 00406 * @brief 00407 * Adds a resource 00408 * 00409 * @param[in] cResource 00410 * Resource to add 00411 * 00412 * @return 00413 * 'true' if all went fine, else 'false' 00414 * (maybe resource is already within another manager) 00415 */ 00416 bool Add(AType &cResource); 00417 00418 /** 00419 * @brief 00420 * Removes a resource 00421 * 00422 * @param[in] cResource 00423 * Resource to remove 00424 * 00425 * @return 00426 * 'true' if all went fine, else 'false' (maybe resource is not within the manager) 00427 * 00428 * @note 00429 * - This function will only remove the resource from the manager, but 00430 * NOT deleting it! 00431 */ 00432 bool Remove(AType &cResource); 00433 00434 00435 }; 00436 00437 00438 //[-------------------------------------------------------] 00439 //[ Namespace ] 00440 //[-------------------------------------------------------] 00441 } // PLCore 00442 00443 00444 //[-------------------------------------------------------] 00445 //[ Implementation ] 00446 //[-------------------------------------------------------] 00447 #include "PLCore/Container/ResourceManager.inl" 00448 00449 00450 #endif // __PLCORE_RESOURCE_MANAGER_H__
|