Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / AncestorIterator.h
blobd8a56267fdd62132c3a76016a31765309f447d13
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * Implementation of some generic iterators over ancestor nodes.
10 * Note that these keep raw pointers to the nodes they iterate from, and as
11 * such the DOM should not be mutated while they're in use. There are debug
12 * assertions (via nsMutationGuard) that check this in debug builds.
15 #ifndef mozilla_dom_AncestorIterator_h
16 #define mozilla_dom_AncestorIterator_h
18 #include "nsINode.h"
19 #include "nsIContentInlines.h"
20 #include "FilteredNodeIterator.h"
22 namespace mozilla::dom {
24 #ifdef DEBUG
25 # define MUTATION_GUARD(class_name_) \
26 nsMutationGuard mMutationGuard; \
27 ~class_name_() { MOZ_ASSERT(!mMutationGuard.Mutated(0)); }
28 #else
29 # define MUTATION_GUARD(class_name_)
30 #endif
32 #define DEFINE_ANCESTOR_ITERATOR(name_, method_) \
33 class Inclusive##name_ { \
34 using Self = Inclusive##name_; \
36 public: \
37 explicit Inclusive##name_(const nsINode& aNode) \
38 : mCurrent(const_cast<nsINode*>(&aNode)) {} \
39 Self& begin() { return *this; } \
40 std::nullptr_t end() const { return nullptr; } \
41 bool operator!=(std::nullptr_t) const { return !!mCurrent; } \
42 void operator++() { mCurrent = mCurrent->method_(); } \
43 nsINode* operator*() { return mCurrent; } \
45 MUTATION_GUARD(Inclusive##name_) \
47 protected: \
48 explicit Inclusive##name_(nsINode* aCurrent) : mCurrent(aCurrent) {} \
49 nsINode* mCurrent; \
50 }; \
51 class name_ : public Inclusive##name_ { \
52 public: \
53 using Super = Inclusive##name_; \
54 explicit name_(const nsINode& aNode) \
55 : Inclusive##name_(aNode.method_()) {} \
56 }; \
57 template <typename T> \
58 class name_##OfTypeIterator : public FilteredNodeIterator<T, name_> { \
59 public: \
60 explicit name_##OfTypeIterator(const nsINode& aNode) \
61 : FilteredNodeIterator<T, name_>(aNode) {} \
62 }; \
63 template <typename T> \
64 class Inclusive##name_##OfTypeIterator \
65 : public FilteredNodeIterator<T, Inclusive##name_> { \
66 public: \
67 explicit Inclusive##name_##OfTypeIterator(const nsINode& aNode) \
68 : FilteredNodeIterator<T, Inclusive##name_>(aNode) {} \
71 DEFINE_ANCESTOR_ITERATOR(Ancestors, GetParentNode)
72 DEFINE_ANCESTOR_ITERATOR(FlatTreeAncestors, GetFlattenedTreeParentNode)
74 #undef MUTATION_GUARD
76 } // namespace mozilla::dom
78 template <typename T>
79 inline mozilla::dom::AncestorsOfTypeIterator<T> nsINode::AncestorsOfType()
80 const {
81 return mozilla::dom::AncestorsOfTypeIterator<T>(*this);
84 template <typename T>
85 inline mozilla::dom::InclusiveAncestorsOfTypeIterator<T>
86 nsINode::InclusiveAncestorsOfType() const {
87 return mozilla::dom::InclusiveAncestorsOfTypeIterator<T>(*this);
90 template <typename T>
91 inline mozilla::dom::FlatTreeAncestorsOfTypeIterator<T>
92 nsINode::FlatTreeAncestorsOfType() const {
93 return mozilla::dom::FlatTreeAncestorsOfTypeIterator<T>(*this);
96 template <typename T>
97 inline mozilla::dom::InclusiveFlatTreeAncestorsOfTypeIterator<T>
98 nsINode::InclusiveFlatTreeAncestorsOfType() const {
99 return mozilla::dom::InclusiveFlatTreeAncestorsOfTypeIterator<T>(*this);
102 template <typename T>
103 inline T* nsINode::FirstAncestorOfType() const {
104 return *(AncestorsOfType<T>());
107 #endif // mozilla_dom_AncestorIterator.h