#ifndef _ResourceManager_H__ #define _ResourceManager_H__ #include #include //#include "Containers.h" #include "Mesh.h" #include "Material.h" namespace GtpVisibilityPreprocessor { /** Resource manager controlling loading and destroying of resources. @note Resources must be created using the manager to assure that they are destroyed properly, otherwise the developer has the responsibility to destroy them himself. @note This class defines the pattern of singleton to assure that there is always only one resource manager. */ template class ResourceManager { public: /** Returns the resource manager as a singleton. */ static ResourceManager *GetSingleton() { if (!sResourceManager) { sResourceManager = new ResourceManager(); } return sResourceManager; } /** We also want full control over the delete. */ static void DelSingleton() { DEL_PTR(sResourceManager); } /** Creates new entry with a unique id */ T *CreateResource() { const int id = CreateUniqueId(); T *resource = new T(id); mEntries[id] = resource; return resource; } /** Searches for the mesh with the given name. @returns mesh or NULL if mesh was not found. */ T *FindEntry(const int id) const { std::map::const_iterator mit = mEntries.find(id); if (mit != mEntries.end()) { return (*mit).second; } return NULL; } /** Destroys mesh with the given name. @returns true if the mesh was found, false if not */ bool DestroyEntry(const int id) { if (!FindEntry(id)) { T *resource = mEntries[id]; mEntries.erase(id); DEL_PTR(resource); return true; } return false; } /** Returns size of resource container. */ const int GetSize() { return (int)mEntries.size(); } protected: /** Default constructor. The constructor is protected to have control over instantiation using the singleton pattern. */ ResourceManager() {} /** Release resources. */ ~ResourceManager() { std::map::iterator mit, mit_end = mEntries.end(); for (mit = mEntries.begin(); mit != mEntries.end(); ++ mit) { //cout << "mesh: " << (*mit).first << " " << (*mit).second << endl; DEL_PTR((*mit).second); } } /** Copy constructor. */ ResourceManager(const ResourceManager& source) { } /** Helper function returning unique id for resource. */ static int CreateUniqueId() { return sCurrentId ++; } protected: /// the resource container std::map mEntries; private: static ResourceManager *sResourceManager; static int sCurrentId; }; // only instance of the resource manager template ResourceManager *ResourceManager::sResourceManager = NULL; template int ResourceManager::sCurrentId = 0; /// This type is responsible for creation and disposal the meshes typedef ResourceManager MeshManager; /// This type is esponsible for creation and disposal of the materials. typedef ResourceManager MaterialManager; } #endif