IdNameMap.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. namespace Rml {
  32. namespace Core {
  33. template <typename ID>
  34. class IdNameMap {
  35. std::vector<String> name_map; // IDs are indices into the name_map
  36. UnorderedMap<String, ID> reverse_map;
  37. protected:
  38. IdNameMap(size_t num_ids_to_reserve) {
  39. static_assert((int)ID::Invalid == 0, "Invalid id must be zero");
  40. name_map.reserve(num_ids_to_reserve);
  41. reverse_map.reserve(num_ids_to_reserve);
  42. AddPair(ID::Invalid, "invalid");
  43. }
  44. public:
  45. void AddPair(ID id, const String& name) {
  46. // Should only be used for defined IDs
  47. if ((size_t)id >= name_map.size())
  48. name_map.resize(1 + (size_t)id);
  49. name_map[(size_t)id] = name;
  50. bool inserted = reverse_map.emplace(name, id).second;
  51. RMLUI_ASSERT(inserted);
  52. (void)inserted;
  53. }
  54. void AssertAllInserted(ID number_of_defined_ids) const {
  55. std::ptrdiff_t cnt = std::count_if(name_map.begin(), name_map.end(), [](const String& name) { return !name.empty(); });
  56. RMLUI_ASSERT(cnt == (std::ptrdiff_t)number_of_defined_ids && reverse_map.size() == (size_t)number_of_defined_ids);
  57. (void)cnt;
  58. }
  59. ID GetId(const String& name) const
  60. {
  61. auto it = reverse_map.find(name);
  62. if (it != reverse_map.end())
  63. return it->second;
  64. return ID::Invalid;
  65. }
  66. const String& GetName(ID id) const
  67. {
  68. if (static_cast<size_t>(id) < name_map.size())
  69. return name_map[static_cast<size_t>(id)];
  70. return name_map[static_cast<size_t>(ID::Invalid)];
  71. }
  72. ID GetOrCreateId(const String& name)
  73. {
  74. // All predefined properties must be set before possibly adding custom properties here
  75. RMLUI_ASSERT(name_map.size() == reverse_map.size());
  76. ID next_id = static_cast<ID>(name_map.size());
  77. // Only insert if not already in list
  78. auto pair = reverse_map.emplace(name, next_id);
  79. const auto& it = pair.first;
  80. bool inserted = pair.second;
  81. if (inserted)
  82. name_map.push_back(name);
  83. // Return the property id that already existed, or the new one if inserted
  84. return it->second;
  85. }
  86. };
  87. class PropertyIdNameMap : public IdNameMap<PropertyId> {
  88. public:
  89. PropertyIdNameMap(size_t reserve_num_properties) : IdNameMap(reserve_num_properties) {}
  90. };
  91. class ShorthandIdNameMap : public IdNameMap<ShorthandId> {
  92. public:
  93. ShorthandIdNameMap(size_t reserve_num_shorthands) : IdNameMap(reserve_num_shorthands) {}
  94. };
  95. }
  96. }
  97. #endif