Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / gn / deps_iterator.h
blob1c3a75e141747c3faa5e86bb0e3f8db5255853a3
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 "base/basictypes.h"
9 #include "tools/gn/label_ptr.h"
11 class Target;
13 // Provides an iterator for iterating over multiple LabelTargetVectors to
14 // make it convenient to iterate over all deps of a target.
16 // This works by maintaining a simple stack of vectors (since we have a fixed
17 // number of deps types). When the stack is empty, we've reached the end. This
18 // means that the default-constructed iterator == end() for any sequence.
19 class DepsIterator {
20 public:
21 // Creates an empty iterator.
22 DepsIterator();
24 // Iterate over the deps in the given vectors. If passing less than three,
25 // pad with nulls.
26 DepsIterator(const LabelTargetVector* a,
27 const LabelTargetVector* b,
28 const LabelTargetVector* c);
30 // Prefix increment operator. This assumes there are more items (i.e.
31 // *this != DepsIterator()).
33 // For internal use, this function tolerates an initial index equal to the
34 // length of the current vector. In this case, it will advance to the next
35 // one.
36 DepsIterator& operator++();
38 // Comparison for STL-based loops.
39 bool operator!=(const DepsIterator& other) {
40 return current_index_ != other.current_index_ ||
41 vect_stack_[0] != other.vect_stack_[0] ||
42 vect_stack_[1] != other.vect_stack_[1] ||
43 vect_stack_[2] != other.vect_stack_[2];
46 // Dereference operator for STL-compatible iterators.
47 const LabelTargetPair& operator*() const {
48 DCHECK_LT(current_index_, vect_stack_[0]->size());
49 return (*vect_stack_[0])[current_index_];
52 private:
53 const LabelTargetVector* vect_stack_[3];
55 size_t current_index_;
58 // Provides a virtual container implementing begin() and end() for a
59 // sequence of deps. This can then be used in range-based for loops.
60 class DepsIteratorRange {
61 public:
62 explicit DepsIteratorRange(const DepsIterator& b);
63 ~DepsIteratorRange();
65 const DepsIterator& begin() const { return begin_; }
66 const DepsIterator& end() const { return end_; }
68 private:
69 DepsIterator begin_;
70 DepsIterator end_;
73 #endif // TOOLS_GN_DEPS_ITERATOR_H_