GeometryDatabase.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. int size() const
  67. {
  68. return (int)geometry_list.size() - (int)free_list.size();
  69. }
  70. void clear() {
  71. geometry_list.clear();
  72. free_list.clear();
  73. }
  74. void erase(GeometryDatabaseHandle handle)
  75. {
  76. free_list.push_back(handle);
  77. }
  78. // Iterate over every item in the database, skipping free slots.
  79. template<typename Func>
  80. void for_each(Func func)
  81. {
  82. std::sort(free_list.begin(), free_list.end());
  83. size_t i_begin_next = 0;
  84. for (GeometryDatabaseHandle freelist_entry : free_list)
  85. {
  86. const size_t i_end = size_t(freelist_entry);
  87. const size_t i_begin = i_begin_next;
  88. i_begin_next = i_end + 1;
  89. for (size_t i = i_begin; i < i_end; i++)
  90. func(geometry_list[i]);
  91. }
  92. for (size_t i = i_begin_next; i < geometry_list.size(); i++)
  93. func(geometry_list[i]);
  94. }
  95. private:
  96. // List of all active geometry, in addition to free slots.
  97. // Free slots (as defined by the 'free_list') may contain dangling pointers and must not be dereferenced.
  98. Vector<Geometry*> geometry_list;
  99. // Declares free slots in the 'geometry_list' as indices.
  100. Vector<GeometryDatabaseHandle> free_list;
  101. };
  102. static Database geometry_database;
  103. GeometryDatabaseHandle Insert(Geometry* geometry)
  104. {
  105. return geometry_database.insert(geometry);
  106. }
  107. void Erase(GeometryDatabaseHandle handle)
  108. {
  109. geometry_database.erase(handle);
  110. }
  111. void ReleaseAll()
  112. {
  113. geometry_database.for_each([](Geometry* geometry) {
  114. geometry->Release();
  115. });
  116. }
  117. #ifdef RMLUI_TESTS_ENABLED
  118. bool PrepareForTests()
  119. {
  120. if (geometry_database.size() > 0)
  121. return false;
  122. // Even with size()==0 we can have items in the geometry list which should all be duplicated by the free list. We want to clear them for the tests.
  123. geometry_database.clear();
  124. return true;
  125. }
  126. bool ListMatchesDatabase(const Vector<Geometry>& geometry_list)
  127. {
  128. int i = 0;
  129. bool result = true;
  130. geometry_database.for_each([&geometry_list, &i, &result](Geometry* geometry) {
  131. result &= (geometry == &geometry_list[i++]);
  132. });
  133. return result;
  134. }
  135. #endif // RMLUI_TESTS_ENABLED
  136. } // namespace GeometryDatabase
  137. } // namespace Rml