16d528ed9Sopenharmony_ci// Copyright (c) 2013 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci#ifndef TOOLS_GN_LABEL_PTR_H_
66d528ed9Sopenharmony_ci#define TOOLS_GN_LABEL_PTR_H_
76d528ed9Sopenharmony_ci
86d528ed9Sopenharmony_ci#include <stddef.h>
96d528ed9Sopenharmony_ci
106d528ed9Sopenharmony_ci#include <functional>
116d528ed9Sopenharmony_ci
126d528ed9Sopenharmony_ci#include "gn/label.h"
136d528ed9Sopenharmony_ci
146d528ed9Sopenharmony_ciclass Config;
156d528ed9Sopenharmony_ciclass ParseNode;
166d528ed9Sopenharmony_ciclass Target;
176d528ed9Sopenharmony_ci
186d528ed9Sopenharmony_ci// Structure that holds a labeled "thing". This is used for various places
196d528ed9Sopenharmony_ci// where we need to store lists of targets or configs. We sometimes populate
206d528ed9Sopenharmony_ci// the pointers on another thread from where we compute the labels, so this
216d528ed9Sopenharmony_ci// structure lets us save them separately. This also allows us to store the
226d528ed9Sopenharmony_ci// location of the thing that added this dependency.
236d528ed9Sopenharmony_citemplate <typename T>
246d528ed9Sopenharmony_cistruct LabelPtrPair {
256d528ed9Sopenharmony_ci  using DestType = T;
266d528ed9Sopenharmony_ci
276d528ed9Sopenharmony_ci  LabelPtrPair() = default;
286d528ed9Sopenharmony_ci
296d528ed9Sopenharmony_ci  explicit LabelPtrPair(const Label& l) : label(l) {}
306d528ed9Sopenharmony_ci
316d528ed9Sopenharmony_ci  // This constructor is typically used in unit tests, it extracts the label
326d528ed9Sopenharmony_ci  // automatically from a given pointer.
336d528ed9Sopenharmony_ci  explicit LabelPtrPair(const T* p) : label(p->label()), ptr(p) {}
346d528ed9Sopenharmony_ci
356d528ed9Sopenharmony_ci  ~LabelPtrPair() = default;
366d528ed9Sopenharmony_ci
376d528ed9Sopenharmony_ci  Label label;
386d528ed9Sopenharmony_ci  bool is_external_deps;
396d528ed9Sopenharmony_ci  const T* ptr = nullptr;
406d528ed9Sopenharmony_ci
416d528ed9Sopenharmony_ci  // The origin of this dependency. This will be null for internally generated
426d528ed9Sopenharmony_ci  // dependencies. This happens when a group is automatically expanded and that
436d528ed9Sopenharmony_ci  // group's members are added to the target that depends on that group.
446d528ed9Sopenharmony_ci  const ParseNode* origin = nullptr;
456d528ed9Sopenharmony_ci};
466d528ed9Sopenharmony_ci
476d528ed9Sopenharmony_ciusing LabelConfigPair = LabelPtrPair<Config>;
486d528ed9Sopenharmony_ciusing LabelTargetPair = LabelPtrPair<Target>;
496d528ed9Sopenharmony_ci
506d528ed9Sopenharmony_ciusing LabelConfigVector = std::vector<LabelConfigPair>;
516d528ed9Sopenharmony_ciusing LabelTargetVector = std::vector<LabelTargetPair>;
526d528ed9Sopenharmony_ci
536d528ed9Sopenharmony_ci// Default comparison operators -----------------------------------------------
546d528ed9Sopenharmony_ci//
556d528ed9Sopenharmony_ci// The default hash and comparison operators operate on the label, which should
566d528ed9Sopenharmony_ci// always be valid, whereas the pointer is sometimes null.
576d528ed9Sopenharmony_ci
586d528ed9Sopenharmony_citemplate <typename T>
596d528ed9Sopenharmony_ciinline bool operator==(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) {
606d528ed9Sopenharmony_ci  return a.label == b.label;
616d528ed9Sopenharmony_ci}
626d528ed9Sopenharmony_ci
636d528ed9Sopenharmony_citemplate <typename T>
646d528ed9Sopenharmony_ciinline bool operator<(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) {
656d528ed9Sopenharmony_ci  return a.label < b.label;
666d528ed9Sopenharmony_ci}
676d528ed9Sopenharmony_ci
686d528ed9Sopenharmony_cinamespace std {
696d528ed9Sopenharmony_ci
706d528ed9Sopenharmony_citemplate <typename T>
716d528ed9Sopenharmony_cistruct hash<LabelPtrPair<T>> {
726d528ed9Sopenharmony_ci  std::size_t operator()(const LabelPtrPair<T>& v) const {
736d528ed9Sopenharmony_ci    hash<Label> h;
746d528ed9Sopenharmony_ci    return h(v.label);
756d528ed9Sopenharmony_ci  }
766d528ed9Sopenharmony_ci};
776d528ed9Sopenharmony_ci
786d528ed9Sopenharmony_ci}  // namespace std
796d528ed9Sopenharmony_ci
806d528ed9Sopenharmony_ci#endif  // TOOLS_GN_LABEL_PTR_H_
81