Guest User

Untitled

a guest
Jul 15th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #ifndef RESOURCEMANAGER_H
  2.     #define RESOURCEMANAGER_H
  3.  
  4.     #include <iostream>
  5.     #include <map>
  6.  
  7.     namespace BPE
  8.         {
  9.             template <class T>
  10.             class Resource
  11.                 {
  12.                     public:
  13.                         Resource(const std::string &, const std::string &);
  14.  
  15.                         T* const getResource() const;
  16.                         const std::string &getFilename() const;
  17.                         const std::string &getToken() const;
  18.  
  19.                     private:
  20.                         T* resource;
  21.                         std::string filename,
  22.                                     token;
  23.                 };
  24.  
  25.             template <class T>
  26.             class ResourceManager
  27.                 {
  28.                     public:
  29.                         void addResource(const std::string &, const std::string &);
  30.                         T* getResource(const std::string &) const;
  31.  
  32.                     private:
  33.                         std::map<const std::string, Resource<T> > resources;
  34.                 };
  35.  
  36.             template <class T>
  37.             Resource<T>::Resource(const std::string &fname, const std::string &tok) : resource(new T), filename(fname), token(tok)
  38.                 {
  39.                     resource -> LoadFromFile(filename);
  40.                     std::cout << "Loading: " << token << " (" << filename << ")\n";
  41.                 }
  42.  
  43.             template <class T>
  44.             T* const Resource<T>::getResource() const
  45.                 {
  46.                     return resource;
  47.                 }
  48.  
  49.             template <class T>
  50.             const std::string &Resource<T>::getFilename() const
  51.                 {
  52.                     return filename;
  53.                 }
  54.  
  55.             template <class T>
  56.             const std::string &Resource<T>::getToken() const
  57.                 {
  58.                     return token;
  59.                 }
  60.  
  61.             template <class T>
  62.             void ResourceManager<T>::addResource(const std::string &filename, const std::string &token)
  63.                 {
  64.                     resources.push_back(Resource<T>(filename, token));
  65.                 }
  66.  
  67.             template <class T>
  68.             T* ResourceManager<T>::getResource(const std::string &token) const
  69.                 {
  70.                     if (token != "")
  71.                         return resources[token];
  72.  
  73.                     return NULL;
  74.                 }
  75.         }
  76.  
  77. #endif // RESOURCEMANAGER_H
Add Comment
Please, Sign In to add comment