Fix DisownOpener and related tests.
[chromium-blink-merge.git] / tools / gn / label_ptr.h
blob771b14ba3fce1a767a6d8405433d32afb8c7a405
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(NULL), origin(NULL) {}
27 explicit LabelPtrPair(const Label& l) : label(l), ptr(NULL), origin(NULL) {
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) : label(p->label()), ptr(p), origin(NULL) {
35 ~LabelPtrPair() {}
37 Label label;
38 const T* ptr; // May be NULL.
39 const ParseNode* origin; // May be NULL.
42 typedef LabelPtrPair<Config> LabelConfigPair;
43 typedef LabelPtrPair<Target> LabelTargetPair;
45 typedef std::vector<LabelConfigPair> LabelConfigVector;
46 typedef std::vector<LabelTargetPair> LabelTargetVector;
48 // Comparison and search functions ---------------------------------------------
50 // To do a brute-force search by label:
51 // std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label));
52 template<typename T>
53 struct LabelPtrLabelEquals : public std::unary_function<Label, bool> {
54 explicit LabelPtrLabelEquals(const Label& l) : label(l) {}
56 bool operator()(const LabelPtrPair<T>& arg) const {
57 return arg.label == label;
60 const Label& label;
63 // To do a brute-force search by object pointer:
64 // std::find_if(vect.begin(), vect.end(), LabelPtrPtrEquals<Config>(config));
65 template<typename T>
66 struct LabelPtrPtrEquals : public std::unary_function<T, bool> {
67 explicit LabelPtrPtrEquals(const T* p) : ptr(p) {}
69 bool operator()(const LabelPtrPair<T>& arg) const {
70 return arg.ptr == ptr;
73 const T* ptr;
76 // To sort by label:
77 // std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>());
78 template<typename T>
79 struct LabelPtrLabelLess : public std::binary_function<LabelPtrPair<T>,
80 LabelPtrPair<T>,
81 bool> {
82 bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const {
83 return a.label < b.label;
87 // Default comparison operators -----------------------------------------------
89 // The default hash and comparison operators operate on the label, which should
90 // always be valid, whereas the pointer is sometimes null.
92 template<typename T> inline bool operator==(const LabelPtrPair<T>& a,
93 const LabelPtrPair<T>& b) {
94 return a.label == b.label;
97 template<typename T> inline bool operator<(const LabelPtrPair<T>& a,
98 const LabelPtrPair<T>& b) {
99 return a.label < b.label;
102 namespace BASE_HASH_NAMESPACE {
104 #if defined(COMPILER_GCC)
105 template<typename T> struct hash< LabelPtrPair<T> > {
106 std::size_t operator()(const LabelPtrPair<T>& v) const {
107 BASE_HASH_NAMESPACE::hash<Label> h;
108 return h(v.label);
111 #elif defined(COMPILER_MSVC)
112 template<typename T>
113 inline size_t hash_value(const LabelPtrPair<T>& v) {
114 return BASE_HASH_NAMESPACE::hash_value(v.label);
116 #endif // COMPILER...
118 } // namespace BASE_HASH_NAMESPACE
120 #endif // TOOLS_GN_LABEL_PTR_H_