PropertyIterators.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ROCKETCOREPROPERTYITERATORS_H
  28. #define ROCKETCOREPROPERTYITERATORS_H
  29. #include "Types.h"
  30. namespace Rocket {
  31. namespace Core {
  32. /**
  33. Provides iterators for properties defined in the element's style or definition.
  34. @author Michael R. P. Ragazzon
  35. */
  36. // Iterates over the properties of an element definition matching the given pseudo classes.
  37. // Note: Modifying the underlying definition invalidates the iterator.
  38. // Note: 'pseudo_classes' must outlive the iterator.
  39. class ROCKETCORE_API ElementDefinitionIterator {
  40. public:
  41. using difference_type = std::ptrdiff_t;
  42. using value_type = std::pair<const String&, const Property&>;
  43. using pointer = value_type*;
  44. using reference = value_type&;
  45. using iterator_category = std::input_iterator_tag;
  46. using PropertyIt = PropertyMap::const_iterator;
  47. using PseudoIt = PseudoClassPropertyDictionary::const_iterator;
  48. ElementDefinitionIterator();
  49. ElementDefinitionIterator(const StringList& pseudo_classes, PropertyIt it_properties, PseudoIt it_pseudo_class_properties, PropertyIt it_properties_end, PseudoIt it_pseudo_class_properties_end);
  50. ElementDefinitionIterator& operator++();
  51. bool operator==(const ElementDefinitionIterator& other) const { return pseudo_classes == other.pseudo_classes && it_properties == other.it_properties && it_pseudo_class_properties == other.it_pseudo_class_properties && i_pseudo_class == other.i_pseudo_class; }
  52. bool operator!=(const ElementDefinitionIterator& other) const { return !(*this == other); }
  53. value_type operator*() const
  54. {
  55. if (it_properties != it_properties_end)
  56. return { it_properties->first, it_properties->second };
  57. return { it_pseudo_class_properties->first, it_pseudo_class_properties->second[i_pseudo_class].second };
  58. }
  59. // Return the list of pseudo classes which defines the current property, possibly null
  60. const PseudoClassList* pseudo_class_list() const;
  61. private:
  62. const StringList* pseudo_classes;
  63. PropertyIt it_properties, it_properties_end;
  64. PseudoIt it_pseudo_class_properties, it_pseudo_class_properties_end;
  65. size_t i_pseudo_class = 0;
  66. void proceed_to_next_valid();
  67. };
  68. // An iterator for local properties defined on an element.
  69. // Note: Modifying the underlying style invalidates the iterator.
  70. class ROCKETCORE_API ElementStyleIterator {
  71. public:
  72. using difference_type = std::ptrdiff_t;
  73. using value_type = std::pair<const String&, const Property&>;
  74. using pointer = value_type *;
  75. using reference = value_type &;
  76. using iterator_category = std::input_iterator_tag;
  77. using PropertyIt = PropertyMap::const_iterator;
  78. using DefinitionIt = ElementDefinitionIterator;
  79. ElementStyleIterator();
  80. ElementStyleIterator(const PropertyMap* property_map, PropertyIt it_properties, DefinitionIt it_definition, PropertyIt it_properties_end, DefinitionIt it_definition_end);
  81. ElementStyleIterator& operator++();
  82. bool operator==(const ElementStyleIterator& other) const { return property_map == other.property_map && it_properties == other.it_properties && it_definition == other.it_definition; }
  83. bool operator!=(const ElementStyleIterator& other) const { return !(*this == other); }
  84. value_type operator*() const
  85. {
  86. if (it_properties != it_properties_end)
  87. return { it_properties->first, it_properties->second };
  88. return *it_definition;
  89. }
  90. // Return the list of pseudo classes which defines the current property, possibly null
  91. const PseudoClassList* pseudo_class_list() const;
  92. private:
  93. const PropertyMap* property_map;
  94. PropertyIt it_properties, it_properties_end;
  95. DefinitionIt it_definition, it_definition_end;
  96. void proceed_to_next_valid();
  97. };
  98. }
  99. }
  100. #endif