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"
32 void SetValue(
const void* value) { raw_ = value; }
34 PersistentNode*
GetNode()
const {
return node_; }
35 void SetNode(PersistentNode* node) { node_ = node; }
45 mutable const void* raw_ =
nullptr;
46 mutable PersistentNode* node_ =
nullptr;
48 friend class PersistentRegion;
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 {
265 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
266 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
267 typename LocationPolicy2,
typename CheckingPolicy2>
269 CheckingPolicy1>& p1,
271 CheckingPolicy2>& p2) {
272 return p1.Get() == p2.Get();
275 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
276 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
277 typename LocationPolicy2,
typename CheckingPolicy2>
279 CheckingPolicy1>& p1,
281 CheckingPolicy2>& p2) {
285 template <
typename T1,
typename PersistentWeaknessPolicy,
286 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
287 typename T2,
typename MemberWriteBarrierPolicy,
288 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
290 PersistentLocationPolicy,
291 PersistentCheckingPolicy>& p,
292 BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
293 MemberCheckingPolicy>
295 return p.Get() == m.Get();
298 template <
typename T1,
typename PersistentWeaknessPolicy,
299 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
300 typename T2,
typename MemberWriteBarrierPolicy,
301 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
303 PersistentLocationPolicy,
304 PersistentCheckingPolicy>& p,
305 BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
306 MemberCheckingPolicy>
311 template <
typename T1,
typename MemberWriteBarrierPolicy,
312 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
313 typename T2,
typename PersistentWeaknessPolicy,
314 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
315 bool operator==(BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
316 MemberCheckingPolicy>
319 PersistentLocationPolicy,
320 PersistentCheckingPolicy>& p) {
321 return m.Get() == p.Get();
324 template <
typename T1,
typename MemberWriteBarrierPolicy,
325 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
326 typename T2,
typename PersistentWeaknessPolicy,
327 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
328 bool operator!=(BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
329 MemberCheckingPolicy>
332 PersistentLocationPolicy,
333 PersistentCheckingPolicy>& p) {
337 template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
339 CheckingPolicy>> : std::true_type {};
349 template <
typename T>
359 template <
typename T>
360 using WeakPersistent =