summaryrefslogtreecommitdiff
path: root/src/resourceManager.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/resourceManager.h')
-rw-r--r--src/resourceManager.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/resourceManager.h b/src/resourceManager.h
new file mode 100644
index 0000000..be6e250
--- /dev/null
+++ b/src/resourceManager.h
@@ -0,0 +1,54 @@
+#ifndef RESOURCE_MANAGER_H
+#define RESOURCE_MANAGER_H
+
+#include <map>
+#include <string>
+#include <memory>
+#include <stdexcept>
+#include <cassert>
+
+template <typename Resource, typename Identifier>
+class ResourceManager{
+ private:
+ std::map<Identifier,std::unique_ptr<Resource>> resourceMap;
+ public:
+ void load(Identifier id, const std::string& path);
+ Resource& get(Identifier id);
+ const Resource& get(Identifier id) const;
+};
+
+template <typename Resource, typename Identifier>
+void
+ResourceManager<Resource, Identifier>::load(Identifier id,
+ const std::string& path){
+ auto resource = std::make_unique<Resource>();
+ if( !resource->loadFromFile(path) ){
+ throw("ResoureManager::load - Failed to load filename: " + path);
+ }
+ auto inserted = resourceMap.insert(
+ std::make_pair(id, std::move(resource))
+ );
+ assert(inserted.second);
+}
+
+template <typename Resource, typename Identifier>
+Resource&
+ResourceManager<Resource, Identifier>::get(Identifier id)
+{
+ auto found = resourceMap.find(id);
+ assert(found != resourceMap.end());
+
+ return *found->second;
+}
+
+template <typename Resource, typename Identifier>
+const Resource&
+ResourceManager<Resource, Identifier>::get(Identifier id) const
+{
+ auto found = resourceMap.find(id);
+ assert(found != resourceMap.end());
+
+ return *found->second;
+}
+
+#endif // RESOURCE_MANAGER_H