Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. namespace Engine
  2. {
  3.     /// <summary>
  4.     /// Templated class for storing and searching in ID-Name-Record triples inside the game scene
  5.     /// it maps between integer ID value, string (name) and record pointer.
  6.     /// </summary>
  7.     template <typename T>
  8.     class SceneMap
  9.     {
  10.     private:
  11.         /// <summary>ID-data association</summary>
  12.         std::map<int, T*> mIdMap;
  13.  
  14.         /// <summary>Name-data association</summary>
  15.         std::map<std::string, T*> mNameMap;
  16.  
  17.         /// <summary>ID-Name association</summary>
  18.         std::map<int, std::string> mIdNameMap;
  19.  
  20.         /// <summary>Name-ID association</summary>
  21.         std::map<std::string, int> mNameIdMap;
  22.  
  23.         /// <summary>Unnamed node</summary>
  24.         std::string mUnnamed;
  25.  
  26.         /// <summary>
  27.         /// Remove record from search structure
  28.         /// </summary>
  29.         /// <param name="idMap_it">Iterator for id-data pair</param>
  30.         /// <param name="nameMap_it">Iterator for name-data pair</param>
  31.         /// <param name="idName_it">Iterator for id-name pair</param>
  32.         /// <param name="nameId_it">Iterator for name-id pair</param>
  33.         inline void Remove(typename std::map<int, T*>::iterator idMap_it,
  34.             typename std::map<std::string, T*>::iterator nameMap_it,
  35.             typename std::map<int, std::string>::iterator idName_it,
  36.             typename std::map<std::string, int>::iterator nameId_it)
  37.         {
  38.             mIdMap.erase(idMap_it);
  39.             mNameMap.erase(nameMap_it);
  40.             mIdNameMap.erase(idName_it);
  41.             mNameIdMap.erase(nameId_it);
  42.         }
  43.  
  44.     public:
  45.         /// <summary>
  46.         /// Default constructor
  47.         /// </summary>
  48.         SceneMap()
  49.         {
  50.             mUnnamed = "Unnamed";
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Add new record.
  55.         /// </summary>
  56.         /// <param name="id">Record ID</param>
  57.         /// <param name="name">Record name</param>
  58.         /// <param name="record">Pointer to data</param>
  59.         inline void Add(int id, const std::string& name, T* record)
  60.         {
  61.             mIdMap.insert(std::pair<int, T*>(id, record));
  62.             mNameMap.insert(std::pair<std::string, T*>(name, record));
  63.             mIdNameMap.insert(std::pair<int, std::string>(id, name));
  64.             mNameIdMap.insert(std::pair<std::string, int>(name, id));
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Remove record based on id. Method has to find iterators to all 4 maps
  69.         /// </summary>
  70.         /// <param name="id">ID of the record to remove</param>
  71.         inline void Remove(int id)
  72.         {
  73.             auto id_it = mIdMap.find(id);
  74.             if (id_it == mIdMap.end())
  75.             {
  76.                 return;
  77.             }
  78.  
  79.             auto id_name_it = mIdNameMap.find(id);
  80.             if (id_name_it == mIdNameMap.end())
  81.             {
  82.                 return;
  83.             }
  84.  
  85.             auto name_it = mNameMap.find(id_name_it->second);
  86.             if (name_it == mNameMap.end())
  87.             {
  88.                 return;
  89.             }
  90.  
  91.             auto name_id_it = mNameIdMap.find(id_name_it->second);
  92.             if (name_id_it == mNameIdMap.end())
  93.             {
  94.                 return;
  95.             }
  96.  
  97.             Remove(id_it, name_it, id_name_it, name_id_it);
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Remove record based on name. Method has to find iterators to all 4 maps
  102.         /// </summary>
  103.         /// <param name="name">Name of the record to remove</param>
  104.         inline void Remove(const std::string& name)
  105.         {
  106.             auto name_it = mNameMap.find(name);
  107.             if (name_it == mNameMap.end())
  108.             {
  109.                 return;
  110.             }
  111.  
  112.             auto name_id_it = mNameIdMap.find(name);
  113.             if (name_id_it == mNameIdMap.end())
  114.             {
  115.                 return;
  116.             }
  117.  
  118.             auto id_it = mIdMap.find(name_id_it->second);
  119.             if (id_it == mIdMap.end())
  120.             {
  121.                 return;
  122.             }
  123.  
  124.             auto id_name_it = mIdNameMap.find(name_id_it->second);
  125.             if (id_name_it == mIdNameMap.end())
  126.             {
  127.                 return;
  128.             }
  129.  
  130.             Remove(id_it, name_it, id_name_it, name_id_it);
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Get record based on ID
  135.         /// </summary>
  136.         /// <param name="id">Specifies ID of the record</param>
  137.         inline T* Get(int id)
  138.         {
  139.             auto id_it = mIdMap.find(id);
  140.             if (id_it == mIdMap.end())
  141.             {
  142.                 return NULL;
  143.             }
  144.  
  145.             return id_it->second;
  146.         }
  147.  
  148.         /// <summary>
  149.         /// Get record based on name
  150.         /// </summary>
  151.         /// <param name="name">Specifies name of the record</param>
  152.         inline T* Get(const std::string& name)
  153.         {
  154.             auto name_it = mNameMap.find(name);
  155.             if (name_it == mNameMap.end())
  156.             {
  157.                 return NULL;
  158.             }
  159.  
  160.             return name_it->second;
  161.         }
  162.  
  163.         /// <summary>
  164.         /// Get ID by entity pointer
  165.         /// </summary>
  166.         /// <param name="e">Pointer to entity</param>
  167.         inline int GetID(Entity* e)
  168.         {
  169.             unsigned int id = 0;
  170.             for (auto it = mIdMap.begin();
  171.                 it != mIdMap.end();
  172.                 it++)
  173.             {
  174.                 if (it->second == e)
  175.                 {
  176.                     id = it->first;
  177.                     break;
  178.                 }
  179.             }
  180.             return id;
  181.         }
  182.  
  183.         /// <summary>
  184.         /// Get name by ID
  185.         /// </summary>
  186.         inline const std::string& GetName(int id)
  187.         {
  188.             auto it = mIdNameMap.find(id);
  189.             if (it != mIdNameMap.end())
  190.             {
  191.                 return it->second;
  192.             }
  193.  
  194.             return mUnnamed;
  195.         }
  196.  
  197.         /// <summary>
  198.         /// Set name for ID
  199.         /// </summary>
  200.         inline void SetName(int id, const std::string& name)
  201.         {
  202.             T* entity = Get(id);
  203.  
  204.             if (entity != nullptr)
  205.             {
  206.                 mIdNameMap[id] = name;
  207.  
  208.                 for (auto it = mNameIdMap.begin(); it != mNameIdMap.end(); it++)
  209.                 {
  210.                     if (it->second == id)
  211.                     {
  212.                         mNameIdMap.erase(it);
  213.                         mNameIdMap.insert(std::pair<std::string, int>(name, id));
  214.                         break;
  215.                     }
  216.                 }
  217.  
  218.                 for (auto it = mNameMap.begin(); it != mNameMap.end(); it++)
  219.                 {
  220.                     if (it->second == entity)
  221.                     {
  222.                         mNameMap.erase(it);
  223.                         mNameMap.insert(std::pair<std::string, T*>(name, entity));
  224.                         break;
  225.                     }
  226.                 }
  227.             }          
  228.         }
  229.  
  230.         ALIGNED_NEW_DELETE("Engine::SceneMap")
  231.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement