PropertiesIterator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREPROPERTIESITERATOR_H
  28. #define ROCKETCOREPROPERTIESITERATOR_H
  29. #include "../../Include/Rocket/Core/Types.h"
  30. #include "DirtyPropertyList.h"
  31. #include "ElementDefinition.h"
  32. namespace Rocket {
  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. using PseudoIt = PseudoClassPropertyDictionary::const_iterator;
  41. PropertiesIterator(const PseudoClassList& element_pseudo_classes, PropertyIt it_style, PropertyIt it_style_end, PseudoIt it_pseudo, PseudoIt it_pseudo_end, PropertyIt it_base, PropertyIt it_base_end)
  42. : element_pseudo_classes(&element_pseudo_classes), it_style(it_style), it_style_end(it_style_end), it_pseudo(it_pseudo), it_pseudo_end(it_pseudo_end), it_base(it_base), it_base_end(it_base_end)
  43. {
  44. if (this->element_pseudo_classes->empty())
  45. this->it_pseudo = this->it_pseudo_end;
  46. ProceedToNextValid();
  47. }
  48. PropertiesIterator& operator++() {
  49. if (it_style != it_style_end)
  50. // First, we iterate over the local properties
  51. ++it_style;
  52. else if (it_pseudo != it_pseudo_end)
  53. // Then, we iterate over the properties given by the pseudo classes in the element's definition
  54. ++i_pseudo_class;
  55. else
  56. // Finally, we iterate over the base properties given by the element's definition
  57. ++it_base;
  58. // If we reached the end of one of the iterator pairs, we need to continue iteration on the next pair.
  59. ProceedToNextValid();
  60. return *this;
  61. }
  62. ValueType operator*() const
  63. {
  64. if (it_style != it_style_end)
  65. return { it_style->first, it_style->second };
  66. if (it_pseudo != it_pseudo_end)
  67. return { it_pseudo->first, it_pseudo->second[i_pseudo_class].second };
  68. return { it_base->first, it_base->second };
  69. }
  70. bool AtEnd() const {
  71. return at_end;
  72. }
  73. // Return the list of pseudo classes which defines the current property, possibly null.
  74. const PseudoClassList* GetPseudoClassList() const
  75. {
  76. if (it_style == it_style_end && it_pseudo != it_pseudo_end)
  77. return &it_pseudo->second[i_pseudo_class].first;
  78. return nullptr;
  79. }
  80. private:
  81. const PseudoClassList* element_pseudo_classes;
  82. DirtyPropertyList iterated_properties;
  83. PropertyIt it_style, it_style_end;
  84. PseudoIt it_pseudo, it_pseudo_end;
  85. PropertyIt it_base, it_base_end;
  86. size_t i_pseudo_class = 0;
  87. bool at_end = false;
  88. inline bool IsDirtyRemove(PropertyId id)
  89. {
  90. if (!iterated_properties.Contains(id))
  91. {
  92. iterated_properties.Insert(id);
  93. return true;
  94. }
  95. return false;
  96. }
  97. inline void ProceedToNextValid() {
  98. for (; it_style != it_style_end; ++it_style)
  99. {
  100. if (IsDirtyRemove(it_style->first))
  101. return;
  102. }
  103. // Iterate over all the pseudo classes and match the applicable rules
  104. for (; it_pseudo != it_pseudo_end; ++it_pseudo)
  105. {
  106. }
  107. for (; it_base != it_base_end; ++it_base)
  108. {
  109. if (IsDirtyRemove(it_base->first))
  110. return;
  111. }
  112. // All iterators are now at the end
  113. at_end = true;
  114. }
  115. };
  116. }
  117. }
  118. #endif