GeometryDatabase.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include "GeometryDatabase.h"
  29. #include "../../Include/RmlUi/Core/Geometry.h"
  30. #include <algorithm>
  31. namespace Rml {
  32. namespace GeometryDatabase {
  33. class Database {
  34. public:
  35. Database() {
  36. constexpr size_t reserve_size = 512;
  37. geometry_list.reserve(reserve_size);
  38. free_list.reserve(reserve_size);
  39. }
  40. ~Database() {
  41. #ifdef RMLUI_TESTS_ENABLED
  42. RMLUI_ASSERT(geometry_list.size() == free_list.size());
  43. std::sort(free_list.begin(), free_list.end());
  44. for (size_t i = 0; i < free_list.size(); i++)
  45. {
  46. RMLUI_ASSERT(i == free_list[i]);
  47. }
  48. #endif
  49. }
  50. GeometryDatabaseHandle insert(Geometry* value)
  51. {
  52. GeometryDatabaseHandle handle;
  53. if (free_list.empty())
  54. {
  55. handle = GeometryDatabaseHandle(geometry_list.size());
  56. geometry_list.push_back(value);
  57. }
  58. else
  59. {
  60. handle = free_list.back();
  61. free_list.pop_back();
  62. geometry_list[handle] = value;
  63. }
  64. return handle;
  65. }
  66. void erase(GeometryDatabaseHandle handle)
  67. {
  68. free_list.push_back(handle);
  69. }
  70. // Iterate over every item in the database, skipping free slots.
  71. template<typename Func>
  72. void for_each(Func func)
  73. {
  74. std::sort(free_list.begin(), free_list.end());
  75. size_t i_begin_next = 0;
  76. for (GeometryDatabaseHandle freelist_entry : free_list)
  77. {
  78. const size_t i_end = size_t(freelist_entry);
  79. const size_t i_begin = i_begin_next;
  80. i_begin_next = i_end + 1;
  81. for (size_t i = i_begin; i < i_end; i++)
  82. func(geometry_list[i]);
  83. }
  84. for (size_t i = i_begin_next; i < geometry_list.size(); i++)
  85. func(geometry_list[i]);
  86. }
  87. private:
  88. // List of all active geometry, in addition to free slots.
  89. // Free slots (as defined by the 'free_list') may contain dangling pointers and must not be dereferenced.
  90. std::vector<Geometry*> geometry_list;
  91. // Declares free slots in the 'geometry_list' as indices.
  92. std::vector<GeometryDatabaseHandle> free_list;
  93. };
  94. static Database geometry_database;
  95. GeometryDatabaseHandle Insert(Geometry* geometry)
  96. {
  97. return geometry_database.insert(geometry);
  98. }
  99. void Erase(GeometryDatabaseHandle handle)
  100. {
  101. geometry_database.erase(handle);
  102. }
  103. void ReleaseAll()
  104. {
  105. geometry_database.for_each([](Geometry* geometry) {
  106. geometry->Release();
  107. });
  108. }
  109. } // namespace GeometryDatabase
  110. } // namespace Rml