12template <
class T>
class Ref;
57 mRefCount.fetch_add(1, memory_order_relaxed);
62 #ifndef JPH_TSAN_ENABLED
64 if (
mRefCount.fetch_sub(1, memory_order_release) == 1)
67 atomic_thread_fence(memory_order_acquire);
68 delete static_cast<const T *
>(
this);
72 if (
mRefCount.fetch_sub(1, memory_order_acq_rel) == 1)
73 delete static_cast<const T *
>(
this);
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; }
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; }
122 inline operator T *()
const {
return mPtr; }
129 inline bool operator == (
const T * inRHS)
const {
return mPtr == inRHS; }
131 inline bool operator != (
const T * inRHS)
const {
return mPtr != inRHS; }
135 inline T *
GetPtr()
const {
return mPtr; }
144 inline void AddRef() {
if (mPtr !=
nullptr) mPtr->AddRef(); }
145 inline void Release() {
if (mPtr !=
nullptr) mPtr->Release(); }
161 inline RefConst(
const T * inRHS) : mPtr(inRHS) { AddRef(); }
165 inline RefConst(
Ref<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr =
nullptr; }
169 inline RefConst<T> &
operator = (
const T * inRHS) {
if (mPtr != inRHS) { Release(); mPtr = inRHS; AddRef(); }
return *
this; }
173 inline RefConst<T> &
operator = (
Ref<T> &&inRHS)
noexcept {
if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr =
nullptr; }
return *
this; }
176 inline operator const T * ()
const {
return mPtr; }
183 inline bool operator == (
const T * inRHS)
const {
return mPtr == inRHS; }
186 inline bool operator != (
const T * inRHS)
const {
return mPtr != inRHS; }
191 inline const T *
GetPtr()
const {
return mPtr; }
198 inline void AddRef() {
if (mPtr !=
nullptr) mPtr->AddRef(); }
199 inline void Release() {
if (mPtr !=
nullptr) mPtr->Release(); }
206JPH_SUPPRESS_WARNING_PUSH
215 size_t operator () (
const JPH::Ref<T> &inRHS)
const
217 return hash<T *> { }(inRHS.GetPtr());
225 size_t operator () (
const JPH::RefConst<T> &inRHS)
const
227 return hash<const T *> { }(inRHS.GetPtr());
232JPH_SUPPRESS_WARNING_POP
#define JPH_EXPORT
Definition: Core.h:236
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition: Core.h:263
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_IF_ENABLE_ASSERTS(...)
Definition: IssueReporting.h:35
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
Definition: Reference.h:157
const T * GetPtr() const
Get pointer.
Definition: Reference.h:191
bool operator!=(const T *inRHS) const
Definition: Reference.h:186
RefConst()
Constructor.
Definition: Reference.h:160
~RefConst()
Definition: Reference.h:166
RefConst(const T *inRHS)
Definition: Reference.h:161
const T & operator*() const
Definition: Reference.h:180
RefConst< T > & operator=(const T *inRHS)
Assignment operators.
Definition: Reference.h:169
RefConst(const Ref< T > &inRHS)
Definition: Reference.h:164
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition: Reference.h:194
const T * operator->() const
Access like a normal pointer.
Definition: Reference.h:179
RefConst(RefConst< T > &&inRHS) noexcept
Definition: Reference.h:163
RefConst(const RefConst< T > &inRHS)
Definition: Reference.h:162
bool operator==(const T *inRHS) const
Comparison.
Definition: Reference.h:183
RefConst(Ref< T > &&inRHS) noexcept
Definition: Reference.h:165
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:138
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
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.