Advertisement
SolidDesu

Untitled

Mar 13th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. template<typename T>
  2. class Resource
  3. {
  4. public:
  5.     static Data<T> Create(std::string name);
  6.     static Data<T> Get(std::string name);
  7.     static void Free(Data<T> resource);
  8. private:
  9.     static bool ResourceExists(std::string name){ return resources.find(name) != resources.end(); }
  10.     static std::map<std::string, Data<T>> resources;
  11. };
  12.  
  13. template<typename T>
  14. std::map<std::string, Data<T>> Resource<T>::resources;
  15.  
  16. template<typename T>
  17. Data<T> Resource<T>::Create(std::string name)
  18. {
  19.     Data<T> data;
  20.     if (ResourceExists(name))
  21.     {
  22.         data = Data<T>();
  23.     }
  24.     else
  25.     {
  26.         data = Data<T>::Create();
  27.         resources.insert(std::make_pair(name, data));
  28.     }
  29.     return data;
  30. }
  31. template<typename T>
  32. Data<T> Resource<T>::Get(std::string name)
  33. {
  34.     Data<T> data;
  35.     if (ResourceExists(name))
  36.     {
  37.         data = resources[name];
  38.     }
  39.     else
  40.     {
  41.         File file = File::Open(name, File::READ);
  42.         T value = ResourceFactory::Create<T>(file);
  43.         data = Data<T>::Create(value);
  44.     }
  45.     return data;
  46. }
  47. template<typename T>
  48. void Resource<T>::Free(Data<T> resource)
  49. {
  50.     resources.erase(resource->GetName());
  51.     resource->Release();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement