DirtyPropertyList.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 RMLUICOREDIRTYPROPERTYLIST_H
  29. #define RMLUICOREDIRTYPROPERTYLIST_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include "../../Include/RmlUi/Core/ID.h"
  32. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  33. #include <bitset>
  34. namespace Rml {
  35. namespace Core {
  36. class DirtyPropertyList {
  37. private:
  38. static constexpr size_t N = (size_t)PropertyId::NumDefinedIds;
  39. std::bitset<N> dirty_set;
  40. PropertyNameList dirty_custom_properties;
  41. public:
  42. DirtyPropertyList(bool all_dirty = false)
  43. {
  44. static_assert((size_t)PropertyId::Invalid == 0, "DirtyPropertyList makes an assumption that PropertyId::Invalid is zero.");
  45. if (all_dirty)
  46. DirtyAll();
  47. }
  48. void Insert(PropertyId id) {
  49. if ((size_t)id < N)
  50. dirty_set.set((size_t)id);
  51. else
  52. dirty_custom_properties.insert(id);
  53. }
  54. void Insert(const PropertyNameList& properties) {
  55. if (dirty_set.test(0)) return;
  56. for (auto id : properties)
  57. Insert(id);
  58. }
  59. void DirtyAll() {
  60. // We are using PropertyId::Invalid (0) as a flag for all properties dirty.
  61. // However, we set the whole set so that we don't have to do the extra check
  62. // for the first bit in Contains().
  63. dirty_set.set();
  64. }
  65. void Clear() {
  66. dirty_set.reset();
  67. dirty_custom_properties.clear();
  68. }
  69. void Remove(PropertyId id) {
  70. if ((size_t)id < N)
  71. dirty_set.reset((size_t)id);
  72. else
  73. dirty_custom_properties.erase(id);
  74. }
  75. bool Empty() const {
  76. return dirty_set.none() && dirty_custom_properties.empty();
  77. }
  78. bool Contains(PropertyId id) const {
  79. if ((size_t)id < N)
  80. return dirty_set.test((size_t)id);
  81. else if (dirty_set.test(0))
  82. return true;
  83. else
  84. return dirty_custom_properties.count(id) == 1;
  85. }
  86. bool IsAllDirty() const {
  87. return dirty_set.test(0);
  88. }
  89. // Union with another set
  90. DirtyPropertyList& operator|=(const DirtyPropertyList& other)
  91. {
  92. dirty_set |= other.dirty_set;
  93. if (other.dirty_custom_properties.size() > 0)
  94. dirty_custom_properties.insert(other.dirty_custom_properties.begin(), other.dirty_custom_properties.end());
  95. return *this;
  96. }
  97. // Intersection with another set
  98. DirtyPropertyList& operator&=(const DirtyPropertyList& other)
  99. {
  100. dirty_set &= other.dirty_set;
  101. if (dirty_custom_properties.size() > 0 && other.dirty_custom_properties.size() > 0 && !dirty_set.test(0))
  102. {
  103. for (auto it = dirty_custom_properties.begin(); it != dirty_custom_properties.end();)
  104. if (other.dirty_custom_properties.count(*it) == 0)
  105. it = dirty_custom_properties.erase(it);
  106. else
  107. ++it;
  108. }
  109. else
  110. {
  111. dirty_custom_properties.clear();
  112. }
  113. return *this;
  114. }
  115. PropertyNameList ToPropertyList() const {
  116. if (IsAllDirty())
  117. return StyleSheetSpecification::GetRegisteredProperties();
  118. PropertyNameList property_list = dirty_custom_properties;
  119. property_list.reserve(dirty_set.count());
  120. for (size_t i = 1; i < N; i++)
  121. if (dirty_set.test(i))
  122. property_list.insert((PropertyId)i);
  123. return property_list;
  124. }
  125. };
  126. }
  127. }
  128. #endif