• 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 <stddef.h>
9 
10 #include <functional>
11 
12 #include "gn/label.h"
13 
14 class Config;
15 class ParseNode;
16 class Target;
17 
18 // Structure that holds a labeled "thing". This is used for various places
19 // where we need to store lists of targets or configs. We sometimes populate
20 // the pointers on another thread from where we compute the labels, so this
21 // structure lets us save them separately. This also allows us to store the
22 // location of the thing that added this dependency.
23 template <typename T>
24 struct LabelPtrPair {
25   using DestType = T;
26 
27   LabelPtrPair() = default;
28 
LabelPtrPairLabelPtrPair29   explicit LabelPtrPair(const Label& l) : label(l) {}
30 
31   // This constructor is typically used in unit tests, it extracts the label
32   // automatically from a given pointer.
LabelPtrPairLabelPtrPair33   explicit LabelPtrPair(const T* p) : label(p->label()), ptr(p) {}
34 
35   ~LabelPtrPair() = default;
36 
37   Label label;
38   bool is_external_deps;
39   const T* ptr = nullptr;
40 
41   // The origin of this dependency. This will be null for internally generated
42   // dependencies. This happens when a group is automatically expanded and that
43   // group's members are added to the target that depends on that group.
44   const ParseNode* origin = nullptr;
45 };
46 
47 using LabelConfigPair = LabelPtrPair<Config>;
48 using LabelTargetPair = LabelPtrPair<Target>;
49 
50 using LabelConfigVector = std::vector<LabelConfigPair>;
51 using LabelTargetVector = std::vector<LabelTargetPair>;
52 
53 // Default comparison operators -----------------------------------------------
54 //
55 // The default hash and comparison operators operate on the label, which should
56 // always be valid, whereas the pointer is sometimes null.
57 
58 template <typename T>
59 inline bool operator==(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) {
60   return a.label == b.label;
61 }
62 
63 template <typename T>
64 inline bool operator<(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) {
65   return a.label < b.label;
66 }
67 
68 namespace std {
69 
70 template <typename T>
71 struct hash<LabelPtrPair<T>> {
72   std::size_t operator()(const LabelPtrPair<T>& v) const {
73     hash<Label> h;
74     return h(v.label);
75   }
76 };
77 
78 }  // namespace std
79 
80 #endif  // TOOLS_GN_LABEL_PTR_H_
81