PropertiesIterator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 RMLUICOREPROPERTIESITERATOR_H
  29. #define RMLUICOREPROPERTIESITERATOR_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include "DirtyPropertyList.h"
  32. namespace Rml {
  33. namespace Core {
  34. // An iterator for local properties defined on an element.
  35. // Note: Modifying the underlying style invalidates the iterator.
  36. class PropertiesIterator {
  37. public:
  38. using ValueType = std::pair<PropertyId, const Property&>;
  39. using PropertyIt = PropertyMap::const_iterator;
  40. PropertiesIterator(const PseudoClassList& element_pseudo_classes, PropertyIt it_style, PropertyIt it_style_end, PropertyIt it_definition, PropertyIt it_definition_end)
  41. : element_pseudo_classes(&element_pseudo_classes), it_style(it_style), it_style_end(it_style_end), it_definition(it_definition), it_definition_end(it_definition_end)
  42. {
  43. ProceedToNextValid();
  44. }
  45. PropertiesIterator& operator++() {
  46. if (it_style != it_style_end)
  47. // We iterate over the local style properties first
  48. ++it_style;
  49. else
  50. // .. and then over the properties given by the element's definition
  51. ++it_definition;
  52. // If we reached the end of one of the iterator pairs, we need to continue iteration on the next pair.
  53. ProceedToNextValid();
  54. return *this;
  55. }
  56. ValueType operator*() const
  57. {
  58. if (it_style != it_style_end)
  59. return { it_style->first, it_style->second };
  60. return { it_definition->first, it_definition->second };
  61. }
  62. bool AtEnd() const {
  63. return at_end;
  64. }
  65. // Return the list of pseudo classes which defines the current property, possibly null.
  66. const PseudoClassList* GetPseudoClassList() const
  67. {
  68. // TODO: Not implemented
  69. return nullptr;
  70. }
  71. private:
  72. const PseudoClassList* element_pseudo_classes;
  73. DirtyPropertyList iterated_properties;
  74. PropertyIt it_style, it_style_end;
  75. PropertyIt it_definition, it_definition_end;
  76. bool at_end = false;
  77. inline bool IsDirtyRemove(PropertyId id)
  78. {
  79. if (!iterated_properties.Contains(id))
  80. {
  81. iterated_properties.Insert(id);
  82. return true;
  83. }
  84. return false;
  85. }
  86. inline void ProceedToNextValid()
  87. {
  88. for (; it_style != it_style_end; ++it_style)
  89. {
  90. if (IsDirtyRemove(it_style->first))
  91. return;
  92. }
  93. for (; it_definition != it_definition_end; ++it_definition)
  94. {
  95. if (IsDirtyRemove(it_definition->first))
  96. return;
  97. }
  98. // All iterators are now at the end
  99. at_end = true;
  100. }
  101. };
  102. }
  103. }
  104. #endif