Retry fdbff0e3b60139805609f643eb40:
[chromium-blink-merge.git] / tools / gn / label_ptr.h
blobc1cf9102980fd6cdd9dc43255f66bd4e85240b9e
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.
5 #ifndef TOOLS_GN_LABEL_PTR_H_
6 #define TOOLS_GN_LABEL_PTR_H_
8 #include <functional>
10 #include "tools/gn/label.h"
12 class Config;
13 class ParseNode;
14 class Target;
16 // Structure that holds a labeled "thing". This is used for various places
17 // where we need to store lists of targets or configs. We sometimes populate
18 // the pointers on another thread from where we compute the labels, so this
19 // structure lets us save them separately. This also allows us to store the
20 // location of the thing that added this dependency.
21 template<typename T>
22 struct LabelPtrPair {
23 typedef T DestType;
25 LabelPtrPair() : label(), ptr(nullptr), origin(nullptr) {}
27 explicit LabelPtrPair(const Label& l)
28 : label(l), ptr(nullptr), origin(nullptr) {}
30 // This contructor is typically used in unit tests, it extracts the label
31 // automatically from a given pointer.
32 explicit LabelPtrPair(const T* p)
33 : label(p->label()), ptr(p), origin(nullptr) {}
35 ~LabelPtrPair() {}
37 Label label;
38 const T* ptr; // May be NULL.
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;
46 typedef LabelPtrPair<Config> LabelConfigPair;
47 typedef LabelPtrPair<Target> LabelTargetPair;
49 typedef std::vector<LabelConfigPair> LabelConfigVector;
50 typedef std::vector<LabelTargetPair> LabelTargetVector;
52 // Comparison and search functions ---------------------------------------------
54 // To do a brute-force search by label:
55 // std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label));
56 template<typename T>
57 struct LabelPtrLabelEquals : public std::unary_function<Label, bool> {
58 explicit LabelPtrLabelEquals(const Label& l) : label(l) {}
60 bool operator()(const LabelPtrPair<T>& arg) const {
61 return arg.label == label;
64 const Label& label;
67 // To do a brute-force search by object pointer:
68 // std::find_if(vect.begin(), vect.end(), LabelPtrPtrEquals<Config>(config));
69 template<typename T>
70 struct LabelPtrPtrEquals : public std::unary_function<T, bool> {
71 explicit LabelPtrPtrEquals(const T* p) : ptr(p) {}
73 bool operator()(const LabelPtrPair<T>& arg) const {
74 return arg.ptr == ptr;
77 const T* ptr;
80 // To sort by label:
81 // std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>());
82 template<typename T>
83 struct LabelPtrLabelLess : public std::binary_function<LabelPtrPair<T>,
84 LabelPtrPair<T>,
85 bool> {
86 bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const {
87 return a.label < b.label;
91 // Default comparison operators -----------------------------------------------
93 // The default hash and comparison operators operate on the label, which should
94 // always be valid, whereas the pointer is sometimes null.
96 template<typename T> inline bool operator==(const LabelPtrPair<T>& a,
97 const LabelPtrPair<T>& b) {
98 return a.label == b.label;
101 template<typename T> inline bool operator<(const LabelPtrPair<T>& a,
102 const LabelPtrPair<T>& b) {
103 return a.label < b.label;
106 namespace BASE_HASH_NAMESPACE {
108 template<typename T> struct hash< LabelPtrPair<T> > {
109 std::size_t operator()(const LabelPtrPair<T>& v) const {
110 BASE_HASH_NAMESPACE::hash<Label> h;
111 return h(v.label);
115 } // namespace BASE_HASH_NAMESPACE
117 #endif // TOOLS_GN_LABEL_PTR_H_