Resource manager refactoring

This commit is contained in:
2014-04-13 03:42:12 +02:00
parent 14feb3659b
commit e4e5068727
7 changed files with 74 additions and 203 deletions
+25 -13
View File
@@ -21,34 +21,46 @@ public:
// Registers the factory function of a resource type
void RegisterType(std::string resourceType, std::function<Resource*(std::string)> factoryFunction);
// Registers a future instance of a resource
void RegisterResource(std::string resourceType, std::string resourceName);
// Loads all registered resources and caches them
void PreCache();
// Loads a resource and caches it for future use
void Preload(std::string resourceType, std::string resourceName);
template <typename T>
// Hot-loads a resource and caches it for future use
T* Load(std::string resourceType, std::string resourceName);
template <typename T>
// Fetches a preloaded resource
T* Fetch(std::string resourceName) const;
private:
unsigned int m_ResourceTypeCount = 0;
std::unordered_map<std::string, unsigned int> m_ResourceTypeIDs;
std::unordered_map<unsigned int, unsigned int> m_ResourceCount;
std::unordered_map<std::string, unsigned int> m_ResourceIDs;
std::unordered_map<std::string, std::function<Resource*(std::string)>> m_FactoryFunctions; // type -> factory function
std::unordered_map<std::string, std::string> m_RegisteredResources; // name -> type
std::unordered_map<std::string, Resource*> m_ResourceCache; // name -> resource
// TODO: Getters for IDs
unsigned int m_CurrentResourceTypeID = 0;
std::unordered_map<std::string, unsigned int> m_ResourceTypeIDs;
// Number of resources of a type. Doubles as local ID.
std::unordered_map<unsigned int, unsigned int> m_ResourceCount;
unsigned int GetTypeID(std::string resourceType);
unsigned int GetNewResourceID(unsigned int typeID);
// Internal: Create a resource and cache it
Resource* CreateResource(std::string resourceType, std::string resourceName);
};
template <typename T>
T* ResourceManager::Load(std::string resourceType, std::string resourceName)
{
return static_cast<T*>(CreateResource(resourceType, resourceName));
}
template <typename T>
T* ResourceManager::Fetch(std::string resourceName) const
{
if (m_ResourceCache.find(resourceName) == m_ResourceCache.end())
{
LOG_ERROR("Failed to load resource \"%s\": Resource not precached!", resourceName.c_str());
LOG_ERROR("Failed to fetch resource \"%s\": Resource not loaded!", resourceName.c_str());
return nullptr;
}
else