Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Reference.h
Go to the documentation of this file.
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
10
11// Forward declares
12template <class T> class Ref;
13template <class T> class RefConst;
14
33template <class T>
35{
36public:
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); }
41
45 inline void SetEmbedded() const { JPH_IF_ENABLE_ASSERTS(uint32 old = ) mRefCount.fetch_add(cEmbedded, memory_order_relaxed); JPH_ASSERT(old < cEmbedded); }
46
48 inline RefTarget & operator = (const RefTarget &) { /* Don't copy refcount */ return *this; }
49
51 uint32 GetRefCount() const { return mRefCount.load(memory_order_relaxed); }
52
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
78 static int sInternalGetRefCountOffset() { return offsetof(T, mRefCount); }
79
80protected:
81 static constexpr uint32 cEmbedded = 0x0ebedded;
82
83 mutable atomic<uint32> mRefCount = 0;
84};
85
88{
89public:
91 virtual ~RefTargetVirtual() = default;
92
94 virtual void AddRef() = 0;
95
97 virtual void Release() = 0;
98};
99
105template <class T>
106class Ref
107{
108public:
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
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
122 inline operator T *() const { return mPtr; }
123
125 inline T * operator -> () const { return mPtr; }
126 inline T & operator * () const { return *mPtr; }
127
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
135 inline T * GetPtr() const { return mPtr; }
136
139 {
140 return Hash<T *> { } (mPtr);
141 }
142
144 void ** InternalGetPointer() { return reinterpret_cast<void **>(&mPtr); }
145
146private:
147 template <class T2> friend class RefConst;
148
150 inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); }
151 inline void Release() { if (mPtr != nullptr) mPtr->Release(); }
152
153 T * mPtr;
154};
155
161template <class T>
163{
164public:
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
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
182 inline operator const T * () const { return mPtr; }
183
185 inline const T * operator -> () const { return mPtr; }
186 inline const T & operator * () const { return *mPtr; }
187
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
197 inline const T * GetPtr() const { return mPtr; }
198
201 {
202 return Hash<const T *> { } (mPtr);
203 }
204
206 void ** InternalGetPointer() { return const_cast<void **>(reinterpret_cast<const void **>(&mPtr)); }
207
208private:
210 inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); }
211 inline void Release() { if (mPtr != nullptr) mPtr->Release(); }
212
213 const T * mPtr;
214};
215
217
218JPH_SUPPRESS_WARNING_PUSH
219JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
220
221namespace std
222{
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
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
244JPH_SUPPRESS_WARNING_POP
#define JPH_EXPORT
Definition Core.h:271
std::uint64_t uint64
Definition Core.h:485
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition Core.h:298
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#define JPH_IF_ENABLE_ASSERTS(...)
Definition IssueReporting.h:35
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
Definition Reference.h:163
const T * GetPtr() const
Get pointer.
Definition Reference.h:197
bool operator!=(const T *inRHS) const
Definition Reference.h:192
RefConst()
Constructor.
Definition Reference.h:166
~RefConst()
Definition Reference.h:172
RefConst(const T *inRHS)
Definition Reference.h:167
const T & operator*() const
Definition Reference.h:186
RefConst< T > & operator=(const T *inRHS)
Assignment operators.
Definition Reference.h:175
RefConst(const Ref< T > &inRHS)
Definition Reference.h:170
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition Reference.h:206
const T * operator->() const
Access like a normal pointer.
Definition Reference.h:185
RefConst(RefConst< T > &&inRHS) noexcept
Definition Reference.h:169
uint64 GetHash() const
Get hash for this object.
Definition Reference.h:200
RefConst(const RefConst< T > &inRHS)
Definition Reference.h:168
bool operator==(const T *inRHS) const
Comparison.
Definition Reference.h:189
RefConst(Ref< T > &&inRHS) noexcept
Definition Reference.h:171
Definition Reference.h:107
Ref(const Ref< T > &inRHS)
Definition Reference.h:112
T * GetPtr() const
Get pointer.
Definition Reference.h:135
bool operator!=(const T *inRHS) const
Definition Reference.h:131
Ref()
Constructor.
Definition Reference.h:110
~Ref()
Definition Reference.h:114
T * operator->() const
Access like a normal pointer.
Definition Reference.h:125
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition Reference.h:144
Ref< T > & operator=(T *inRHS)
Assignment operators.
Definition Reference.h:117
Ref(Ref< T > &&inRHS) noexcept
Definition Reference.h:113
Ref(T *inRHS)
Definition Reference.h:111
uint64 GetHash() const
Get hash for this object.
Definition Reference.h:138
bool operator==(const T *inRHS) const
Comparison.
Definition Reference.h:129
T & operator*() const
Definition Reference.h:126
Definition Reference.h:35
void Release() const
Definition Reference.h:60
atomic< uint32 > mRefCount
Current reference count.
Definition Reference.h:83
RefTarget & operator=(const RefTarget &)
Assignment operator.
Definition Reference.h:48
void SetEmbedded() const
Definition Reference.h:45
RefTarget()=default
Constructor.
RefTarget(const RefTarget &)
Definition Reference.h:39
static int sInternalGetRefCountOffset()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition Reference.h:78
~RefTarget()
assert no one is referencing us
Definition Reference.h:40
uint32 GetRefCount() const
Get current refcount of this object.
Definition Reference.h:51
void AddRef() const
Add or release a reference to this object.
Definition Reference.h:54
static constexpr uint32 cEmbedded
A large value that gets added to the refcount to mark the object as embedded.
Definition Reference.h:81
Pure virtual version of RefTarget.
Definition Reference.h:88
virtual ~RefTargetVirtual()=default
Virtual destructor.
virtual void Release()=0
Virtual release reference.
virtual void AddRef()=0
Virtual add reference.
Definition Array.h:590
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59