• 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   const T* ptr = nullptr;
39 
40   // The origin of this dependency. This will be null for internally generated
41   // dependencies. This happens when a group is automatically expanded and that
42   // group's members are added to the target that depends on that group.
43   const ParseNode* origin = nullptr;
44 };
45 
46 using LabelConfigPair = LabelPtrPair<Config>;
47 using LabelTargetPair = LabelPtrPair<Target>;
48 
49 using LabelConfigVector = std::vector<LabelConfigPair>;
50 using LabelTargetVector = std::vector<LabelTargetPair>;
51 
52 // Default comparison operators -----------------------------------------------
53 //
54 // The default hash and comparison operators operate on the label, which should
55 // always be valid, whereas the pointer is sometimes null.
56 
57 template <typename T>
58 inline bool operator==(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) {
59   return a.label == b.label;
60 }
61 
62 template <typename T>
63 inline bool operator<(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) {
64   return a.label < b.label;
65 }
66 
67 namespace std {
68 
69 template <typename T>
70 struct hash<LabelPtrPair<T>> {
71   std::size_t operator()(const LabelPtrPair<T>& v) const {
72     hash<Label> h;
73     return h(v.label);
74   }
75 };
76 
77 }  // namespace std
78 
79 #endif  // TOOLS_GN_LABEL_PTR_H_
80