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