DescriptionIntroduce typed Node/Element iterators for range-based for loops of C++11.
Intend to replace the following typical for loop:
for (Node* child = node.firstChild(); child; child = child->nextSibling()) {
....
}
with range-based for loop of C++11:
for (Node& child : NodeTraversal::childrenOf(node)) {
....
}
Less verbose and improves readability, especially if we could close our eyes on NodeTraversal prefix.
The following iterators are introduced to address common use cases in our code base:
1. NodeTraversal::childrenOf(const Node&)
This could replace:
for (Node* child = node.firstChild(); child; child = child->nextSibling()) { ... }
with:
for (Node& child : NodeTraversal::childrenOf(node)) { ... }
2. NodeTraversal::descendantsOf(const Node&)
This could replace:
for (Node* node = root.firstChild(); node; node = NodeTraversal::next(*node, &root) { ... }
with:
for (Node& node : NodeTraversal::descendantsOf(root)) { ... }
3. NodeTraversal::inclusiveDescendantsOf(const Node&)
This could replace:
for (Node* node = &root; node; node = NodeTraversal::next(*node, &root) { ... }
with:
for (Node& node : NodeTraversal::inclusiveDescendantsOf(root)) { ... }
4. NodeTraversal::fromNext(const Node&)
This could replace:
for (Node* node = NodeTraversal::next(start); node; node = NodeTraversal::next(*node) { ... }
with:
for (Node& node : NodeTraversal::fromNext(start)) { ... }
There are also typed template versions for other element types:
5. Traversal<ElementType>::childrenOf(const Node&)
This could replace:
for (ElementType* child = Traversal<ElementType>::firstChild(node); child; child = Traversal<ElementType>::nextSibling(*child)) { ... }
with:
for (ElementType& child : Traversal<ElementType>::childrenOf(node)) { ... }
6. Traversal<ElementType>::descendantsOf(const Node&)
This could replace:
for (ElementType* node = Traversal<ElementType>::firstWithin(root); node; node = Traversal<ElementType>::next(*node, &root)) { ... }
with:
for (ElementType& node : Traversal<ElementType>::descendantsOf(root)) { ... }
7. Traversal<ElementType>::inclusiveDescendantsOf(const ElementType&)
This could replace:
for (ElementType* node = &root; node; node = Traversal<ElementType>::next(*node, &root)) { ... }
with:
for (ElementType& node : Traversal<ElementType>::inclusiveDescendantsOf(root)) { ... }
8. Traversal<ElementType>::fromNext(const Node&)
This could replace:
for (ElementType* node = Traversal<ElementType>::next(start); node; node = Traversal<ElementType>::next(*node) { ... }
with:
for (ElementType& node : Traversal<ElementType>::fromNext(start)) { ... }
New iterators should be zero overhead because compilers can make them all inlined in a release build.
As the first showcase, I let some files use this range-based for loops. This is not comprehensive so that this patch wouldn't get larger.
Other changes will be done in another patch.
We might want to have similar things for other classes, such as RenderObject, which can also be done in another patch.
BUG=none
TEST=no layout test failure
Committed: https://src.chromium.org/viewvc/blink?view=rev&revision=183872
Patch Set 1 #Patch Set 2 : Rename SiblingIterator to SiblingRange #Patch Set 3 : Experimental iterators for range-based for loop #
Total comments: 1
Patch Set 4 : Clean up #Patch Set 5 : Rename `from` to `fromNext`. Make some parameters const references. #
Total comments: 2
Messages
Total messages: 20 (6 generated)
|