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 // Iterates over the deps of a target. 14 // 15 // Since there are multiple kinds of deps, this iterator allows looping over 16 // each one in one loop. 17 class DepsIterator { 18 public: 19 enum LinkedOnly { 20 LINKED_ONLY, 21 }; 22 23 // Iterate over public, private, and data deps. 24 explicit DepsIterator(const Target* t); 25 26 // Iterate over the public and private linked deps, but not the data deps. 27 DepsIterator(const Target* t, LinkedOnly); 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 // 36 // 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 38 // one. 39 void Advance(); 40 41 // The current dependency. 42 const LabelTargetPair& pair() const { 43 DCHECK_LT(current_index_, vect_stack_[0]->size()); 44 return (*vect_stack_[0])[current_index_]; 45 } 46 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: 54 const LabelTargetVector* vect_stack_[3]; 55 56 size_t current_index_; 57 58 DISALLOW_COPY_AND_ASSIGN(DepsIterator); 59 }; 60 61 #endif // TOOLS_GN_DEPS_ITERATOR_H_ 62