From 213f947d0907858cace470736c15f87caa934591 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 14 Nov 2024 17:01:10 +0100 Subject: fix resource manager --- src/crepe/api/ResourceManager.hpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src/crepe/api/ResourceManager.hpp') diff --git a/src/crepe/api/ResourceManager.hpp b/src/crepe/api/ResourceManager.hpp index 9cd4bcb..62cac20 100644 --- a/src/crepe/api/ResourceManager.hpp +++ b/src/crepe/api/ResourceManager.hpp @@ -1,24 +1,27 @@ #pragma once +#include +#include + #include "ResourceManager.h" namespace crepe { -template -std::shared_ptr ResourceManager::cache(const std::string & file_path, - bool reload) { - auto it = asset_cache.find(file_path); +template +T & ResourceManager::cache(const Asset & asset) { + using namespace std; + static_assert(is_base_of::value, "cache must recieve a derivative class of Resource"); - if (!reload && it != asset_cache.end()) { - return std::any_cast>(it->second); - } + if (!this->resources.contains(asset)) + this->resources[asset] = make_unique(asset); - std::shared_ptr new_asset - = std::make_shared(file_path.c_str()); + Resource * resource = this->resources.at(asset).get(); + T * concrete_resource = dynamic_cast(resource); - asset_cache[file_path] = new_asset; + if (concrete_resource == nullptr) + throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path())); - return new_asset; + return *concrete_resource; } } // namespace crepe -- cgit v1.2.3