diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-14 17:01:10 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-14 17:01:10 +0100 |
commit | 213f947d0907858cace470736c15f87caa934591 (patch) | |
tree | 2d15878761eefb84e5ccdfa75a91646d9fab4aa2 /src/crepe/api/ResourceManager.hpp | |
parent | 431b0bd7c6c502b42bb5be5488371d8c475e7024 (diff) |
fix resource manager
Diffstat (limited to 'src/crepe/api/ResourceManager.hpp')
-rw-r--r-- | src/crepe/api/ResourceManager.hpp | 25 |
1 files changed, 14 insertions, 11 deletions
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 <stdexcept> +#include <format> + #include "ResourceManager.h" namespace crepe { -template <typename asset> -std::shared_ptr<asset> ResourceManager::cache(const std::string & file_path, - bool reload) { - auto it = asset_cache.find(file_path); +template <typename T> +T & ResourceManager::cache(const Asset & asset) { + using namespace std; + static_assert(is_base_of<Resource, T>::value, "cache must recieve a derivative class of Resource"); - if (!reload && it != asset_cache.end()) { - return std::any_cast<std::shared_ptr<asset>>(it->second); - } + if (!this->resources.contains(asset)) + this->resources[asset] = make_unique<T>(asset); - std::shared_ptr<asset> new_asset - = std::make_shared<asset>(file_path.c_str()); + Resource * resource = this->resources.at(asset).get(); + T * concrete_resource = dynamic_cast<T *>(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 |