2
0

IdNameMap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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 RMLUICOREIDNAMEMAP_H
  29. #define RMLUICOREIDNAMEMAP_H
  30. #include "../../Include/RmlUi/Core/Header.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. #include <algorithm>
  33. namespace Rml {
  34. namespace Core {
  35. template <typename ID>
  36. class IdNameMap {
  37. std::vector<String> name_map; // IDs are indices into the name_map
  38. UnorderedMap<String, ID> reverse_map;
  39. protected:
  40. IdNameMap(size_t num_ids_to_reserve) {
  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. void 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. RMLUI_ASSERT(cnt == (std::ptrdiff_t)number_of_defined_ids && reverse_map.size() == (size_t)number_of_defined_ids);
  61. (void)number_of_defined_ids;
  62. (void)cnt;
  63. }
  64. ID GetId(const String& name) const
  65. {
  66. auto it = reverse_map.find(name);
  67. if (it != reverse_map.end())
  68. return it->second;
  69. return ID::Invalid;
  70. }
  71. const String& GetName(ID id) const
  72. {
  73. if (static_cast<size_t>(id) < name_map.size())
  74. return name_map[static_cast<size_t>(id)];
  75. return name_map[static_cast<size_t>(ID::Invalid)];
  76. }
  77. ID GetOrCreateId(const String& name)
  78. {
  79. // All predefined properties must be set before possibly adding custom properties here
  80. RMLUI_ASSERT(name_map.size() == reverse_map.size());
  81. ID next_id = static_cast<ID>(name_map.size());
  82. // Only insert if not already in list
  83. auto pair = reverse_map.emplace(name, next_id);
  84. const auto& it = pair.first;
  85. bool inserted = pair.second;
  86. if (inserted)
  87. name_map.push_back(name);
  88. // Return the property id that already existed, or the new one if inserted
  89. return it->second;
  90. }
  91. };
  92. class PropertyIdNameMap : public IdNameMap<PropertyId> {
  93. public:
  94. PropertyIdNameMap(size_t reserve_num_properties) : IdNameMap(reserve_num_properties) {}
  95. };
  96. class ShorthandIdNameMap : public IdNameMap<ShorthandId> {
  97. public:
  98. ShorthandIdNameMap(size_t reserve_num_shorthands) : IdNameMap(reserve_num_shorthands) {}
  99. };
  100. }
  101. }
  102. #endif