DataView.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/DataView.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. namespace Rml {
  32. namespace Core {
  33. DataView::~DataView()
  34. {}
  35. Element* DataView::GetElement() const
  36. {
  37. Element* result = attached_element.get();
  38. if (!result)
  39. Log::Message(Log::LT_WARNING, "Could not retrieve element in view, was it destroyed?");
  40. return result;
  41. }
  42. int DataView::GetElementDepth() const {
  43. return element_depth;
  44. }
  45. bool DataView::IsValid() const {
  46. return static_cast<bool>(attached_element);
  47. }
  48. DataView::DataView(Element* element) : attached_element(element->GetObserverPtr()), element_depth(0) {
  49. if (element)
  50. {
  51. for (Element* parent = element->GetParentNode(); parent; parent = parent->GetParentNode())
  52. element_depth += 1;
  53. }
  54. }
  55. DataViews::DataViews()
  56. {}
  57. DataViews::~DataViews()
  58. {}
  59. void DataViews::Add(DataViewPtr view) {
  60. views_to_add.push_back(std::move(view));
  61. }
  62. void DataViews::OnElementRemove(Element* element)
  63. {
  64. for (auto it = views.begin(); it != views.end();)
  65. {
  66. auto& view = *it;
  67. if (view && view->GetElement() == element)
  68. {
  69. views_to_remove.push_back(std::move(view));
  70. it = views.erase(it);
  71. }
  72. else
  73. ++it;
  74. }
  75. }
  76. bool DataViews::Update(DataModel& model, const DirtyVariables& dirty_variables)
  77. {
  78. bool result = false;
  79. // View updates may result in newly added views, thus we do it recursively but with an upper limit.
  80. // Without the loop, newly added views won't be updated until the next Update() call.
  81. for(int i = 0; i == 0 || (!views_to_add.empty() && i < 10); i++)
  82. {
  83. std::vector<DataView*> dirty_views;
  84. if (!views_to_add.empty())
  85. {
  86. views.reserve(views.size() + views_to_add.size());
  87. for (auto&& view : views_to_add)
  88. {
  89. dirty_views.push_back(view.get());
  90. for (const String& variable_name : view->GetVariableNameList())
  91. name_view_map.emplace(variable_name, view.get());
  92. views.push_back(std::move(view));
  93. }
  94. views_to_add.clear();
  95. }
  96. for (const String& variable_name : dirty_variables)
  97. {
  98. auto pair = name_view_map.equal_range(variable_name);
  99. for (auto it = pair.first; it != pair.second; ++it)
  100. dirty_views.push_back(it->second);
  101. }
  102. // Remove duplicate entries
  103. std::sort(dirty_views.begin(), dirty_views.end());
  104. auto it_remove = std::unique(dirty_views.begin(), dirty_views.end());
  105. dirty_views.erase(it_remove, dirty_views.end());
  106. // Sort by the element's depth in the document tree so that any structural changes due to a changed variable are reflected in the element's children.
  107. // Eg. the 'data-for' view will remove children if any of its data variable array size is reduced.
  108. std::sort(dirty_views.begin(), dirty_views.end(), [](auto&& left, auto&& right) { return left->GetElementDepth() < right->GetElementDepth(); });
  109. for (DataView* view : dirty_views)
  110. {
  111. RMLUI_ASSERT(view);
  112. if (!view)
  113. continue;
  114. if (view->IsValid())
  115. result |= view->Update(model);
  116. }
  117. // Destroy views marked for destruction
  118. // @performance: Horrible...
  119. if (!views_to_remove.empty())
  120. {
  121. for (const auto& view : views_to_remove)
  122. {
  123. for (auto it = name_view_map.begin(); it != name_view_map.end(); )
  124. {
  125. if (it->second == view.get())
  126. it = name_view_map.erase(it);
  127. else
  128. ++it;
  129. }
  130. }
  131. views_to_remove.clear();
  132. }
  133. }
  134. return result;
  135. }
  136. }
  137. }