Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / gn / deps_iterator.h
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 "base/basictypes.h"
9 #include "tools/gn/label_ptr.h"
10
11 class Target;
12
13 // Provides an iterator for iterating over multiple LabelTargetVectors to
14 // make it convenient to iterate over all deps of a target.
15 //
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();
23
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);
29
30   // Prefix increment operator. This assumes there are more items (i.e.
31   // *this != DepsIterator()).
32   //
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++();
37
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];
44   }
45
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_];
50   }
51
52  private:
53   const LabelTargetVector* vect_stack_[3];
54
55   size_t current_index_;
56 };
57
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_;
71 };
72
73 #endif  // TOOLS_GN_DEPS_ITERATOR_H_