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/source-location.h"
13 #include "cppgc/type-traits.h"
14 #include "cppgc/visitor.h"
31 PersistentNode*
GetNode()
const {
return node_; }
32 void SetNode(PersistentNode* node) { node_ = node; }
42 mutable void* raw_ =
nullptr;
43 mutable PersistentNode* node_ =
nullptr;
45 friend class PersistentRegion;
49 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
50 typename CheckingPolicy>
52 public LocationPolicy,
53 private WeaknessPolicy,
54 private CheckingPolicy {
56 using typename WeaknessPolicy::IsStrongPersistent;
57 using PointeeType = T;
62 : LocationPolicy(loc) {}
66 : LocationPolicy(loc) {}
76 if (!IsValid())
return;
79 this->CheckPointer(Get());
92 template <
typename U,
typename OtherWeaknessPolicy,
93 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
94 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
97 OtherCheckingPolicy>& other,
106 :
PersistentBase(std::move(other)), LocationPolicy(std::move(other)) {
107 if (!IsValid())
return;
109 other.SetValue(
nullptr);
110 other.SetNode(
nullptr);
111 this->CheckPointer(Get());
115 template <
typename U,
typename MemberBarrierPolicy,
116 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
117 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
119 MemberWeaknessTag, MemberCheckingPolicy>
128 return operator=(other.Get());
131 template <
typename U,
typename OtherWeaknessPolicy,
132 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
133 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
136 OtherCheckingPolicy>& other) {
137 return operator=(other.Get());
142 if (
this == &other)
return *
this;
145 LocationPolicy::operator=(std::move(other));
146 if (!IsValid())
return *
this;
148 other.SetValue(
nullptr);
149 other.SetNode(
nullptr);
150 this->CheckPointer(Get());
155 template <
typename U,
typename MemberBarrierPolicy,
156 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
157 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
159 internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
160 MemberCheckingPolicy>
162 return operator=(member.Get());
201 static void Trace(
Visitor* v,
const void* ptr) {
203 v->TraceRoot(*persistent, persistent->Location());
206 bool IsValid()
const {
213 void Assign(T* ptr) {
218 this->CheckPointer(ptr);
225 if (!IsValid())
return;
228 this->CheckPointer(Get());
231 void ClearFromGC()
const {
241 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
242 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
243 typename LocationPolicy2,
typename CheckingPolicy2>
245 CheckingPolicy1>& p1,
247 CheckingPolicy2>& p2) {
248 return p1.Get() == p2.Get();
251 template <
typename T1,
typename WeaknessPolicy1,
typename LocationPolicy1,
252 typename CheckingPolicy1,
typename T2,
typename WeaknessPolicy2,
253 typename LocationPolicy2,
typename CheckingPolicy2>
255 CheckingPolicy1>& p1,
257 CheckingPolicy2>& p2) {
261 template <
typename T1,
typename PersistentWeaknessPolicy,
262 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
263 typename T2,
typename MemberWriteBarrierPolicy,
264 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
266 PersistentLocationPolicy,
267 PersistentCheckingPolicy>& p,
268 BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
269 MemberCheckingPolicy>
271 return p.Get() == m.Get();
274 template <
typename T1,
typename PersistentWeaknessPolicy,
275 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
276 typename T2,
typename MemberWriteBarrierPolicy,
277 typename MemberWeaknessTag,
typename MemberCheckingPolicy>
279 PersistentLocationPolicy,
280 PersistentCheckingPolicy>& p,
281 BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
282 MemberCheckingPolicy>
287 template <
typename T1,
typename MemberWriteBarrierPolicy,
288 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
289 typename T2,
typename PersistentWeaknessPolicy,
290 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
291 bool operator==(BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
292 MemberCheckingPolicy>
295 PersistentLocationPolicy,
296 PersistentCheckingPolicy>& p) {
297 return m.Get() == p.Get();
300 template <
typename T1,
typename MemberWriteBarrierPolicy,
301 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
302 typename T2,
typename PersistentWeaknessPolicy,
303 typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
304 bool operator!=(BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
305 MemberCheckingPolicy>
308 PersistentLocationPolicy,
309 PersistentCheckingPolicy>& p) {
313 template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
315 CheckingPolicy>> : std::true_type {};
325 template <
typename T>
335 template <
typename T>
336 using WeakPersistent =