summaryrefslogtreecommitdiffstats
path: root/tools/gn/deps_iterator.h
diff options
context:
space:
mode:
authorbrettw <brettw@chromium.org>2014-09-27 15:27:10 -0700
committerCommit bot <commit-bot@chromium.org>2014-09-27 22:27:30 +0000
commita09df11aa1c5f416df4fb97f2303d9c1ff76b150 (patch)
tree6c1209f791c655b8b68fc8f91e8e8e42c3650546 /tools/gn/deps_iterator.h
parentf446a070a0aa29a153b0cf78b33ef22da84cb023 (diff)
downloadchromium_src-a09df11aa1c5f416df4fb97f2303d9c1ff76b150.zip
chromium_src-a09df11aa1c5f416df4fb97f2303d9c1ff76b150.tar.gz
chromium_src-a09df11aa1c5f416df4fb97f2303d9c1ff76b150.tar.bz2
Convert GN's deps iterator to work with range-based for loops.
Reworks DepsIterator so it is compatible with STL iterators enough to work with range-based for loops. The iterator is now created by a target rather than taking a target as an argument, which makes the loops more natural. I also changed some loops around code I was touching to use range-based. Review URL: https://codereview.chromium.org/610043002 Cr-Commit-Position: refs/heads/master@{#297122}
Diffstat (limited to 'tools/gn/deps_iterator.h')
-rw-r--r--tools/gn/deps_iterator.h66
1 files changed, 39 insertions, 27 deletions
diff --git a/tools/gn/deps_iterator.h b/tools/gn/deps_iterator.h
index 5fc3c01e..1c3a75e 100644
--- a/tools/gn/deps_iterator.h
+++ b/tools/gn/deps_iterator.h
@@ -10,52 +10,64 @@
class Target;
-// Iterates over the deps of a target.
+// Provides an iterator for iterating over multiple LabelTargetVectors to
+// make it convenient to iterate over all deps of a target.
//
-// Since there are multiple kinds of deps, this iterator allows looping over
-// each one in one loop.
+// This works by maintaining a simple stack of vectors (since we have a fixed
+// number of deps types). When the stack is empty, we've reached the end. This
+// means that the default-constructed iterator == end() for any sequence.
class DepsIterator {
public:
- enum LinkedOnly {
- LINKED_ONLY,
- };
+ // Creates an empty iterator.
+ DepsIterator();
- // Iterate over public, private, and data deps.
- explicit DepsIterator(const Target* t);
+ // Iterate over the deps in the given vectors. If passing less than three,
+ // pad with nulls.
+ DepsIterator(const LabelTargetVector* a,
+ const LabelTargetVector* b,
+ const LabelTargetVector* c);
- // Iterate over the public and private linked deps, but not the data deps.
- DepsIterator(const Target* t, LinkedOnly);
-
- // Returns true when there are no more targets.
- bool done() const {
- return !vect_stack_[0];
- }
-
- // Advance to the next position. This assumes !done().
+ // Prefix increment operator. This assumes there are more items (i.e.
+ // *this != DepsIterator()).
//
// For internal use, this function tolerates an initial index equal to the
// length of the current vector. In this case, it will advance to the next
// one.
- void Advance();
+ DepsIterator& operator++();
- // The current dependency.
- const LabelTargetPair& pair() const {
+ // Comparison for STL-based loops.
+ bool operator!=(const DepsIterator& other) {
+ return current_index_ != other.current_index_ ||
+ vect_stack_[0] != other.vect_stack_[0] ||
+ vect_stack_[1] != other.vect_stack_[1] ||
+ vect_stack_[2] != other.vect_stack_[2];
+ }
+
+ // Dereference operator for STL-compatible iterators.
+ const LabelTargetPair& operator*() const {
DCHECK_LT(current_index_, vect_stack_[0]->size());
return (*vect_stack_[0])[current_index_];
}
- // The pointer to the current dependency.
- const Target* target() const { return pair().ptr; }
-
- // The label of the current dependency.
- const Label& label() const { return pair().label; }
-
private:
const LabelTargetVector* vect_stack_[3];
size_t current_index_;
+};
+
+// Provides a virtual container implementing begin() and end() for a
+// sequence of deps. This can then be used in range-based for loops.
+class DepsIteratorRange {
+ public:
+ explicit DepsIteratorRange(const DepsIterator& b);
+ ~DepsIteratorRange();
+
+ const DepsIterator& begin() const { return begin_; }
+ const DepsIterator& end() const { return end_; }
- DISALLOW_COPY_AND_ASSIGN(DepsIterator);
+ private:
+ DepsIterator begin_;
+ DepsIterator end_;
};
#endif // TOOLS_GN_DEPS_ITERATOR_H_