IdNameMap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_IDNAMEMAP_H
  29. #define RMLUI_CORE_IDNAMEMAP_H
  30. #include "../../Include/RmlUi/Core/Header.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. #include <algorithm>
  33. namespace Rml {
  34. template <typename ID>
  35. class IdNameMap {
  36. Vector<String> name_map; // IDs are indices into the name_map
  37. UnorderedMap<String, ID> reverse_map;
  38. protected:
  39. IdNameMap(size_t num_ids_to_reserve)
  40. {
  41. static_assert((int)ID::Invalid == 0, "Invalid id must be zero");
  42. name_map.reserve(num_ids_to_reserve);
  43. reverse_map.reserve(num_ids_to_reserve);
  44. AddPair(ID::Invalid, "invalid");
  45. }
  46. public:
  47. void AddPair(ID id, const String& name)
  48. {
  49. // Should only be used for defined IDs
  50. if ((size_t)id >= name_map.size())
  51. name_map.resize(1 + (size_t)id);
  52. name_map[(size_t)id] = name;
  53. bool inserted = reverse_map.emplace(name, id).second;
  54. RMLUI_ASSERT(inserted);
  55. (void)inserted;
  56. }
  57. bool AssertAllInserted(ID number_of_defined_ids) const
  58. {
  59. std::ptrdiff_t cnt = std::count_if(name_map.begin(), name_map.end(), [](const String& name) { return !name.empty(); });
  60. return cnt == (std::ptrdiff_t)number_of_defined_ids && reverse_map.size() == (size_t)number_of_defined_ids;
  61. }
  62. ID GetId(const String& name) const
  63. {
  64. auto it = reverse_map.find(name);
  65. if (it != reverse_map.end())
  66. return it->second;
  67. return ID::Invalid;
  68. }
  69. const String& GetName(ID id) const
  70. {
  71. if (static_cast<size_t>(id) < name_map.size())
  72. return name_map[static_cast<size_t>(id)];
  73. return name_map[static_cast<size_t>(ID::Invalid)];
  74. }
  75. ID GetOrCreateId(const String& name)
  76. {
  77. // All predefined properties must be set before possibly adding custom properties here
  78. RMLUI_ASSERT(name_map.size() == reverse_map.size());
  79. ID next_id = static_cast<ID>(name_map.size());
  80. // Only insert if not already in list
  81. auto pair = reverse_map.emplace(name, next_id);
  82. const auto& it = pair.first;
  83. bool inserted = pair.second;
  84. if (inserted)
  85. name_map.push_back(name);
  86. // Return the property id that already existed, or the new one if inserted
  87. return it->second;
  88. }
  89. };
  90. class PropertyIdNameMap : public IdNameMap<PropertyId> {
  91. public:
  92. PropertyIdNameMap(size_t reserve_num_properties) : IdNameMap(reserve_num_properties) {}
  93. };
  94. class ShorthandIdNameMap : public IdNameMap<ShorthandId> {
  95. public:
  96. ShorthandIdNameMap(size_t reserve_num_shorthands) : IdNameMap(reserve_num_shorthands) {}
  97. };
  98. } // namespace Rml
  99. #endif