/* * ===================================================================================== * * Filename: ResourceHandler.hpp * * Description: * * Created: 17/01/2018 19:16:19 * * Author: Quentin Bazin, * * ===================================================================================== */ #ifndef RESOURCEHANDLER_HPP_ #define RESOURCEHANDLER_HPP_ #include #include #include "Exception.hpp" #include "IResourceLoader.hpp" #include "XMLFile.hpp" class ResourceHandler { public: template T &add(const std::string &name, Args &&...args) { if(has(name)) { throw EXCEPTION("A resource of type", typeid(T).name(), "already exists with name:", name); } m_resources.emplace(name, std::make_shared(std::forward(args)...)); return get(name); } bool has(const std::string &name) { return m_resources.count(name) == 1; } template T &get(const std::string &name) { auto it = m_resources.find(name); if(it == m_resources.end()) { throw EXCEPTION("Unable to find resource with name:", name); } return *std::static_pointer_cast(it->second); } template static auto loadConfigFile(const char *xmlFilename) -> typename std::enable_if::value>::type { ResourceLoader loader; loader.load(xmlFilename, getInstance()); } static ResourceHandler &getInstance(); static void setInstance(ResourceHandler &handler); private: static ResourceHandler *instance; std::map> m_resources; }; #endif // RESOURCEHANDLER_HPP_