UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
resourcecontainer.h
Go to the documentation of this file.
1 #ifndef UGDK_RESOURCE_RESOURCECONTAINER_H_
2 #define UGDK_RESOURCE_RESOURCECONTAINER_H_
3 
4 #include <string>
5 #include <memory>
6 #include <unordered_map>
7 
8 #ifdef DEBUG
9 #include <cstdio>
10 #include <typeinfo>
11 #define TOSTRING(X) typeid(X).name()
12 #else
13 #define TOSTRING(X) "<UNKTYPE>"
14 #endif
15 
16 #include <ugdk/system/config.h>
17 #include <ugdk/debug/log.h>
18 
19 namespace ugdk {
20 namespace resource {
21 
22 template <class T>
24  public:
25  static ResourceContainer* Get() {
26  return Storage().get();
27  }
28  static void Clear() {
29  Storage().reset();
30  }
31  template<class ...Args>
32  static void Create(Args... args) {
33  Storage().reset(new ResourceContainer(std::forward<Args>(args)...));
34  }
35 
37 
38  void Insert(const std::string& tag, T* val);
39  void Replace(const std::string& tag, T* val);
40  bool Exists(const std::string& tag) const;
41  T* Find(const std::string& tag);
42  T* Load(const std::string& filepath, const std::string& tag);
43 
44  protected:
45  static std::unique_ptr<ResourceContainer>& Storage() {
46  static std::unique_ptr<ResourceContainer> ptr;
47  return ptr;
48  }
49 
50  private:
51  using DataMap = std::unordered_map < std::string, std::unique_ptr<T> >;
52  using Loader_T = T* (*)(const std::string&);
53 
54  DataMap database_;
55  Loader_T loader_;
56 
57  ResourceContainer() : loader_(T::LoadFromFile) {}
58  ResourceContainer(Loader_T loader) : loader_(loader) {}
59 };
60 
61 template<class T>
62 void ResourceContainer<T>::Insert(const std::string& tag, T* val) {
63  auto& pos = database_[tag];
64  debug::DebugConditionalLog(!pos, debug::LogLevel::ERROR, "UGDK",
65  "ResourceContainer<", TOSTRING(T), "> - Tag '", tag, "' already exists.");
66  pos.reset(val);
67 }
68 
69 template<class T>
70 void ResourceContainer<T>::Replace(const std::string& tag, T* val) {
71  database_[tag].reset(val);
72 }
73 
74 template<class T>
75 bool ResourceContainer<T>::Exists(const std::string& tag) const {
76  typename DataMap::const_iterator it = database_.find(tag);
77  return (it != database_.end() && it->second);
78 }
79 
80 template<class T>
81 T* ResourceContainer<T>::Find(const std::string& tag) {
82  return database_[tag].get();
83 }
84 
86 template<class T>
87 T* ResourceContainer<T>::Load(const std::string& filepath, const std::string& tag) {
88  if (Exists(tag)) return Find(tag);
89  T* obj = loader_(filepath);
90  debug::DebugConditionalLog(obj != nullptr, debug::LogLevel::ERROR, "UGDK",
91  "GenericContainer<", TOSTRING(T), "> - loader_ for '", tag, "' returned nullptr.");
92  Insert(tag, obj);
93  return obj;
94 }
95 
96 template <class T>
97 inline T* NullLoad(const std::string& filepath) {
98  debug::DebugLog(debug::LogLevel::WARNING, "UGDK",
99  "NullLoad<", TOSTRING(T), "> called with path '", filepath, "'");
100  return nullptr;
101 }
102 
103 } // namespace resource
104 } // namespace ugdk
105 
106 #endif // UGDK_RESOURCE_RESOURCECONTAINER_H_
107 
bool Exists(const std::string &tag) const
Definition: resourcecontainer.h:75
void DebugConditionalLog(bool assertion, LogLevel level, const std::string &owner, Ts &&...ts)
If assertion is false, run the DebugLog command.
Definition: log.h:72
~ResourceContainer()
Definition: resourcecontainer.h:36
void DebugLog(LogLevel level, const std::string &owner, Ts &&...ts)
Definition: log.h:60
T * Find(const std::string &tag)
Definition: resourcecontainer.h:81
static ResourceContainer * Get()
Definition: resourcecontainer.h:25
Definition: animation.h:11
static std::unique_ptr< ResourceContainer > & Storage()
Definition: resourcecontainer.h:45
void Insert(const std::string &tag, T *val)
Definition: resourcecontainer.h:62
#define TOSTRING(X)
Definition: resourcecontainer.h:13
void Replace(const std::string &tag, T *val)
Definition: resourcecontainer.h:70
T * Load(const std::string &filepath, const std::string &tag)
Uses T::Load(const std::string&) in order to Load a new object.
Definition: resourcecontainer.h:87
T * NullLoad(const std::string &filepath)
Definition: resourcecontainer.h:97
Definition: resourcecontainer.h:23
static void Create(Args...args)
Definition: resourcecontainer.h:32
static void Clear()
Definition: resourcecontainer.h:28