summaryrefslogtreecommitdiffstats
path: root/tools/gn/label_ptr.h
blob: c1cf9102980fd6cdd9dc43255f66bd4e85240b9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TOOLS_GN_LABEL_PTR_H_
#define TOOLS_GN_LABEL_PTR_H_

#include <functional>

#include "tools/gn/label.h"

class Config;
class ParseNode;
class Target;

// Structure that holds a labeled "thing". This is used for various places
// where we need to store lists of targets or configs. We sometimes populate
// the pointers on another thread from where we compute the labels, so this
// structure lets us save them separately. This also allows us to store the
// location of the thing that added this dependency.
template<typename T>
struct LabelPtrPair {
  typedef T DestType;

  LabelPtrPair() : label(), ptr(nullptr), origin(nullptr) {}

  explicit LabelPtrPair(const Label& l)
      : label(l), ptr(nullptr), origin(nullptr) {}

  // This contructor is typically used in unit tests, it extracts the label
  // automatically from a given pointer.
  explicit LabelPtrPair(const T* p)
      : label(p->label()), ptr(p), origin(nullptr) {}

  ~LabelPtrPair() {}

  Label label;
  const T* ptr;  // May be NULL.

  // The origin of this dependency. This will be null for internally generated
  // dependencies. This happens when a group is automatically expanded and that
  // group's members are added to the target that depends on that group.
  const ParseNode* origin;
};

typedef LabelPtrPair<Config> LabelConfigPair;
typedef LabelPtrPair<Target> LabelTargetPair;

typedef std::vector<LabelConfigPair> LabelConfigVector;
typedef std::vector<LabelTargetPair> LabelTargetVector;

// Comparison and search functions ---------------------------------------------

// To do a brute-force search by label:
// std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label));
template<typename T>
struct LabelPtrLabelEquals : public std::unary_function<Label, bool> {
  explicit LabelPtrLabelEquals(const Label& l) : label(l) {}

  bool operator()(const LabelPtrPair<T>& arg) const {
    return arg.label == label;
  }

  const Label& label;
};

// To do a brute-force search by object pointer:
// std::find_if(vect.begin(), vect.end(), LabelPtrPtrEquals<Config>(config));
template<typename T>
struct LabelPtrPtrEquals : public std::unary_function<T, bool> {
  explicit LabelPtrPtrEquals(const T* p) : ptr(p) {}

  bool operator()(const LabelPtrPair<T>& arg) const {
    return arg.ptr == ptr;
  }

  const T* ptr;
};

// To sort by label:
// std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>());
template<typename T>
struct LabelPtrLabelLess : public std::binary_function<LabelPtrPair<T>,
                                                       LabelPtrPair<T>,
                                                       bool> {
  bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const {
    return a.label < b.label;
  }
};

// Default comparison operators -----------------------------------------------
//
// The default hash and comparison operators operate on the label, which should
// always be valid, whereas the pointer is sometimes null.

template<typename T> inline bool operator==(const LabelPtrPair<T>& a,
                                            const LabelPtrPair<T>& b) {
  return a.label == b.label;
}

template<typename T> inline bool operator<(const LabelPtrPair<T>& a,
                                           const LabelPtrPair<T>& b) {
  return a.label < b.label;
}

namespace BASE_HASH_NAMESPACE {

template<typename T> struct hash< LabelPtrPair<T> > {
  std::size_t operator()(const LabelPtrPair<T>& v) const {
    BASE_HASH_NAMESPACE::hash<Label> h;
    return h(v.label);
  }
};

}  // namespace BASE_HASH_NAMESPACE

#endif  // TOOLS_GN_LABEL_PTR_H_