summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/dom/TypedElementDescendantIterator.h
blob: 66d38cd91bc478970ce696b9d8048cbbfe34b77a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef TypedElementDescendantIterator_h
#define TypedElementDescendantIterator_h

#include "ElementIterator.h"

namespace WebCore {

template<typename ElementType> class DoubleTypedElementDescendantIterator;

template <typename ElementType>
class TypedElementDescendantIterator : public ElementIterator<ElementType> {
public:
    TypedElementDescendantIterator(const ContainerNode& root);
    TypedElementDescendantIterator(const ContainerNode& root, ElementType* current);
    TypedElementDescendantIterator& operator++();
};

template <typename ElementType>
class TypedElementDescendantConstIterator : public ElementConstIterator<ElementType>  {
public:
    TypedElementDescendantConstIterator(const ContainerNode& root);
    TypedElementDescendantConstIterator(const ContainerNode& root, const ElementType* current);
    TypedElementDescendantConstIterator& operator++();
};

template <typename ElementType>
class TypedElementDescendantIteratorAdapter {
public:
    TypedElementDescendantIteratorAdapter(ContainerNode& root);
    TypedElementDescendantIterator<ElementType> begin();
    TypedElementDescendantIterator<ElementType> end();
    TypedElementDescendantIterator<ElementType> beginAt(ElementType&);
    TypedElementDescendantIterator<ElementType> from(Element&);

    ElementType* first();
    ElementType* last();

private:
    ContainerNode& m_root;
};

template <typename ElementType>
class TypedElementDescendantConstIteratorAdapter {
public:
    TypedElementDescendantConstIteratorAdapter(const ContainerNode& root);
    TypedElementDescendantConstIterator<ElementType> begin() const;
    TypedElementDescendantConstIterator<ElementType> end() const;
    TypedElementDescendantConstIterator<ElementType> beginAt(const ElementType&) const;
    TypedElementDescendantConstIterator<ElementType> from(const Element&) const;

    const ElementType* first() const;
    const ElementType* last() const;

private:
    const ContainerNode& m_root;
};

template<typename ElementType> class DoubleTypedElementDescendantIteratorAdapter {
public:
    typedef TypedElementDescendantIteratorAdapter<ElementType> SingleAdapter;
    typedef DoubleTypedElementDescendantIterator<ElementType> Iterator;

    DoubleTypedElementDescendantIteratorAdapter(SingleAdapter&&, SingleAdapter&&);
    Iterator begin();
    Iterator end();

private:
    std::pair<SingleAdapter, SingleAdapter> m_pair;
};

template<typename ElementType> class DoubleTypedElementDescendantIterator {
public:
    typedef TypedElementDescendantIterator<ElementType> SingleIterator;
    typedef std::pair<ElementType&, ElementType&> ReferenceProxy;

    DoubleTypedElementDescendantIterator(SingleIterator&&, SingleIterator&&);
    ReferenceProxy operator*() const;
    bool operator==(const DoubleTypedElementDescendantIterator&) const;
    bool operator!=(const DoubleTypedElementDescendantIterator&) const;
    DoubleTypedElementDescendantIterator& operator++();

private:
    std::pair<SingleIterator, SingleIterator> m_pair;
};

template <typename ElementType> TypedElementDescendantIteratorAdapter<ElementType> descendantsOfType(ContainerNode&);
template <typename ElementType> TypedElementDescendantConstIteratorAdapter<ElementType> descendantsOfType(const ContainerNode&);

// This must only be used when both sets of descendants are known to be the same length.
// If they are different lengths, this will stop when the shorter one reaches the end, but also an assertion will fail.
template<typename ElementType> DoubleTypedElementDescendantIteratorAdapter<ElementType> descendantsOfType(ContainerNode& firstRoot, ContainerNode& secondRoot);

// TypedElementDescendantIterator

template <typename ElementType>
inline TypedElementDescendantIterator<ElementType>::TypedElementDescendantIterator(const ContainerNode& root)
    : ElementIterator<ElementType>(&root)
{
}

template <typename ElementType>
inline TypedElementDescendantIterator<ElementType>::TypedElementDescendantIterator(const ContainerNode& root, ElementType* current)
    : ElementIterator<ElementType>(&root, current)
{
}

template <typename ElementType>
inline TypedElementDescendantIterator<ElementType>& TypedElementDescendantIterator<ElementType>::operator++()
{
    return static_cast<TypedElementDescendantIterator<ElementType>&>(ElementIterator<ElementType>::traverseNext());
}

// TypedElementDescendantConstIterator

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType>::TypedElementDescendantConstIterator(const ContainerNode& root)
    : ElementConstIterator<ElementType>(&root)

{
}

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType>::TypedElementDescendantConstIterator(const ContainerNode& root, const ElementType* current)
    : ElementConstIterator<ElementType>(&root, current)
{
}

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType>& TypedElementDescendantConstIterator<ElementType>::operator++()
{
    return static_cast<TypedElementDescendantConstIterator<ElementType>&>(ElementConstIterator<ElementType>::traverseNext());
}

// TypedElementDescendantIteratorAdapter

template <typename ElementType>
inline TypedElementDescendantIteratorAdapter<ElementType>::TypedElementDescendantIteratorAdapter(ContainerNode& root)
    : m_root(root)
{
}

template <typename ElementType>
inline TypedElementDescendantIterator<ElementType> TypedElementDescendantIteratorAdapter<ElementType>::begin()
{
    return TypedElementDescendantIterator<ElementType>(m_root, Traversal<ElementType>::firstWithin(m_root));
}

template <typename ElementType>
inline TypedElementDescendantIterator<ElementType> TypedElementDescendantIteratorAdapter<ElementType>::end()
{
    return TypedElementDescendantIterator<ElementType>(m_root);
}
    
template <typename ElementType>
inline TypedElementDescendantIterator<ElementType> TypedElementDescendantIteratorAdapter<ElementType>::beginAt(ElementType& descendant)
{
    ASSERT(descendant.isDescendantOf(&m_root));
    return TypedElementDescendantIterator<ElementType>(m_root, &descendant);
}

template <typename ElementType>
inline TypedElementDescendantIterator<ElementType> TypedElementDescendantIteratorAdapter<ElementType>::from(Element& descendant)
{
    ASSERT(descendant.isDescendantOf(&m_root));
    if (is<ElementType>(descendant))
        return TypedElementDescendantIterator<ElementType>(m_root, downcast<ElementType>(&descendant));
    ElementType* next = Traversal<ElementType>::next(descendant, &m_root);
    return TypedElementDescendantIterator<ElementType>(m_root, next);
}

template <typename ElementType>
inline ElementType* TypedElementDescendantIteratorAdapter<ElementType>::first()
{
    return Traversal<ElementType>::firstWithin(m_root);
}

template <typename ElementType>
inline ElementType* TypedElementDescendantIteratorAdapter<ElementType>::last()
{
    return Traversal<ElementType>::lastWithin(m_root);
}

// TypedElementDescendantConstIteratorAdapter

template <typename ElementType>
inline TypedElementDescendantConstIteratorAdapter<ElementType>::TypedElementDescendantConstIteratorAdapter(const ContainerNode& root)
    : m_root(root)
{
}

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType> TypedElementDescendantConstIteratorAdapter<ElementType>::begin() const
{
    return TypedElementDescendantConstIterator<ElementType>(m_root, Traversal<ElementType>::firstWithin(m_root));
}

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType> TypedElementDescendantConstIteratorAdapter<ElementType>::end() const
{
    return TypedElementDescendantConstIterator<ElementType>(m_root);
}

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType> TypedElementDescendantConstIteratorAdapter<ElementType>::beginAt(const ElementType& descendant) const
{
    ASSERT(descendant.isDescendantOf(&m_root));
    return TypedElementDescendantConstIterator<ElementType>(m_root, &descendant);
}

template <typename ElementType>
inline TypedElementDescendantConstIterator<ElementType> TypedElementDescendantConstIteratorAdapter<ElementType>::from(const Element& descendant) const
{
    ASSERT(descendant.isDescendantOf(&m_root));
    if (is<ElementType>(descendant))
        return TypedElementDescendantConstIterator<ElementType>(m_root, downcast<ElementType>(&descendant));
    const ElementType* next = Traversal<ElementType>::next(descendant, &m_root);
    return TypedElementDescendantConstIterator<ElementType>(m_root, next);
}

template <typename ElementType>
inline const ElementType* TypedElementDescendantConstIteratorAdapter<ElementType>::first() const
{
    return Traversal<ElementType>::firstWithin(m_root);
}

template <typename ElementType>
inline const ElementType* TypedElementDescendantConstIteratorAdapter<ElementType>::last() const
{
    return Traversal<ElementType>::lastWithin(m_root);
}

// DoubleTypedElementDescendantIteratorAdapter

template<typename ElementType> inline DoubleTypedElementDescendantIteratorAdapter<ElementType>::DoubleTypedElementDescendantIteratorAdapter(SingleAdapter&& first, SingleAdapter&& second)
    : m_pair(WTFMove(first), WTFMove(second))
{
}

template<typename ElementType> inline auto DoubleTypedElementDescendantIteratorAdapter<ElementType>::begin() -> Iterator
{
    return Iterator(m_pair.first.begin(), m_pair.second.begin());
}

template<typename ElementType> inline auto DoubleTypedElementDescendantIteratorAdapter<ElementType>::end() -> Iterator
{
    return Iterator(m_pair.first.end(), m_pair.second.end());
}

// DoubleTypedElementDescendantIterator

template<typename ElementType> inline DoubleTypedElementDescendantIterator<ElementType>::DoubleTypedElementDescendantIterator(SingleIterator&& first, SingleIterator&& second)
    : m_pair(WTFMove(first), WTFMove(second))
{
}

template<typename ElementType> inline auto DoubleTypedElementDescendantIterator<ElementType>::operator*() const -> ReferenceProxy
{
    return { *m_pair.first, *m_pair.second };
}

template<typename ElementType> inline bool DoubleTypedElementDescendantIterator<ElementType>::operator==(const DoubleTypedElementDescendantIterator& other) const
{
    ASSERT((m_pair.first == other.m_pair.first) == (m_pair.second == other.m_pair.second));
    return m_pair.first == other.m_pair.first || m_pair.second == other.m_pair.second;
}

template<typename ElementType> inline bool DoubleTypedElementDescendantIterator<ElementType>::operator!=(const DoubleTypedElementDescendantIterator& other) const
{
    return !(*this == other);
}

template<typename ElementType> inline DoubleTypedElementDescendantIterator<ElementType>& DoubleTypedElementDescendantIterator<ElementType>::operator++()
{
    ++m_pair.first;
    ++m_pair.second;
    return *this;
}

// Standalone functions

template <typename ElementType>
inline TypedElementDescendantIteratorAdapter<ElementType> descendantsOfType(ContainerNode& root)
{
    return TypedElementDescendantIteratorAdapter<ElementType>(root);
}

template <typename ElementType>
inline TypedElementDescendantConstIteratorAdapter<ElementType> descendantsOfType(const ContainerNode& root)
{
    return TypedElementDescendantConstIteratorAdapter<ElementType>(root);
}

template<typename ElementType> inline DoubleTypedElementDescendantIteratorAdapter<ElementType> descendantsOfType(ContainerNode& firstRoot, ContainerNode& secondRoot)
{
    return { descendantsOfType<ElementType>(firstRoot), descendantsOfType<ElementType>(secondRoot) };
}

}

#endif