PropertiesIterator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "../../Include/RmlUi/Core/PropertyIdSet.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(PropertyIt it_style, PropertyIt it_style_end, PropertyIt it_definition, PropertyIt it_definition_end)
  41. : 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. private:
  66. PropertyIdSet iterated_properties;
  67. PropertyIt it_style, it_style_end;
  68. PropertyIt it_definition, it_definition_end;
  69. bool at_end = false;
  70. inline bool IsDirtyRemove(PropertyId id)
  71. {
  72. if (!iterated_properties.Contains(id))
  73. {
  74. iterated_properties.Insert(id);
  75. return true;
  76. }
  77. return false;
  78. }
  79. inline void ProceedToNextValid()
  80. {
  81. for (; it_style != it_style_end; ++it_style)
  82. {
  83. if (IsDirtyRemove(it_style->first))
  84. return;
  85. }
  86. for (; it_definition != it_definition_end; ++it_definition)
  87. {
  88. if (IsDirtyRemove(it_definition->first))
  89. return;
  90. }
  91. // All iterators are now at the end
  92. at_end = true;
  93. }
  94. };
  95. }
  96. }
  97. #endif