PropertiesIterator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-2023 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 RMLUI_CORE_PROPERTIESITERATOR_H
  29. #define RMLUI_CORE_PROPERTIESITERATOR_H
  30. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. namespace Rml {
  33. // An iterator for local properties defined on an element.
  34. // Note: Modifying the underlying style invalidates the iterator.
  35. class PropertiesIterator {
  36. public:
  37. using ValueType = Pair<PropertyId, const Property&>;
  38. using PropertyIt = PropertyMap::const_iterator;
  39. PropertiesIterator(PropertyIt it_style, PropertyIt it_style_end, PropertyIt it_definition, PropertyIt it_definition_end) :
  40. it_style(it_style), it_style_end(it_style_end), it_definition(it_definition), it_definition_end(it_definition_end)
  41. {
  42. ProceedToNextValid();
  43. }
  44. PropertiesIterator& operator++()
  45. {
  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 { return at_end; }
  63. private:
  64. PropertyIdSet iterated_properties;
  65. PropertyIt it_style, it_style_end;
  66. PropertyIt it_definition, it_definition_end;
  67. bool at_end = false;
  68. inline bool IsDirtyRemove(PropertyId id)
  69. {
  70. if (!iterated_properties.Contains(id))
  71. {
  72. iterated_properties.Insert(id);
  73. return true;
  74. }
  75. return false;
  76. }
  77. inline void ProceedToNextValid()
  78. {
  79. for (; it_style != it_style_end; ++it_style)
  80. {
  81. if (IsDirtyRemove(it_style->first))
  82. return;
  83. }
  84. for (; it_definition != it_definition_end; ++it_definition)
  85. {
  86. if (IsDirtyRemove(it_definition->first))
  87. return;
  88. }
  89. // All iterators are now at the end
  90. at_end = true;
  91. }
  92. };
  93. } // namespace Rml
  94. #endif