v8/include/cppgc/trace-trait.h
Michael Lippautz c4e7f6b697 cppgc: Provide Visitor and tracing infrastructure for Member
This CL adds the necessary traits to dispatch from Member through a
visitor implementation for GarabgeCollected and GarbageCollectedMixin.

Bug: chromium:1056170
Change-Id: I12680335044aaa842639fb5e8f9a3ac61587f51a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2138431
Reviewed-by: Hannes Payer <hpayer@chromium.org>
Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Reviewed-by: Anton Bikineev <bikineev@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67041}
2020-04-07 15:37:35 +00:00

69 lines
1.8 KiB
C++

// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_HEAP_CPPGC_TRACE_TRAIT_H_
#define V8_HEAP_CPPGC_TRACE_TRAIT_H_
#include <type_traits>
#include "include/cppgc/type-traits.h"
namespace cppgc {
class Visitor;
namespace internal {
template <typename T,
bool =
IsGarbageCollectedMixinTypeV<typename std::remove_const<T>::type>>
struct TraceTraitImpl;
} // namespace internal
using TraceCallback = void (*)(Visitor*, const void*);
// TraceDescriptor is used to describe how to trace an object.
struct TraceDescriptor {
// The adjusted base pointer of the object that should be traced.
const void* base_object_payload;
// A callback for tracing the object.
TraceCallback callback;
};
template <typename T>
struct TraceTrait {
static_assert(internal::IsTraceableV<T>, "T must have a Trace() method");
static TraceDescriptor GetTraceDescriptor(const void* self) {
return internal::TraceTraitImpl<T>::GetTraceDescriptor(
static_cast<const T*>(self));
}
static void Trace(Visitor* visitor, const void* self) {
// TODO(chromium:1056170): Remove const_cast when Trace() methods are const.
static_cast<T*>(const_cast<void*>(self))->Trace(visitor);
}
};
namespace internal {
template <typename T>
struct TraceTraitImpl<T, false> {
static TraceDescriptor GetTraceDescriptor(const void* self) {
return {self, TraceTrait<T>::Trace};
}
};
template <typename T>
struct TraceTraitImpl<T, true> {
static TraceDescriptor GetTraceDescriptor(const void* self) {
return static_cast<const T*>(self)->GetTraceDescriptor();
}
};
} // namespace internal
} // namespace cppgc
#endif // V8_HEAP_CPPGC_TRACE_TRAIT_H_