• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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_LABEL_PTR_H_
6 #define TOOLS_GN_LABEL_PTR_H_
7 
8 #include <functional>
9 
10 class ParseNode;
11 
12 // Structure that holds a labeled "thing". This is used for various places
13 // where we need to store lists of targets or configs. We sometimes populate
14 // the pointers on another thread from where we compute the labels, so this
15 // structure lets us save them separately. This also allows us to store the
16 // location of the thing that added this dependency.
17 template<typename T>
18 struct LabelPtrPair {
19   typedef T DestType;
20 
LabelPtrPairLabelPtrPair21   LabelPtrPair() : label(), ptr(NULL), origin(NULL) {}
22 
LabelPtrPairLabelPtrPair23   explicit LabelPtrPair(const Label& l) : label(l), ptr(NULL), origin(NULL) {
24   }
25 
26   // This contructor is typically used in unit tests, it extracts the label
27   // automatically from a given pointer.
LabelPtrPairLabelPtrPair28   explicit LabelPtrPair(const T* p) : label(p->label()), ptr(p), origin(NULL) {
29   }
30 
~LabelPtrPairLabelPtrPair31   ~LabelPtrPair() {}
32 
33   Label label;
34   const T* ptr;  // May be NULL.
35   const ParseNode* origin;  // May be NULL.
36 };
37 
38 typedef LabelPtrPair<Config> LabelConfigPair;
39 typedef LabelPtrPair<Target> LabelTargetPair;
40 
41 typedef std::vector<LabelConfigPair> LabelConfigVector;
42 typedef std::vector<LabelTargetPair> LabelTargetVector;
43 
44 // Comparison and search functions ---------------------------------------------
45 
46 // To do a brute-force search by label:
47 // std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label));
48 template<typename T>
49 struct LabelPtrLabelEquals : public std::unary_function<Label, bool> {
LabelPtrLabelEqualsLabelPtrLabelEquals50   explicit LabelPtrLabelEquals(const Label& l) : label(l) {}
51 
operatorLabelPtrLabelEquals52   bool operator()(const LabelPtrPair<T>& arg) const {
53     return arg.label == label;
54   }
55 
56   const Label& label;
57 };
58 
59 // To do a brute-force search by object pointer:
60 // std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(config));
61 template<typename T>
62 struct LabelPtrPtrEquals : public std::unary_function<T, bool> {
LabelPtrPtrEqualsLabelPtrPtrEquals63   explicit LabelPtrPtrEquals(const T* p) : ptr(p) {}
64 
operatorLabelPtrPtrEquals65   bool operator()(const LabelPtrPair<T>& arg) const {
66     return arg.ptr == ptr;
67   }
68 
69   const T* ptr;
70 };
71 
72 // To sort by label:
73 // std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>());
74 template<typename T>
75 struct LabelPtrLabelLess : public std::binary_function<LabelPtrPair<T>,
76                                                        LabelPtrPair<T>,
77                                                        bool> {
operatorLabelPtrLabelLess78   bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const {
79     return a.label < b.label;
80   }
81 };
82 
83 #endif  // TOOLS_GN_LABEL_PTR_H_
84