Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/Reference.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Core/Atomics.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 // Forward declares
12 template <class T> class Ref;
13 template <class T> class RefConst;
14
15 /// Simple class to facilitate reference counting / releasing
16 /// Derive your class from RefTarget and you can reference it by using Ref<classname> or RefConst<classname>
17 ///
18 /// Reference counting classes keep an integer which indicates how many references
19 /// to the object are active. Reference counting objects are derived from RefTarget
20 /// and staT & their life with a reference count of zero. They can then be assigned
21 /// to equivalents of pointers (Ref) which will increase the reference count immediately.
22 /// If the destructor of Ref is called or another object is assigned to the reference
23 /// counting pointer it will decrease the reference count of the object again. If this
24 /// reference count becomes zero, the object is destroyed.
25 ///
26 /// This provides a very powerful mechanism to prevent memory leaks, but also gives
27 /// some responsibility to the programmer. The most notable point is that you cannot
28 /// have one object reference another and have the other reference the first one
29 /// back, because this way the reference count of both objects will never become
30 /// lower than 1, resulting in a memory leak. By carefully designing your classes
31 /// (and particularly identifying who owns who in the class hierarchy) you can avoid
32 /// these problems.
33 template <class T>
34 class RefTarget
35 {
36 public:
37 /// Constructor
38 inline RefTarget() = default;
39 inline RefTarget(const RefTarget &) { /* Do not copy refcount */ }
40 inline ~RefTarget() { JPH_IF_ENABLE_ASSERTS(uint32 value = mRefCount.load(memory_order_relaxed);) JPH_ASSERT(value == 0 || value == cEmbedded); } ///< assert no one is referencing us
41
42 /// Mark this class as embedded, this means the type can be used in a compound or constructed on the stack.
43 /// The Release function will never destruct the object, it is assumed the destructor will be called by whoever allocated
44 /// the object and at that point in time it is checked that no references are left to the structure.
45 inline void SetEmbedded() const { JPH_IF_ENABLE_ASSERTS(uint32 old = ) mRefCount.fetch_add(cEmbedded, memory_order_relaxed); JPH_ASSERT(old < cEmbedded); }
46
47 /// Assignment operator
48 inline RefTarget & operator = (const RefTarget &) { /* Don't copy refcount */ return *this; }
49
50 /// Get current refcount of this object
51 uint32 GetRefCount() const { return mRefCount.load(memory_order_relaxed); }
52
53 /// Add or release a reference to this object
54 inline void AddRef() const
55 {
56 // Adding a reference can use relaxed memory ordering
57 mRefCount.fetch_add(1, memory_order_relaxed);
58 }
59
60 inline void Release() const
61 {
62 #ifndef JPH_TSAN_ENABLED
63 // Releasing a reference must use release semantics...
64 if (mRefCount.fetch_sub(1, memory_order_release) == 1)
65 {
66 // ... so that we can use acquire to ensure that we see any updates from other threads that released a ref before deleting the object
67 atomic_thread_fence(memory_order_acquire);
68 delete static_cast<const T *>(this);
69 }
70 #else
71 // But under TSAN, we cannot use atomic_thread_fence, so we use an acq_rel operation unconditionally instead
72 if (mRefCount.fetch_sub(1, memory_order_acq_rel) == 1)
73 delete static_cast<const T *>(this);
74 #endif
75 }
76
77 /// INTERNAL HELPER FUNCTION USED BY SERIALIZATION
78 static int sInternalGetRefCountOffset() { return offsetof(T, mRefCount); }
79
80 protected:
81 static constexpr uint32 cEmbedded = 0x0ebedded; ///< A large value that gets added to the refcount to mark the object as embedded
82
83 mutable atomic<uint32> mRefCount = 0; ///< Current reference count
84 };
85
86 /// Pure virtual version of RefTarget
87 class JPH_EXPORT RefTargetVirtual
88 {
89 public:
90 /// Virtual destructor
91 virtual ~RefTargetVirtual() = default;
92
93 /// Virtual add reference
94 virtual void AddRef() = 0;
95
96 /// Virtual release reference
97 virtual void Release() = 0;
98 };
99
100 /// Class for automatic referencing, this is the equivalent of a pointer to type T
101 /// if you assign a value to this class it will increment the reference count by one
102 /// of this object, and if you assign something else it will decrease the reference
103 /// count of the first object again. If it reaches a reference count of zero it will
104 /// be deleted
105 template <class T>
106 class Ref
107 {
108 public:
109 /// Constructor
110 inline Ref() : mPtr(nullptr) { }
111 inline Ref(T *inRHS) : mPtr(inRHS) { AddRef(); }
112 inline Ref(const Ref<T> &inRHS) : mPtr(inRHS.mPtr) { AddRef(); }
113 inline Ref(Ref<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr = nullptr; }
114 inline ~Ref() { Release(); }
115
116 /// Assignment operators
117 inline Ref<T> & operator = (T *inRHS) { if (mPtr != inRHS) { Release(); mPtr = inRHS; AddRef(); } return *this; }
118 inline Ref<T> & operator = (const Ref<T> &inRHS) { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; AddRef(); } return *this; }
119 inline Ref<T> & operator = (Ref<T> &&inRHS) noexcept { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr = nullptr; } return *this; }
120
121 /// Casting operators
122 inline operator T *() const { return mPtr; }
123
124 /// Access like a normal pointer
125 inline T * operator -> () const { return mPtr; }
126 inline T & operator * () const { return *mPtr; }
127
128 /// Comparison
129 inline bool operator == (const T * inRHS) const { return mPtr == inRHS; }
130 inline bool operator == (const Ref<T> &inRHS) const { return mPtr == inRHS.mPtr; }
131 inline bool operator != (const T * inRHS) const { return mPtr != inRHS; }
132 inline bool operator != (const Ref<T> &inRHS) const { return mPtr != inRHS.mPtr; }
133
134 /// Get pointer
135 inline T * GetPtr() const { return mPtr; }
136
137 /// Get hash for this object
138 uint64 GetHash() const
139 {
140 return Hash<T *> { } (mPtr);
141 }
142
143 /// INTERNAL HELPER FUNCTION USED BY SERIALIZATION
144 void ** InternalGetPointer() { return reinterpret_cast<void **>(&mPtr); }
145
146 private:
147 template <class T2> friend class RefConst;
148
149 /// Use "variable = nullptr;" to release an object, do not call these functions
150 inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); }
151 inline void Release() { if (mPtr != nullptr) mPtr->Release(); }
152
153 T * mPtr; ///< Pointer to object that we are reference counting
154 };
155
156 /// Class for automatic referencing, this is the equivalent of a CONST pointer to type T
157 /// if you assign a value to this class it will increment the reference count by one
158 /// of this object, and if you assign something else it will decrease the reference
159 /// count of the first object again. If it reaches a reference count of zero it will
160 /// be deleted
161 template <class T>
162 class RefConst
163 {
164 public:
165 /// Constructor
166 inline RefConst() : mPtr(nullptr) { }
167 inline RefConst(const T * inRHS) : mPtr(inRHS) { AddRef(); }
168 inline RefConst(const RefConst<T> &inRHS) : mPtr(inRHS.mPtr) { AddRef(); }
169 inline RefConst(RefConst<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr = nullptr; }
170 inline RefConst(const Ref<T> &inRHS) : mPtr(inRHS.mPtr) { AddRef(); }
171 inline RefConst(Ref<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr = nullptr; }
172 inline ~RefConst() { Release(); }
173
174 /// Assignment operators
175 inline RefConst<T> & operator = (const T * inRHS) { if (mPtr != inRHS) { Release(); mPtr = inRHS; AddRef(); } return *this; }
176 inline RefConst<T> & operator = (const RefConst<T> &inRHS) { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; AddRef(); } return *this; }
177 inline RefConst<T> & operator = (RefConst<T> &&inRHS) noexcept { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr = nullptr; } return *this; }
178 inline RefConst<T> & operator = (const Ref<T> &inRHS) { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; AddRef(); } return *this; }
179 inline RefConst<T> & operator = (Ref<T> &&inRHS) noexcept { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr = nullptr; } return *this; }
180
181 /// Casting operators
182 inline operator const T * () const { return mPtr; }
183
184 /// Access like a normal pointer
185 inline const T * operator -> () const { return mPtr; }
186 inline const T & operator * () const { return *mPtr; }
187
188 /// Comparison
189 inline bool operator == (const T * inRHS) const { return mPtr == inRHS; }
190 inline bool operator == (const RefConst<T> &inRHS) const { return mPtr == inRHS.mPtr; }
191 inline bool operator == (const Ref<T> &inRHS) const { return mPtr == inRHS.mPtr; }
192 inline bool operator != (const T * inRHS) const { return mPtr != inRHS; }
193 inline bool operator != (const RefConst<T> &inRHS) const { return mPtr != inRHS.mPtr; }
194 inline bool operator != (const Ref<T> &inRHS) const { return mPtr != inRHS.mPtr; }
195
196 /// Get pointer
197 inline const T * GetPtr() const { return mPtr; }
198
199 /// Get hash for this object
200 uint64 GetHash() const
201 {
202 return Hash<const T *> { } (mPtr);
203 }
204
205 /// INTERNAL HELPER FUNCTION USED BY SERIALIZATION
206 void ** InternalGetPointer() { return const_cast<void **>(reinterpret_cast<const void **>(&mPtr)); }
207
208 private:
209 /// Use "variable = nullptr;" to release an object, do not call these functions
210 inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); }
211 inline void Release() { if (mPtr != nullptr) mPtr->Release(); }
212
213 const T * mPtr; ///< Pointer to object that we are reference counting
214 };
215
216 JPH_NAMESPACE_END
217
218 JPH_SUPPRESS_WARNING_PUSH
219 JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
220
221 namespace std
222 {
223 /// Declare std::hash for Ref
224 template <class T>
225 struct hash<JPH::Ref<T>>
226 {
227 size_t operator () (const JPH::Ref<T> &inRHS) const
228 {
229 return size_t(inRHS.GetHash());
230 }
231 };
232
233 /// Declare std::hash for RefConst
234 template <class T>
235 struct hash<JPH::RefConst<T>>
236 {
237 size_t operator () (const JPH::RefConst<T> &inRHS) const
238 {
239 return size_t(inRHS.GetHash());
240 }
241 };
242 }
243
244 JPH_SUPPRESS_WARNING_POP