node_attached_data.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 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. #include "components/performance_manager/graph/node_attached_data.h"
  5. #include <utility>
  6. #include "base/check_op.h"
  7. #include "base/containers/contains.h"
  8. #include "components/performance_manager/graph/graph_impl.h"
  9. #include "components/performance_manager/public/graph/node.h"
  10. namespace performance_manager {
  11. NodeAttachedData::NodeAttachedData() = default;
  12. NodeAttachedData::~NodeAttachedData() = default;
  13. // static
  14. void NodeAttachedDataMapHelper::AttachInMap(
  15. const Node* node,
  16. std::unique_ptr<NodeAttachedData> data) {
  17. GraphImpl* graph = GraphImpl::FromGraph(node->GetGraph());
  18. DCHECK_CALLED_ON_VALID_SEQUENCE(graph->sequence_checker_);
  19. const NodeBase* node_base = NodeBase::FromNode(node);
  20. DCHECK(graph->NodeInGraph(node_base));
  21. GraphImpl::NodeAttachedDataKey data_key =
  22. std::make_pair(node, data->GetKey());
  23. auto& map = graph->node_attached_data_map_;
  24. DCHECK(!base::Contains(map, data_key));
  25. map[data_key] = std::move(data);
  26. }
  27. // static
  28. NodeAttachedData* NodeAttachedDataMapHelper::GetFromMap(const Node* node,
  29. const void* key) {
  30. GraphImpl* graph = GraphImpl::FromGraph(node->GetGraph());
  31. DCHECK_CALLED_ON_VALID_SEQUENCE(graph->sequence_checker_);
  32. const NodeBase* node_base = NodeBase::FromNode(node);
  33. DCHECK(graph->NodeInGraph(node_base));
  34. GraphImpl::NodeAttachedDataKey data_key = std::make_pair(node, key);
  35. auto& map = graph->node_attached_data_map_;
  36. auto it = map.find(data_key);
  37. if (it == map.end())
  38. return nullptr;
  39. DCHECK_EQ(key, it->second->GetKey());
  40. return it->second.get();
  41. }
  42. // static
  43. std::unique_ptr<NodeAttachedData> NodeAttachedDataMapHelper::DetachFromMap(
  44. const Node* node,
  45. const void* key) {
  46. GraphImpl* graph = GraphImpl::FromGraph(node->GetGraph());
  47. DCHECK_CALLED_ON_VALID_SEQUENCE(graph->sequence_checker_);
  48. const NodeBase* node_base = NodeBase::FromNode(node);
  49. DCHECK(graph->NodeInGraph(node_base));
  50. GraphImpl::NodeAttachedDataKey data_key = std::make_pair(node, key);
  51. auto& map = graph->node_attached_data_map_;
  52. auto it = map.find(data_key);
  53. std::unique_ptr<NodeAttachedData> data;
  54. if (it != map.end()) {
  55. data = std::move(it->second);
  56. map.erase(it);
  57. }
  58. return data;
  59. }
  60. } // namespace performance_manager