| 1 | /* |
| 2 | * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "RenderIterator.h" |
| 29 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | template <typename T> |
| 33 | class RenderChildIterator : public RenderIterator<T> { |
| 34 | public: |
| 35 | RenderChildIterator(const RenderElement& parent); |
| 36 | RenderChildIterator(const RenderElement& parent, T* current); |
| 37 | RenderChildIterator& operator++(); |
| 38 | }; |
| 39 | |
| 40 | template <typename T> |
| 41 | class RenderChildConstIterator : public RenderConstIterator<T> { |
| 42 | public: |
| 43 | RenderChildConstIterator(const RenderElement& parent); |
| 44 | RenderChildConstIterator(const RenderElement& parent, const T* current); |
| 45 | RenderChildConstIterator& operator++(); |
| 46 | }; |
| 47 | |
| 48 | template <typename T> |
| 49 | class RenderChildIteratorAdapter { |
| 50 | public: |
| 51 | RenderChildIteratorAdapter(RenderElement& parent); |
| 52 | RenderChildIterator<T> begin(); |
| 53 | RenderChildIterator<T> end(); |
| 54 | T* first(); |
| 55 | T* last(); |
| 56 | |
| 57 | private: |
| 58 | RenderElement& m_parent; |
| 59 | }; |
| 60 | |
| 61 | template <typename T> |
| 62 | class RenderChildConstIteratorAdapter { |
| 63 | public: |
| 64 | RenderChildConstIteratorAdapter(const RenderElement& parent); |
| 65 | RenderChildConstIterator<T> begin() const; |
| 66 | RenderChildConstIterator<T> end() const; |
| 67 | const T* first() const; |
| 68 | const T* last() const; |
| 69 | |
| 70 | private: |
| 71 | const RenderElement& m_parent; |
| 72 | }; |
| 73 | |
| 74 | template <typename T> RenderChildIteratorAdapter<T> childrenOfType(RenderElement&); |
| 75 | template <typename T> RenderChildConstIteratorAdapter<T> childrenOfType(const RenderElement&); |
| 76 | |
| 77 | // RenderChildIterator |
| 78 | |
| 79 | template <typename T> |
| 80 | inline RenderChildIterator<T>::RenderChildIterator(const RenderElement& parent) |
| 81 | : RenderIterator<T>(&parent) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | template <typename T> |
| 86 | inline RenderChildIterator<T>::RenderChildIterator(const RenderElement& parent, T* current) |
| 87 | : RenderIterator<T>(&parent, current) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | template <typename T> |
| 92 | inline RenderChildIterator<T>& RenderChildIterator<T>::operator++() |
| 93 | { |
| 94 | return static_cast<RenderChildIterator<T>&>(RenderIterator<T>::traverseNextSibling()); |
| 95 | } |
| 96 | |
| 97 | // RenderChildConstIterator |
| 98 | |
| 99 | template <typename T> |
| 100 | inline RenderChildConstIterator<T>::RenderChildConstIterator(const RenderElement& parent) |
| 101 | : RenderConstIterator<T>(&parent) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | template <typename T> |
| 106 | inline RenderChildConstIterator<T>::RenderChildConstIterator(const RenderElement& parent, const T* current) |
| 107 | : RenderConstIterator<T>(&parent, current) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | template <typename T> |
| 112 | inline RenderChildConstIterator<T>& RenderChildConstIterator<T>::operator++() |
| 113 | { |
| 114 | return static_cast<RenderChildConstIterator<T>&>(RenderConstIterator<T>::traverseNextSibling()); |
| 115 | } |
| 116 | |
| 117 | // RenderChildIteratorAdapter |
| 118 | |
| 119 | template <typename T> |
| 120 | inline RenderChildIteratorAdapter<T>::RenderChildIteratorAdapter(RenderElement& parent) |
| 121 | : m_parent(parent) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | template <typename T> |
| 126 | inline RenderChildIterator<T> RenderChildIteratorAdapter<T>::begin() |
| 127 | { |
| 128 | return RenderChildIterator<T>(m_parent, RenderTraversal::firstChild<T>(m_parent)); |
| 129 | } |
| 130 | |
| 131 | template <typename T> |
| 132 | inline RenderChildIterator<T> RenderChildIteratorAdapter<T>::end() |
| 133 | { |
| 134 | return RenderChildIterator<T>(m_parent); |
| 135 | } |
| 136 | |
| 137 | template <typename T> |
| 138 | inline T* RenderChildIteratorAdapter<T>::first() |
| 139 | { |
| 140 | return RenderTraversal::firstChild<T>(m_parent); |
| 141 | } |
| 142 | |
| 143 | template <typename T> |
| 144 | inline T* RenderChildIteratorAdapter<T>::last() |
| 145 | { |
| 146 | return RenderTraversal::lastChild<T>(m_parent); |
| 147 | } |
| 148 | |
| 149 | // RenderChildConstIteratorAdapter |
| 150 | |
| 151 | template <typename T> |
| 152 | inline RenderChildConstIteratorAdapter<T>::RenderChildConstIteratorAdapter(const RenderElement& parent) |
| 153 | : m_parent(parent) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | inline RenderChildConstIterator<T> RenderChildConstIteratorAdapter<T>::begin() const |
| 159 | { |
| 160 | return RenderChildConstIterator<T>(m_parent, RenderTraversal::firstChild<T>(m_parent)); |
| 161 | } |
| 162 | |
| 163 | template <typename T> |
| 164 | inline RenderChildConstIterator<T> RenderChildConstIteratorAdapter<T>::end() const |
| 165 | { |
| 166 | return RenderChildConstIterator<T>(m_parent); |
| 167 | } |
| 168 | |
| 169 | template <typename T> |
| 170 | inline const T* RenderChildConstIteratorAdapter<T>::first() const |
| 171 | { |
| 172 | return RenderTraversal::firstChild<T>(m_parent); |
| 173 | } |
| 174 | |
| 175 | template <typename T> |
| 176 | inline const T* RenderChildConstIteratorAdapter<T>::last() const |
| 177 | { |
| 178 | return RenderTraversal::lastChild<T>(m_parent); |
| 179 | } |
| 180 | |
| 181 | // Standalone functions |
| 182 | |
| 183 | template <typename T> |
| 184 | inline RenderChildIteratorAdapter<T> childrenOfType(RenderElement& parent) |
| 185 | { |
| 186 | return RenderChildIteratorAdapter<T>(parent); |
| 187 | } |
| 188 | |
| 189 | template <typename T> |
| 190 | inline RenderChildConstIteratorAdapter<T> childrenOfType(const RenderElement& parent) |
| 191 | { |
| 192 | return RenderChildConstIteratorAdapter<T>(parent); |
| 193 | } |
| 194 | |
| 195 | } // namespace WebCore |
| 196 | |