v8  8.4.371 (node 14.15.5)
V8 is Google's open source JavaScript engine
trace-trait.h
Go to the documentation of this file.
1 // Copyright 2020 the V8 project 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 INCLUDE_CPPGC_TRACE_TRAIT_H_
6 #define INCLUDE_CPPGC_TRACE_TRAIT_H_
7 
8 #include <type_traits>
9 #include "cppgc/type-traits.h"
10 
11 namespace cppgc {
12 
13 class Visitor;
14 
15 namespace internal {
16 
17 template <typename T,
18  bool =
19  IsGarbageCollectedMixinTypeV<typename std::remove_const<T>::type>>
20 struct TraceTraitImpl;
21 
22 } // namespace internal
23 
24 using TraceCallback = void (*)(Visitor*, const void*);
25 
26 // TraceDescriptor is used to describe how to trace an object.
28  // The adjusted base pointer of the object that should be traced.
29  const void* base_object_payload;
30  // A callback for tracing the object.
31  TraceCallback callback;
32 };
33 
34 template <typename T>
35 struct TraceTrait {
36  static_assert(internal::IsTraceableV<T>, "T must have a Trace() method");
37 
38  static TraceDescriptor GetTraceDescriptor(const void* self) {
39  return internal::TraceTraitImpl<T>::GetTraceDescriptor(
40  static_cast<const T*>(self));
41  }
42 
43  static void Trace(Visitor* visitor, const void* self) {
44  static_cast<const T*>(self)->Trace(visitor);
45  }
46 };
47 
48 namespace internal {
49 
50 template <typename T>
51 struct TraceTraitImpl<T, false> {
52  static TraceDescriptor GetTraceDescriptor(const void* self) {
53  return {self, TraceTrait<T>::Trace};
54  }
55 };
56 
57 template <typename T>
58 struct TraceTraitImpl<T, true> {
59  static TraceDescriptor GetTraceDescriptor(const void* self) {
60  return static_cast<const T*>(self)->GetTraceDescriptor();
61  }
62 };
63 
64 } // namespace internal
65 } // namespace cppgc
66 
67 #endif // INCLUDE_CPPGC_TRACE_TRAIT_H_