#ifndef RESOURCE_MANAGER_H #define RESOURCE_MANAGER_H #include #include #include #include #include // TODO: I don't like the `load` function here as it forces the `Resource` to // have a loadFromFile function that they may not have (see TextureFont) or // they may need to call with several arguments (see SFML shaders). I think // it's better to load them by hand and capture them in the unique pointer // inside. // Maybe make a `manage` function that gets a pointer and captures. template class ResourceManager{ private: std::map> resourceMap; public: void load(Identifier id, const std::string &path); void manage(Identifier id, Resource *resource); Resource& get(Identifier id); const Resource& get(Identifier id) const; }; template void ResourceManager::load(Identifier id, const std::string& path){ auto resource = std::make_unique(); if( !resource->loadFromFile(path) ){ throw("ResoureManager::load - Failed to load filename: " + path); } auto inserted = resourceMap.insert( std::make_pair(id, std::move(resource)) ); assert(inserted.second); } template void ResourceManager::manage(Identifier id, Resource *resource){ std::unique_ptr resource_ptr (resource); auto inserted = resourceMap.insert( std::make_pair(id, std::move(resource_ptr)) ); assert(inserted.second); } template Resource& ResourceManager::get(Identifier id) { auto found = resourceMap.find(id); assert(found != resourceMap.end()); return *found->second; } template const Resource& ResourceManager::get(Identifier id) const { auto found = resourceMap.find(id); assert(found != resourceMap.end()); return *found->second; } #endif // RESOURCE_MANAGER_H