Advertisement
SilverTES

Collection Test

Oct 30th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <map>
  4. #include <vector>
  5.  
  6. using Entity = int;
  7.  
  8. namespace Tool
  9. {
  10.     bool debug = false;
  11.     void setDebug(bool state)
  12.     {
  13.         debug = state;
  14.     }
  15.     template <typename M>
  16.     void log (M msg)
  17.     {
  18.         if (debug)
  19.             std::cout << msg ;
  20.     }
  21.     void ln ()
  22.     {
  23.         if (debug)
  24.             std::cout << std::endl;
  25.     }
  26. }
  27.  
  28. class ContentImage
  29. {
  30. public:
  31.  
  32.     ContentImage(std::string name)
  33.     {
  34.         _name = name;
  35.         _w = 0;
  36.         _h = 0;
  37.         _filename = "noname";
  38.         Tool::log("+++ Image : ");
  39.         Tool::log(_name);
  40.         Tool::ln();
  41.     }
  42.     virtual ~ContentImage()
  43.     {
  44.         Tool::log ("--- Image : ");
  45.         Tool::log(_name);
  46.         Tool::ln();
  47.     }
  48.  
  49.     void showMe()
  50.     {
  51.         using namespace Tool;
  52.         log("=== ShowMe ===");ln();
  53.         log(" name = ");log(_name);ln();
  54.         log(" filename = ");log(_filename);ln();
  55.         log(" w = ");log(_w);ln();
  56.         log(" h = ");log(_h);ln();
  57.         log("=== ====== ===");
  58.     }
  59. private:
  60.     std::string _name;
  61.     int _w;
  62.     int _h;
  63.     std::string _filename;
  64. };
  65.  
  66. using PImage = std::shared_ptr<ContentImage>;
  67. using MapImage = std::map<std::string, std::shared_ptr<ContentImage>>;
  68.  
  69. namespace Content
  70. {
  71.  
  72.     namespace Image
  73.     {
  74.  
  75.         MapImage mapAsset;
  76.  
  77.         void add (std::string name)
  78.         {
  79.             //auto p = std::make_shared<ContentImage>(name);
  80.             std::shared_ptr<ContentImage> p(new ContentImage(name));
  81.  
  82.             mapAsset.insert(std::make_pair(name, p) );
  83.         }
  84.  
  85.         void del (std::string name)
  86.         {
  87.             mapAsset.erase(name);
  88.         }
  89.  
  90.         const std::shared_ptr<ContentImage>& getData(std::string name)
  91.         {
  92.             MapImage::const_iterator it = mapAsset.find(name);
  93.             if (it == mapAsset.end())
  94.                 return NULL;
  95.             else
  96.                 return it->second;
  97.  
  98.         }
  99.  
  100.         void debuglog()
  101.         {
  102.             Tool::log("---- Debug ContentImage ----");
  103.             Tool::ln();
  104.             for (auto & it : mapAsset)
  105.             {
  106.                 std::cout << "name = " << it.first << std::endl;
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112.  
  113. int main()
  114. {
  115.     Tool::setDebug(true);
  116.  
  117.     Content::Image::add("test");
  118.     Content::Image::add("mugen");
  119.     Content::Image::add("silver");
  120.     Content::Image::debuglog();
  121.  
  122.     Tool::ln();
  123.  
  124.     Content::Image::del("test");
  125.     Content::Image::debuglog();
  126.  
  127.     Tool::ln();
  128.  
  129.     //PImage myImage = Content::Image::getData("test");
  130.  
  131.     //myImage->showMe();
  132.  
  133.     Tool::log("Bye !");
  134.     Tool::ln();
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement