5 #ifndef INCLUDE_CPPGC_PERSISTENT_H_
6 #define INCLUDE_CPPGC_PERSISTENT_H_
10 #include "cppgc/internal/persistent-node.h"
11 #include "cppgc/internal/pointer-policies.h"
12 #include "cppgc/sentinel-pointer.h"
13 #include "cppgc/source-location.h"
14 #include "cppgc/type-traits.h"
15 #include "cppgc/visitor.h"
45 mutable const void*
raw_ =
nullptr;
46 mutable PersistentNode*
node_ =
nullptr;
52 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
53 typename CheckingPolicy>
55 public LocationPolicy,
56 private WeaknessPolicy,
57 private CheckingPolicy {
59 using typename WeaknessPolicy::IsStrongPersistent;
60 using PointeeType = T;
65 : LocationPolicy(loc) {}
69 : LocationPolicy(loc) {}
79 if (!IsValid())
return;
82 this->CheckPointer(Get());
95 template <
typename U,
typename OtherWeaknessPolicy,
96 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
97 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
100 OtherCheckingPolicy>& other,
109 :
PersistentBase(std::move(other)), LocationPolicy(std::move(other)) {
110 if (!IsValid())
return;
112 other.SetValue(
nullptr);
113 other.SetNode(
nullptr);
114 this->CheckPointer(Get());
118 template <
typename U,
typename MemberBarrierPolicy,
119 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
120 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
122 MemberWeaknessTag, MemberCheckingPolicy>
131 return operator=(other.Get());
134 template <
typename U,
typename OtherWeaknessPolicy,
135 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
136 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
139 OtherCheckingPolicy>& other) {
140 return operator=(other.Get());
145 if (
this == &other)
return *
this;
148 LocationPolicy::operator=(std::move(other));
149 if (!IsValid())
return *
this;
151 other.SetValue(
nullptr);
152 other.SetNode(
nullptr);
153 this->CheckPointer(Get());
158 template <
typename U,
typename MemberBarrierPolicy,
159 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
160 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
162 internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
163 MemberCheckingPolicy>
165 return operator=(member.Get());
195 return static_cast<T*>(
const_cast<
void*>(
GetValue()));
214 template <
typename U,
typename OtherWeaknessPolicy = WeaknessPolicy,
215 typename OtherLocationPolicy = LocationPolicy,
216 typename OtherCheckingPolicy = CheckingPolicy>
221 OtherCheckingPolicy>(
static_cast<U*>(Get()));
225 static void Trace(
Visitor* v,
const void* ptr) {
227 v->TraceRoot(*persistent, persistent->Location());
230 bool IsValid()
const {
237 void Assign(T* ptr) {
242 this->CheckPointer(ptr);
249 if (!IsValid())
return;
252 this->CheckPointer(Get());
255 void ClearFromGC()
const {
264 T* GetFromGC()
const {
265 return static_cast<T*>(
const_cast<
void*>(
GetValue()));
271 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
272 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
273 typename LocationPolicy2,
typename CheckingPolicy2>
275 CheckingPolicy1>& p1,
277 CheckingPolicy2>& p2) {
278 return p1.Get() == p2.Get();
281 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
282 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
283 typename LocationPolicy2,
typename CheckingPolicy2>
285 CheckingPolicy1>& p1,
287 CheckingPolicy2>& p2) {
291 template <
typename T1,
typename PersistentWeaknessPolicy,
292 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
293 typename T2,
typename MemberWriteBarrierPolicy,
294 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
296 PersistentLocationPolicy,
297 PersistentCheckingPolicy>& p,
298 BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
299 MemberCheckingPolicy>
301 return p.Get() == m.Get();
304 template <
typename T1,
typename PersistentWeaknessPolicy,
305 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
306 typename T2,
typename MemberWriteBarrierPolicy,
307 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
309 PersistentLocationPolicy,
310 PersistentCheckingPolicy>& p,
311 BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
312 MemberCheckingPolicy>
317 template <
typename T1,
typename MemberWriteBarrierPolicy,
318 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
319 typename T2,
typename PersistentWeaknessPolicy,
320 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
321 bool operator==(BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
322 MemberCheckingPolicy>
325 PersistentLocationPolicy,
326 PersistentCheckingPolicy>& p) {
327 return m.Get() == p.Get();
330 template <
typename T1,
typename MemberWriteBarrierPolicy,
331 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
332 typename T2,
typename PersistentWeaknessPolicy,
333 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
334 bool operator!=(BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
335 MemberCheckingPolicy>
338 PersistentLocationPolicy,
339 PersistentCheckingPolicy>& p) {
343 template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
345 CheckingPolicy>> : std::true_type {};
355 template <
typename T>
365 template <
typename T>
366 using WeakPersistent =