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 const internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
123 MemberCheckingPolicy>& member,
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 const internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
163 MemberCheckingPolicy>& member) {
164 return operator=(member.Get());
194 return static_cast<T*>(
const_cast<
void*>(
GetValue()));
213 template <
typename U,
typename OtherWeaknessPolicy = WeaknessPolicy,
214 typename OtherLocationPolicy = LocationPolicy,
215 typename OtherCheckingPolicy = CheckingPolicy>
220 OtherCheckingPolicy>(
static_cast<U*>(Get()));
224 static void Trace(
Visitor* v,
const void* ptr) {
226 v->TraceRoot(*persistent, persistent->Location());
229 bool IsValid()
const {
236 void Assign(T* ptr) {
241 this->CheckPointer(ptr);
248 if (!IsValid())
return;
251 this->CheckPointer(Get());
254 void ClearFromGC()
const {
263 T* GetFromGC()
const {
264 return static_cast<T*>(
const_cast<
void*>(
GetValue()));
270 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
271 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
272 typename LocationPolicy2,
typename CheckingPolicy2>
274 CheckingPolicy1>& p1,
276 CheckingPolicy2>& p2) {
277 return p1.Get() == p2.Get();
280 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
281 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
282 typename LocationPolicy2,
typename CheckingPolicy2>
284 CheckingPolicy1>& p1,
286 CheckingPolicy2>& p2) {
290 template <
typename T1,
typename PersistentWeaknessPolicy,
291 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
292 typename T2,
typename MemberWriteBarrierPolicy,
293 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
296 PersistentLocationPolicy, PersistentCheckingPolicy>&
298 const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
299 MemberCheckingPolicy>& m) {
300 return p.Get() == m.Get();
303 template <
typename T1,
typename PersistentWeaknessPolicy,
304 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
305 typename T2,
typename MemberWriteBarrierPolicy,
306 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
309 PersistentLocationPolicy, PersistentCheckingPolicy>&
311 const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
312 MemberCheckingPolicy>& m) {
316 template <
typename T1,
typename MemberWriteBarrierPolicy,
317 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
318 typename T2,
typename PersistentWeaknessPolicy,
319 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
321 const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
322 MemberCheckingPolicy>& m,
324 PersistentLocationPolicy, PersistentCheckingPolicy>&
326 return m.Get() == p.Get();
329 template <
typename T1,
typename MemberWriteBarrierPolicy,
330 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
331 typename T2,
typename PersistentWeaknessPolicy,
332 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
334 const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
335 MemberCheckingPolicy>& m,
337 PersistentLocationPolicy, PersistentCheckingPolicy>&
342 template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
344 CheckingPolicy>> : std::true_type {};
354 template <
typename T>
364 template <
typename T>
365 using WeakPersistent =