[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / deps_iterator.h
blobcf287185021e72425999055ee4f41977e0ac1cdb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_DEPS_ITERATOR_H_
6 #define TOOLS_GN_DEPS_ITERATOR_H_
8 #include "tools/gn/label_ptr.h"
10 class Target;
12 // Provides an iterator for iterating over multiple LabelTargetVectors to
13 // make it convenient to iterate over all deps of a target.
15 // This works by maintaining a simple stack of vectors (since we have a fixed
16 // number of deps types). When the stack is empty, we've reached the end. This
17 // means that the default-constructed iterator == end() for any sequence.
18 class DepsIterator {
19 public:
20 // Creates an empty iterator.
21 DepsIterator();
23 // Iterate over the deps in the given vectors. If passing less than three,
24 // pad with nulls.
25 DepsIterator(const LabelTargetVector* a,
26 const LabelTargetVector* b,
27 const LabelTargetVector* c);
29 // Prefix increment operator. This assumes there are more items (i.e.
30 // *this != DepsIterator()).
32 // For internal use, this function tolerates an initial index equal to the
33 // length of the current vector. In this case, it will advance to the next
34 // one.
35 DepsIterator& operator++();
37 // Comparison for STL-based loops.
38 bool operator!=(const DepsIterator& other) {
39 return current_index_ != other.current_index_ ||
40 vect_stack_[0] != other.vect_stack_[0] ||
41 vect_stack_[1] != other.vect_stack_[1] ||
42 vect_stack_[2] != other.vect_stack_[2];
45 // Dereference operator for STL-compatible iterators.
46 const LabelTargetPair& operator*() const {
47 DCHECK_LT(current_index_, vect_stack_[0]->size());
48 return (*vect_stack_[0])[current_index_];
51 private:
52 const LabelTargetVector* vect_stack_[3];
54 size_t current_index_;
57 // Provides a virtual container implementing begin() and end() for a
58 // sequence of deps. This can then be used in range-based for loops.
59 class DepsIteratorRange {
60 public:
61 explicit DepsIteratorRange(const DepsIterator& b);
62 ~DepsIteratorRange();
64 const DepsIterator& begin() const { return begin_; }
65 const DepsIterator& end() const { return end_; }
67 private:
68 DepsIterator begin_;
69 DepsIterator end_;
72 #endif // TOOLS_GN_DEPS_ITERATOR_H_