Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <string>
- #include <map>
- #include <assert.h>
- template <typename Resource, typename Identifier>
- class ResourceHolder
- {
- public:
- void load(Identifier id, const std::string& fileName);
- template<typename Parameter>
- void load(Identifier id, const std::string& fileName, const Parameter& secondParam);
- Resource& get(Identifier id);
- const Resource& get(Identifier id) const;
- private:
- std::map<Identifier, std::unique_ptr<Resource>> _resourceMap;
- };
- // Implementation
- // ---------------
- template <typename Resource, typename Identifier>
- void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& fileName)
- {
- std::unique_ptr<Resource> resource(new Resource());
- if (resource->loadFromFile(fileName) == false)
- {
- throw std::runtime_error("ResourceHolder failed to load" + fileName);
- }
- auto inserted = _resourceMap.insert(
- std::make_pair(id, std::move(resource)));
- assert(inserted.second);
- }
- template<typename Resource, typename Identifier>
- Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
- {
- auto found = _resourceMap.find(id);
- assert(("Could not find resource in map!", found != _resourceMap.end()));
- return *found->second;
- }
- template<typename Resource, typename Identifier>
- const Resource& ResourceHolder<Resource, Identifier>::get(Identifier id) const
- {
- auto found = _resourceMap.find(id);
- assert(("Could not find resource in map!", found != _resourceMap.end()));
- return *found->second;
- }
- template <typename Resource, typename Identifier>
- template <typename Parameter>
- void ResourceHolder<Resource, Identifier>::load(
- Identifier id,
- const std::string& fileName,
- const Parameter& secondParam)
- {
- std::unique_ptr<Resource> resource(new Resource());
- if (resource->loadFromFile(fileName, secondParam))
- {
- throw std::runtime_error("ResourceHolder failed to load" + fileName);
- }
- auto inserted = _resourceMap.insert(
- std::make_pair(id, std::move(resource)));
- assert(inserted.second);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement