tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FilteredNodeIterator.h (1334B)


      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/. */
      6 
      7 /**
      8 * Implementation of a generic filtered iterator over nodes.
      9 */
     10 
     11 #ifndef mozilla_dom_FilteredNodeIterator_h
     12 #define mozilla_dom_FilteredNodeIterator_h
     13 
     14 #include "nsINode.h"
     15 
     16 namespace mozilla::dom {
     17 
     18 template <typename T, typename Iter>
     19 class FilteredNodeIterator : public Iter {
     20 public:
     21  explicit FilteredNodeIterator(const nsINode& aNode) : Iter(aNode) {
     22    EnsureValid();
     23  }
     24 
     25  FilteredNodeIterator& begin() { return *this; }
     26  using Iter::end;
     27 
     28  void operator++() {
     29    Iter::operator++();
     30    EnsureValid();
     31  }
     32 
     33  using Iter::operator!=;
     34 
     35  T* operator*() {
     36    nsINode* node = Iter::operator*();
     37    MOZ_ASSERT(!node || T::FromNode(node));
     38    return static_cast<T*>(node);
     39  }
     40 
     41 private:
     42  void EnsureValid() {
     43    while (true) {
     44      nsINode* node = Iter::operator*();
     45      if (!node || T::FromNode(node)) {
     46        return;
     47      }
     48      Iter::operator++();
     49    }
     50  }
     51 
     52  FilteredNodeIterator() : Iter() {}
     53 };
     54 
     55 }  // namespace mozilla::dom
     56 
     57 #endif  // mozilla_dom_FilteredNodeIterator.h