summaryrefslogtreecommitdiffstats
path: root/dom/base/SimpleTreeIterator.h
blob: 7ca50408289c1fb650b0d116546180783d4b826a (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This is a light-weight tree iterator for `for` loops when full iterator
 * functionality isn't required.
 */

#ifndef mozilla_dom_SimpleTreeIterator_h
#define mozilla_dom_SimpleTreeIterator_h

#include "nsINode.h"
#include "nsTArray.h"
#include "mozilla/dom/Element.h"

namespace mozilla {
namespace dom {

class SimpleTreeIterator {
public:
  /**
   * Initialize an iterator with aRoot. After that it can be iterated with a
   * range-based for loop. At the moment, that's the only supported form of use
   * for this iterator.
   */
  explicit SimpleTreeIterator(nsINode& aRoot)
    : mCurrent(&aRoot)
  {
    mTree.AppendElement(&aRoot);
  }

  // Basic support for range-based for loops.
  // This will modify the iterator as it goes.
  SimpleTreeIterator& begin() { return *this; }

  SimpleTreeIterator end() { return SimpleTreeIterator(); }

  bool operator!=(const SimpleTreeIterator& aOther) {
    return mCurrent != aOther.mCurrent;
  }

  void operator++() { Next(); }

  nsINode* operator*() { return mCurrent; }

private:
  // Constructor used only for end() to represent a drained iterator.
  SimpleTreeIterator()
    : mCurrent(nullptr)
  {}

  void Next() {
    MOZ_ASSERT(mCurrent, "Don't call Next() when we have no current node");

    mCurrent = mCurrent->GetNextNode(mTree.LastElement());
  }

  // The current node.
  nsINode* mCurrent;

  // The DOM tree that we're inside of right now.
  AutoTArray<nsINode*, 1> mTree;
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_SimpleTreeIterator_h