PropertiesIterator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 RMLUI_CORE_PROPERTIESITERATOR_H
  29. #define RMLUI_CORE_PROPERTIESITERATOR_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include "../../Include/RmlUi/Core/PropertyIdSet.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. if (it_style != it_style_end)
  46. // We iterate over the local style properties first
  47. ++it_style;
  48. else
  49. // .. and then over the properties given by the element's definition
  50. ++it_definition;
  51. // If we reached the end of one of the iterator pairs, we need to continue iteration on the next pair.
  52. ProceedToNextValid();
  53. return *this;
  54. }
  55. ValueType operator*() const
  56. {
  57. if (it_style != it_style_end)
  58. return { it_style->first, it_style->second };
  59. return { it_definition->first, it_definition->second };
  60. }
  61. bool AtEnd() const {
  62. return at_end;
  63. }
  64. private:
  65. PropertyIdSet iterated_properties;
  66. PropertyIt it_style, it_style_end;
  67. PropertyIt it_definition, it_definition_end;
  68. bool at_end = false;
  69. inline bool IsDirtyRemove(PropertyId id)
  70. {
  71. if (!iterated_properties.Contains(id))
  72. {
  73. iterated_properties.Insert(id);
  74. return true;
  75. }
  76. return false;
  77. }
  78. inline void ProceedToNextValid()
  79. {
  80. for (; it_style != it_style_end; ++it_style)
  81. {
  82. if (IsDirtyRemove(it_style->first))
  83. return;
  84. }
  85. for (; it_definition != it_definition_end; ++it_definition)
  86. {
  87. if (IsDirtyRemove(it_definition->first))
  88. return;
  89. }
  90. // All iterators are now at the end
  91. at_end = true;
  92. }
  93. };
  94. } // namespace Rml
  95. #endif