File: node_inline_data.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (267 lines) | stat: -rw-r--r-- 8,364 bytes parent folder | download | duplicates (6)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PERFORMANCE_MANAGER_GRAPH_NODE_INLINE_DATA_H_
#define COMPONENTS_PERFORMANCE_MANAGER_GRAPH_NODE_INLINE_DATA_H_

#include <tuple>
#include <utility>

#include "base/types/pass_key.h"
#include "components/performance_manager/graph/node_inline_data_impl.h"

namespace performance_manager {

// Helper classes for defining a user data class that is associated with nodes
// in the graph. At most one instance of each type of data may exist per node
// in the graph.
//
// An instance of node inline data has to be specifically permitted to be
// associated with individual node types, with this being enforced at compile
// time via the type system. A storage type must be specified which may be one
// of the following:
//
// - NodeInlineData<T>: Directly stored as a member in the node.
//
// - SparseNodeInlineData<T>: Stored in a unique_ptr<> as a member in the node.
//
// If the data is needed for most instances of a node type and for effectively
// the entire lifetime of the node, use NodeInlineData<T>.
//
// If the data is needed for most instances of a node type, but only for a
// relatively small portion of the node's lifetime, use
// SparseNodeInlineData<T>. The type will be stored as a member on the node, and
// as such it will be slightly faster to access but will always occupy its
// memory footprint, whether it is created or not.
//
// If the data is *not* needed for most instances of a node type, use
// NodeAttachedData<T> (See node_attached_data.h). The type will be stored in a
// unique_ptr, and will thus less memory is allocated when the type is not
// created.
//
// The functionality of this file is intended to be used as follows:
//
// First, add your data type to the node type (or types) that you want to
// associate your data with.
//
// For example:
//
// -- process_node_impl.h --
//
// class ProcessNodeImpl : (...)
//      (...)
//      public SupportsNodeInlineData<DataType1,
//                                    DataType2,
//                           ---->    YourDataType>    <----
//
// -- process_node_impl.h --
//
// Second, derive your data type from one of NodeInlineData<T> or
// SparseNodeInlineData<T>:
//
// -- your_data_type.h --
//
// class YourDataType : public SparseNodeInlineData<YourDataType> {
//  (...)
// };
//
// -- your_data_type.h --
//
// Once defined, your type instance can be created like so:
//
//   YourDataType& your_data_type = YourDataType::Create(node_impl, ..args..);
//
// If the instance already exists, you can access it using Get():
//
//   YourDataType& your_data_type = YourDataType::Get(node_impl);
//
// Optionally, you can preemptively destroy the instance once it is no longer
// needed:
//
//   YourDataType::Destroy(node_impl);

template <class T>
class NodeInlineData {
 public:
  using PassKey = base::PassKey<NodeInlineData>;

  // Returns true if the instance exists for this type.
  template <class NodeImplClass>
  static bool Exists(NodeImplClass* node);

  // Retrieves the instance associated with `node`. Asserts that it exists.
  // `Create()` must have been called before calling this.
  template <class NodeImplClass>
  static T& Get(NodeImplClass* node);

  // Retrieves the instance associated with `node`. Asserts that it exists.
  // `Create()` must have been called before calling this.
  template <class NodeImplClass>
  static const T& Get(const NodeImplClass* node);

  // Creates the instance. `args` is passed as arguments to the constructor of
  // `T`. If `T`'s first argument is the node type, `node` is also passed as an
  // argument to the constructor.
  template <class NodeImplClass, class... Args>
    requires std::is_constructible_v<T, Args...>
  static T& Create(NodeImplClass* node, Args&&... args);
  template <class NodeImplClass, class... Args>
    requires std::is_constructible_v<T, NodeImplClass*, Args...>
  static T& Create(NodeImplClass* node, Args&&... args);

  // Destroys the instance associated with `node`. Asserts that it exists. Note
  // that calling this is optional. The instance will get cleaned up
  // automatically when the node is destroyed.
  template <class NodeImplClass>
  static void Destroy(NodeImplClass* node);
};

template <class T>
class SparseNodeInlineData : public NodeInlineData<T> {};

// Derived by node implementation classes to provide access to inline data.
// Private through the use of base::PassKey so that only the relevant
// NodeInlineData<T> can use it.
template <class... Ts>
class SupportsNodeInlineData {
 public:
  template <class T>
  bool NodeDataExists(base::PassKey<NodeInlineData<T>>);

  template <class T>
  T& GetNodeData(base::PassKey<NodeInlineData<T>>);

  template <class T>
  const T& GetNodeData(base::PassKey<NodeInlineData<T>>) const;

  template <class T, class... Args>
  T& CreateNodeData(base::PassKey<NodeInlineData<T>>, Args&&... args);

  template <class T>
  void DestroyNodeData(base::PassKey<NodeInlineData<T>>);

 protected:
  virtual ~SupportsNodeInlineData() = default;

  void DestroyNodeInlineDataStorage();

 private:
  template <class T>
  internal::Storage<T>& GetStorage();

  template <class T>
  const internal::Storage<T>& GetStorage() const;

  using UnderlyingStorage = std::tuple<internal::Storage<Ts>...>;
  UnderlyingStorage storage_;
};

///////////////////////////////////////////////////////////
// Everything below this point is implementation detail! //
///////////////////////////////////////////////////////////

// Implementation of NodeInlineData<T>:

// static
template <class T>
template <class NodeImplClass>
bool NodeInlineData<T>::Exists(NodeImplClass* node) {
  return node->NodeDataExists(PassKey());
}

// static
template <class T>
template <class NodeImplClass>
T& NodeInlineData<T>::Get(NodeImplClass* node) {
  return node->GetNodeData(PassKey());
}

// static
template <class T>
template <class NodeImplClass>
const T& NodeInlineData<T>::Get(const NodeImplClass* node) {
  return node->GetNodeData(PassKey());
}

// static
template <class T>
template <class NodeImplClass, class... Args>
  requires std::is_constructible_v<T, Args...>
T& NodeInlineData<T>::Create(NodeImplClass* node, Args&&... args) {
  return node->CreateNodeData(PassKey(), std::forward<Args>(args)...);
}

// static
template <class T>
template <class NodeImplClass, class... Args>
  requires std::is_constructible_v<T, NodeImplClass*, Args...>
T& NodeInlineData<T>::Create(NodeImplClass* node, Args&&... args) {
  return node->CreateNodeData(PassKey(), node, std::forward<Args>(args)...);
}

// static
template <class T>
template <class NodeImplClass>
void NodeInlineData<T>::Destroy(NodeImplClass* node) {
  node->DestroyNodeData(PassKey());
}

// Implementation of SupportsNodeInlineData<Ts...>:

template <class... Ts>
template <class T>
bool SupportsNodeInlineData<Ts...>::NodeDataExists(
    base::PassKey<NodeInlineData<T>>) {
  return GetStorage<T>().Exists();
}

template <class... Ts>
template <class T>
T& SupportsNodeInlineData<Ts...>::GetNodeData(
    base::PassKey<NodeInlineData<T>>) {
  return GetStorage<T>().Get();
}

template <class... Ts>
template <class T>
const T& SupportsNodeInlineData<Ts...>::GetNodeData(
    base::PassKey<NodeInlineData<T>>) const {
  return GetStorage<T>().Get();
}

template <class... Ts>
template <class T, class... Args>
T& SupportsNodeInlineData<Ts...>::CreateNodeData(
    base::PassKey<NodeInlineData<T>>,
    Args&&... args) {
  return GetStorage<T>().Create(std::forward<Args>(args)...);
}

template <class... Ts>
template <class T>
void SupportsNodeInlineData<Ts...>::DestroyNodeData(
    base::PassKey<NodeInlineData<T>>) {
  GetStorage<T>().Destroy();
}

template <class... Ts>
void SupportsNodeInlineData<Ts...>::DestroyNodeInlineDataStorage() {
  storage_ = UnderlyingStorage();
}

template <class... Ts>
template <class T>
internal::Storage<T>& SupportsNodeInlineData<Ts...>::GetStorage() {
  return std::get<internal::Storage<T>>(storage_);
}

template <class... Ts>
template <class T>
const internal::Storage<T>& SupportsNodeInlineData<Ts...>::GetStorage() const {
  return std::get<internal::Storage<T>>(storage_);
}

}  // namespace performance_manager

#endif  // COMPONENTS_PERFORMANCE_MANAGER_GRAPH_NODE_INLINE_DATA_H_