Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: tools/gn/deps_iterator.h

Issue 610043002: Convert GN's deps iterator to work with range-based for loops. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef TOOLS_GN_DEPS_ITERATOR_H_ 5 #ifndef TOOLS_GN_DEPS_ITERATOR_H_
6 #define TOOLS_GN_DEPS_ITERATOR_H_ 6 #define TOOLS_GN_DEPS_ITERATOR_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "tools/gn/label_ptr.h" 9 #include "tools/gn/label_ptr.h"
10 10
11 class Target; 11 class Target;
12 12
13 // Iterates over the deps of a target. 13 // Provides an iterator for iterating over multiple LabelTargetVectors to
14 // make it convenient to iterate over all deps of a target.
14 // 15 //
15 // Since there are multiple kinds of deps, this iterator allows looping over 16 // This works by maintaining a simple stack of vectors (since we have a fixed
16 // each one in one loop. 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.
17 class DepsIterator { 19 class DepsIterator {
18 public: 20 public:
19 enum LinkedOnly { 21 // Creates an empty iterator.
20 LINKED_ONLY, 22 DepsIterator();
21 };
22 23
23 // Iterate over public, private, and data deps. 24 // Iterate over the deps in the given vectors. If passing less than three,
24 explicit DepsIterator(const Target* t); 25 // pad with nulls.
26 DepsIterator(const LabelTargetVector* a,
27 const LabelTargetVector* b,
28 const LabelTargetVector* c);
25 29
26 // Iterate over the public and private linked deps, but not the data deps. 30 // Prefix increment operator. This assumes there are more items (i.e.
27 DepsIterator(const Target* t, LinkedOnly); 31 // *this != DepsIterator()).
28
29 // Returns true when there are no more targets.
30 bool done() const {
31 return !vect_stack_[0];
32 }
33
34 // Advance to the next position. This assumes !done().
35 // 32 //
36 // For internal use, this function tolerates an initial index equal to the 33 // For internal use, this function tolerates an initial index equal to the
37 // length of the current vector. In this case, it will advance to the next 34 // length of the current vector. In this case, it will advance to the next
38 // one. 35 // one.
39 void Advance(); 36 DepsIterator& operator++();
40 37
41 // The current dependency. 38 // Comparison for STL-based loops.
42 const LabelTargetPair& pair() const { 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];
44 }
45
46 // Dereference operator for STL-compatible iterators.
47 const LabelTargetPair& operator*() const {
43 DCHECK_LT(current_index_, vect_stack_[0]->size()); 48 DCHECK_LT(current_index_, vect_stack_[0]->size());
44 return (*vect_stack_[0])[current_index_]; 49 return (*vect_stack_[0])[current_index_];
45 } 50 }
46 51
47 // The pointer to the current dependency.
48 const Target* target() const { return pair().ptr; }
49
50 // The label of the current dependency.
51 const Label& label() const { return pair().label; }
52
53 private: 52 private:
54 const LabelTargetVector* vect_stack_[3]; 53 const LabelTargetVector* vect_stack_[3];
55 54
56 size_t current_index_; 55 size_t current_index_;
56 };
57 57
58 DISALLOW_COPY_AND_ASSIGN(DepsIterator); 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();
64
65 const DepsIterator& begin() const { return begin_; }
66 const DepsIterator& end() const { return end_; }
67
68 private:
69 DepsIterator begin_;
70 DepsIterator end_;
59 }; 71 };
60 72
61 #endif // TOOLS_GN_DEPS_ITERATOR_H_ 73 #endif // TOOLS_GN_DEPS_ITERATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698