You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.1KB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef NAMEIDMAPPING_HEADER
  17. #define NAMEIDMAPPING_HEADER
  18. #include <string>
  19. #include <iostream>
  20. #include <set>
  21. #include "irrlichttypes_bloated.h"
  22. #include "util/cpp11_container.h"
  23. class NameIdMapping
  24. {
  25. public:
  26. void serialize(std::ostream &os) const;
  27. void deSerialize(std::istream &is);
  28. void clear()
  29. {
  30. m_id_to_name.clear();
  31. m_name_to_id.clear();
  32. }
  33. void set(u16 id, const std::string &name)
  34. {
  35. m_id_to_name[id] = name;
  36. m_name_to_id[name] = id;
  37. }
  38. void removeId(u16 id)
  39. {
  40. std::string name;
  41. bool found = getName(id, name);
  42. if (!found)
  43. return;
  44. m_id_to_name.erase(id);
  45. m_name_to_id.erase(name);
  46. }
  47. void eraseName(const std::string &name)
  48. {
  49. u16 id;
  50. bool found = getId(name, id);
  51. if (!found)
  52. return;
  53. m_id_to_name.erase(id);
  54. m_name_to_id.erase(name);
  55. }
  56. bool getName(u16 id, std::string &result) const
  57. {
  58. UNORDERED_MAP<u16, std::string>::const_iterator i;
  59. i = m_id_to_name.find(id);
  60. if (i == m_id_to_name.end())
  61. return false;
  62. result = i->second;
  63. return true;
  64. }
  65. bool getId(const std::string &name, u16 &result) const
  66. {
  67. UNORDERED_MAP<std::string, u16>::const_iterator i;
  68. i = m_name_to_id.find(name);
  69. if (i == m_name_to_id.end())
  70. return false;
  71. result = i->second;
  72. return true;
  73. }
  74. u16 size() const { return m_id_to_name.size(); }
  75. private:
  76. UNORDERED_MAP<u16, std::string> m_id_to_name;
  77. UNORDERED_MAP<std::string, u16> m_name_to_id;
  78. };
  79. #endif