Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / gn / label_ptr.h
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 #include "tools/gn/label.h"
11
12 class Config;
13 class ParseNode;
14 class Target;
15
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;
24
25   LabelPtrPair() : label(), ptr(NULL), origin(NULL) {}
26
27   explicit LabelPtrPair(const Label& l) : label(l), ptr(NULL), origin(NULL) {
28   }
29
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) {
33   }
34
35   ~LabelPtrPair() {}
36
37   Label label;
38   const T* ptr;  // May be NULL.
39   const ParseNode* origin;  // May be NULL.
40 };
41
42 typedef LabelPtrPair<Config> LabelConfigPair;
43 typedef LabelPtrPair<Target> LabelTargetPair;
44
45 typedef std::vector<LabelConfigPair> LabelConfigVector;
46 typedef std::vector<LabelTargetPair> LabelTargetVector;
47
48 // Comparison and search functions ---------------------------------------------
49
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) {}
55
56   bool operator()(const LabelPtrPair<T>& arg) const {
57     return arg.label == label;
58   }
59
60   const Label& label;
61 };
62
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) {}
68
69   bool operator()(const LabelPtrPair<T>& arg) const {
70     return arg.ptr == ptr;
71   }
72
73   const T* ptr;
74 };
75
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;
84   }
85 };
86
87 // Default comparison operators -----------------------------------------------
88 //
89 // The default hash and comparison operators operate on the label, which should
90 // always be valid, whereas the pointer is sometimes null.
91
92 template<typename T> inline bool operator==(const LabelPtrPair<T>& a,
93                                             const LabelPtrPair<T>& b) {
94   return a.label == b.label;
95 }
96
97 template<typename T> inline bool operator<(const LabelPtrPair<T>& a,
98                                            const LabelPtrPair<T>& b) {
99   return a.label < b.label;
100 }
101
102 namespace BASE_HASH_NAMESPACE {
103
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);
109   }
110 };
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);
115 }
116 #endif  // COMPILER...
117
118 }  // namespace BASE_HASH_NAMESPACE
119
120 #endif  // TOOLS_GN_LABEL_PTR_H_