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.
4 
5 #ifndef TOOLS_GN_DEPS_ITERATOR_H_
6 #define TOOLS_GN_DEPS_ITERATOR_H_
7 
8 #include <stddef.h>
9 
10 #include "tools/gn/label_ptr.h"
11 
12 // Provides an iterator for iterating over multiple LabelTargetVectors to
13 // make it convenient to iterate over all deps of a target.
14 //
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();
22 
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);
28 
29   // Prefix increment operator. This assumes there are more items (i.e.
30   // *this != DepsIterator()).
31   //
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++();
36 
37   // Comparison for STL-based loops.
38   bool operator!=(const DepsIterator& other) const {
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];
43   }
44 
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_];
49   }
50 
51  private:
52   const LabelTargetVector* vect_stack_[3];
53 
54   size_t current_index_;
55 };
56 
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();
63 
begin()64   const DepsIterator& begin() const { return begin_; }
end()65   const DepsIterator& end() const { return end_; }
66 
67  private:
68   DepsIterator begin_;
69   DepsIterator end_;
70 };
71 
72 #endif  // TOOLS_GN_DEPS_ITERATOR_H_
73