5 #ifndef INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_
6 #define INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_
10 #include "cppgc/internal/persistent-node.h"
11 #include "cppgc/internal/pointer-policies.h"
12 #include "cppgc/persistent.h"
13 #include "cppgc/visitor.h"
41 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
42 typename CheckingPolicy>
44 public LocationPolicy,
45 private WeaknessPolicy,
46 private CheckingPolicy {
48 using typename WeaknessPolicy::IsStrongPersistent;
49 using PointeeType = T;
55 : LocationPolicy(loc) {}
59 : LocationPolicy(loc) {}
68 if (!IsValid(raw))
return;
69 PersistentRegionLock guard;
70 CrossThreadPersistentRegion& region =
this->GetPersistentRegion(raw);
72 this->CheckPointer(raw);
77 UnsafeCtorTag() =
default;
78 template <
typename U,
typename OtherWeaknessPolicy,
79 typename OtherLocationPolicy,
typename OtherCheckingPolicy>
87 if (!IsValid(raw))
return;
88 CrossThreadPersistentRegion& region =
this->GetPersistentRegion(raw);
90 this->CheckPointer(raw);
97 template <
typename U,
typename MemberBarrierPolicy,
98 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
99 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
101 internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
102 MemberCheckingPolicy>
116 template <
typename U,
typename OtherWeaknessPolicy,
117 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
118 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
122 OtherCheckingPolicy>& other,
132 *
this = std::move(other);
137 PersistentRegionLock guard;
138 AssignUnsafe(other.Get());
142 template <
typename U,
typename OtherWeaknessPolicy,
143 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
144 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
148 OtherCheckingPolicy>& other) {
149 PersistentRegionLock guard;
150 AssignUnsafe(other.Get());
155 if (
this == &other)
return *
this;
157 PersistentRegionLock guard;
159 LocationPolicy::operator=(std::move(other));
162 other.SetValue(
nullptr);
163 other.SetNode(
nullptr);
164 this->CheckPointer(Get());
174 template <
typename U,
typename MemberBarrierPolicy,
175 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
176 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
178 internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
179 MemberCheckingPolicy>
181 return operator=(member.Get());
205 return static_cast<T*>(
const_cast<
void*>(
GetValue()));
215 if (IsValid(old_value)) {
216 PersistentRegionLock guard;
220 if (IsValid(old_value)) {
221 CrossThreadPersistentRegion& region =
222 this->GetPersistentRegion(old_value);
271 template <
typename U,
typename OtherWeaknessPolicy = WeaknessPolicy,
272 typename OtherLocationPolicy = LocationPolicy,
273 typename OtherCheckingPolicy = CheckingPolicy>
277 using OtherBasicCrossThreadPersistent =
279 OtherCheckingPolicy>;
280 PersistentRegionLock guard;
281 return OtherBasicCrossThreadPersistent(
282 typename OtherBasicCrossThreadPersistent::UnsafeCtorTag(),
283 static_cast<U*>(Get()));
286 template <
typename U = T,
288 U, WeaknessPolicy>::IsStrongPersistent::value>::type>
296 static bool IsValid(
const void* ptr) {
300 static void Trace(
Visitor* v,
const void* ptr) {
302 v->TraceRoot(*handle, handle->Location());
305 void Assign(T* ptr) {
307 if (IsValid(old_value)) {
308 PersistentRegionLock guard;
312 if (IsValid(old_value)) {
313 CrossThreadPersistentRegion& region =
314 this->GetPersistentRegion(old_value);
315 if (IsValid(ptr) && (®ion == &
this->GetPersistentRegion(ptr))) {
317 this->CheckPointer(ptr);
327 if (!IsValid(ptr))
return;
328 PersistentRegionLock guard;
329 SetNode(this->GetPersistentRegion(ptr).AllocateNode(
this, &Trace)
);
330 this->CheckPointer(ptr);
333 void AssignUnsafe(T* ptr) {
336 if (IsValid(old_value)) {
337 CrossThreadPersistentRegion& region =
338 this->GetPersistentRegion(old_value);
339 if (IsValid(ptr) && (®ion == &
this->GetPersistentRegion(ptr))) {
341 this->CheckPointer(ptr);
348 if (!IsValid(ptr))
return;
349 SetNode(this->GetPersistentRegion(ptr).AllocateNode(
this, &Trace)
);
350 this->CheckPointer(ptr);
353 void ClearFromGC()
const {
354 if (IsValid(GetValueFromGC())) {
355 WeaknessPolicy::GetPersistentRegion(GetValueFromGC())
363 T* GetFromGC()
const {
364 return static_cast<T*>(
const_cast<
void*>(GetValueFromGC()));
370 template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
373 LocationPolicy, CheckingPolicy>>
391 template <
typename T>
406 template <
typename T>