Example #1
0
 Resource get(const std::string &fileName) {
     auto it = resources.find(fileName);
     if(it != resources.end()) {
         return it->second;
     } else {
         cacheResource(fileName, loadResource(fileName));
         return get(fileName);
     }
 }
Example #2
0
Resource* ResourceManager::createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception)
{
	auto facIt = m_FactoryFunctions.find(resourceType);
	if (facIt == m_FactoryFunctions.end()) {
		LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str());
        cacheResource(nullptr, resourceType, resourceName, parent);
		//This basically throws an exception.
        exception = std::make_exception_ptr(Resource::FailedLoadingException()); return nullptr;
	}

	// Call the factory function
    try {
        return cacheResource(facIt->second(resourceName), resourceType, resourceName, parent);
    } catch (const Resource::StillLoadingException&) {
        exception = std::current_exception(); return nullptr;
    } catch (const std::exception& e) {
		LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what());
        cacheResource(nullptr, resourceType, resourceName, parent);
        exception = std::current_exception(); return nullptr;
    }
}