Advertisement
SilverTES

Simple Ressource Manager : C++

Feb 5th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3.  
  4. static size_t indexAsset = 0;
  5.  
  6. struct Image
  7. {
  8.     int _w;
  9.     int _h;
  10. };
  11.  
  12.  
  13. struct Asset
  14. {
  15.     int _id = 0;
  16.     std::string _name;
  17.  
  18.     Asset()
  19.     {
  20.         _id = indexAsset;
  21.         ++indexAsset;
  22.     }
  23.  
  24.     ~Asset()
  25.     {
  26.         std::cout << "- Delete Asset : "<< _name << "\n";
  27.     }
  28.  
  29. };
  30.  
  31. struct Bitmap : public Asset
  32. {
  33.     Image *_data = nullptr;
  34.  
  35.     Bitmap(std::string name, int w, int h)
  36.     {
  37.         _name = name;
  38.         _data = new Image{w,h};
  39.     }
  40.     ~Bitmap()
  41.     {
  42.         if (_data != nullptr)
  43.             delete _data;
  44.     }
  45.  
  46.     Image *getData()
  47.     {
  48.         return _data;
  49.     }
  50. };
  51.  
  52. struct Font : public Asset
  53. {
  54.     Font(std::string name)
  55.     {
  56.         _name = name;
  57.     }
  58.     ~Font()
  59.     {
  60.  
  61.     }
  62. };
  63.  
  64. struct AssetManager
  65. {
  66.     std::unordered_map<std::string, Asset*> _mapAsset;
  67.  
  68.     ~AssetManager()
  69.     {
  70.         if (!_mapAsset.empty())
  71.         {
  72.             auto it = _mapAsset.begin();
  73.  
  74.             while (it != _mapAsset.end())
  75.             {
  76.                 if (it->second != nullptr)
  77.                 {
  78.  
  79.                     delete it->second;
  80.                     it->second = nullptr;
  81.                     //it = _mapAsset.erase(it);
  82.                 }
  83.  
  84.                 ++it;
  85.             }
  86.  
  87.             _mapAsset.clear();
  88.         }
  89.  
  90.     }
  91.  
  92.     void add(Asset *content)
  93.     {
  94.         _mapAsset.insert(std::pair<std::string, Asset*>(content->_name, content) );
  95.     }
  96.  
  97.     bool del(std::string name)
  98.     {
  99.         auto it = _mapAsset.find(name);
  100.  
  101.         if (it == _mapAsset.end())
  102.         {
  103.             std::cout << "Not found : "<< name << "\n";
  104.             return false;
  105.         }
  106.  
  107.         if (it->second != nullptr)
  108.         {
  109.             delete it->second;
  110.             it->second = nullptr;
  111.         }
  112.  
  113.         _mapAsset.erase(it);
  114.         //std::cout << "item found : "<< name << "\n";
  115.         return true;
  116.  
  117.     }
  118.  
  119.  
  120.     template <class C>
  121.     C *get(std::string name)
  122.     {
  123.         auto it = _mapAsset.find(name);
  124.  
  125.         if (it != _mapAsset.end())
  126.             return static_cast<C*>(it->second);
  127.  
  128.         return nullptr;
  129.     }
  130.  
  131.     void showAll()
  132.     {
  133.         auto it = _mapAsset.begin();
  134.  
  135.         while (it != _mapAsset.end())
  136.         {
  137.             if ((*it).second != nullptr)
  138.                 std::cout << "[ "<< it->second->_name << " id = " << it->second->_id << " ]\n";
  139.  
  140.             ++it;
  141.         }
  142.     }
  143.  
  144. };
  145.  
  146.  
  147.  
  148. int main()
  149. {
  150.  
  151.     AssetManager _assetBank;
  152.  
  153.  
  154.     _assetBank.add(new Bitmap("Mugen", 640, 360));
  155.     _assetBank.add(new Font("mainFont"));
  156.     _assetBank.add(new Bitmap("Silver", 480, 160));
  157.  
  158.     _assetBank.showAll();
  159.  
  160.     Bitmap *myImage = nullptr;
  161.  
  162.  
  163.     myImage = _assetBank.get<Bitmap>("Mugen");
  164.  
  165.     std::cout << myImage->_data->_w << "\n";
  166.  
  167.  
  168.     Font *myFont = _assetBank.get<Font>("mainFont");
  169.  
  170.     std::cout << "myFont name = " << myFont->_name << "\n";
  171.  
  172.     _assetBank.del("Silver");
  173.  
  174.     _assetBank.showAll();
  175.  
  176.  
  177. //    if (myImage)
  178. //        delete myImage;
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement