PixelLightAPI  .
ResourceManager.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: ResourceManager.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_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 resource with the given name
00290         *
00291         *  @param[in] sName
00292         *    Resource name
00293         *
00294         *  @return
00295         *    The corresponding resource, a null pointer if there's no match
00296         *
00297         *  @note
00298         *    - You can overload this function to specialize the behavior. This is done
00299         *      for instance within SceneContainer to be able to use 'absolute names'
00300         *      like 'Root.MyScene.MyNode', too.
00301         */
00302         virtual AType *GetByName(const String &sName) const;
00303 
00304 
00305     //[-------------------------------------------------------]
00306     //[ Protected functions                                   ]
00307     //[-------------------------------------------------------]
00308     protected:
00309         /**
00310         *  @brief
00311         *    Constructor
00312         */
00313         ResourceManager();
00314 
00315         /**
00316         *  @brief
00317         *    Destructor
00318         */
00319         virtual ~ResourceManager();
00320 
00321         /**
00322         *  @brief
00323         *    Sets the resource name
00324         *
00325         *  @param[in] cResource
00326         *    Resource to set the name
00327         *  @param[in] sName
00328         *    New resource name
00329         *
00330         *  @return
00331         *    'true' if all went fine, else 'false' (maybe the name is already used)
00332         *
00333         *  @see
00334         *    - GetName()
00335         *
00336         *  @note
00337         *    - You can overload this function if there are 'reserved' names which are NOT
00338         *      allowed to be used or if special characters are not allowed
00339         */
00340         bool SetResourceName(AType &cResource, const String &sName);
00341 
00342 
00343     //[-------------------------------------------------------]
00344     //[ Protected virtual ResourceManager functions           ]
00345     //[-------------------------------------------------------]
00346     protected:
00347         /**
00348         *  @brief
00349         *    Creates a new resource
00350         *
00351         *  @param[in] sName
00352         *    Resource name
00353         *
00354         *  @return
00355         *    Pointer to the created resource, a null pointer if there was an error
00356         *
00357         *  @note
00358         *    - This function is used inside the function Create(). If AType is an abstract
00359         *      class you have to overwrite this function to create an instance of the class.
00360         */
00361         virtual AType *CreateResource(const String &sName = "") = 0;
00362 
00363 
00364     //[-------------------------------------------------------]
00365     //[ Protected data                                        ]
00366     //[-------------------------------------------------------]
00367     protected:
00368         String                   m_sManagerName;        /**< Manager name */
00369         AType                   *m_pStandardResource;   /**< Standard resource, can be a null pointer */
00370         bool                     m_bUnloadUnused;       /**< Unload unused resources? */
00371         Array<AType*>            m_lstResources;        /**< Resource list */
00372         HashMap<String, AType*>  m_mapResources;        /**< Resource map */
00373 
00374 
00375     //[-------------------------------------------------------]
00376     //[ Private functions                                     ]
00377     //[-------------------------------------------------------]
00378     private:
00379         /**
00380         *  @brief
00381         *    Adds a resource
00382         *
00383         *  @param[in] cResource
00384         *    Resource to add
00385         *
00386         *  @return
00387         *    'true' if all went fine, else 'false'
00388         *    (maybe resource is already within another manager)
00389         */
00390         bool Add(AType &cResource);
00391 
00392         /**
00393         *  @brief
00394         *    Removes a resource
00395         *
00396         *  @param[in] cResource
00397         *    Resource to remove
00398         *
00399         *  @return
00400         *    'true' if all went fine, else 'false' (maybe resource is not within the manager)
00401         *
00402         *  @note
00403         *    - This function will only remove the resource from the manager, but
00404         *      NOT deleting it!
00405         */
00406         bool Remove(AType &cResource);
00407 
00408 
00409 };
00410 
00411 
00412 //[-------------------------------------------------------]
00413 //[ Namespace                                             ]
00414 //[-------------------------------------------------------]
00415 } // PLCore
00416 
00417 
00418 //[-------------------------------------------------------]
00419 //[ Implementation                                        ]
00420 //[-------------------------------------------------------]
00421 #include "PLCore/Container/ResourceManager.inl"
00422 
00423 
00424 #endif // __PLCORE_RESOURCE_MANAGER_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:51:00
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported