| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/Atomics.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
|
| 12 |
template <class T> class Ref; |
| 13 |
template <class T> class RefConst; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
template <class T> |
| 34 |
class RefTarget |
| 35 |
{ |
| 36 |
public: |
| 37 |
|
| 38 |
inline RefTarget() = default; |
| 39 |
inline RefTarget(const RefTarget &) { } |
| 40 |
inline ~RefTarget() { JPH_IF_ENABLE_ASSERTS(uint32 value = mRefCount.load(memory_order_relaxed);) JPH_ASSERT(value == 0 || value == cEmbedded); } |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
inline void SetEmbedded() const { JPH_IF_ENABLE_ASSERTS(uint32 old = ) mRefCount.fetch_add(cEmbedded, memory_order_relaxed); JPH_ASSERT(old < cEmbedded); } |
| 46 |
|
| 47 |
|
| 48 |
inline RefTarget & operator = (const RefTarget &) { return *this; } |
| 49 |
|
| 50 |
|
| 51 |
uint32 GetRefCount() const { return mRefCount.load(memory_order_relaxed); } |
| 52 |
|
| 53 |
|
| 54 |
inline void AddRef() const |
| 55 |
{ |
| 56 |
|
| 57 |
mRefCount.fetch_add(1, memory_order_relaxed); |
| 58 |
} |
| 59 |
|
| 60 |
inline void Release() const |
| 61 |
{ |
| 62 |
#ifndef JPH_TSAN_ENABLED |
| 63 |
|
| 64 |
if (mRefCount.fetch_sub(1, memory_order_release) == 1) |
| 65 |
{ |
| 66 |
|
| 67 |
atomic_thread_fence(memory_order_acquire); |
| 68 |
delete static_cast<const T *>(this); |
| 69 |
} |
| 70 |
#else |
| 71 |
|
| 72 |
if (mRefCount.fetch_sub(1, memory_order_acq_rel) == 1) |
| 73 |
delete static_cast<const T *>(this); |
| 74 |
#endif |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
static int sInternalGetRefCountOffset() { return offsetof(T, mRefCount); } |
| 79 |
|
| 80 |
protected: |
| 81 |
static constexpr uint32 cEmbedded = 0x0ebedded; |
| 82 |
|
| 83 |
mutable atomic<uint32> mRefCount = 0; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
class JPH_EXPORT RefTargetVirtual |
| 88 |
{ |
| 89 |
public: |
| 90 |
|
| 91 |
virtual ~RefTargetVirtual() = default; |
| 92 |
|
| 93 |
|
| 94 |
virtual void AddRef() = 0; |
| 95 |
|
| 96 |
|
| 97 |
virtual void Release() = 0; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
template <class T> |
| 106 |
class Ref |
| 107 |
{ |
| 108 |
public: |
| 109 |
|
| 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 |
|
| 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 |
|
| 122 |
inline operator T *() const { return mPtr; } |
| 123 |
|
| 124 |
|
| 125 |
inline T * operator -> () const { return mPtr; } |
| 126 |
inline T & operator * () const { return *mPtr; } |
| 127 |
|
| 128 |
|
| 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 |
|
| 135 |
inline T * GetPtr() const { return mPtr; } |
| 136 |
|
| 137 |
|
| 138 |
uint64 GetHash() const |
| 139 |
{ |
| 140 |
return Hash<T *> { } (mPtr); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
void ** InternalGetPointer() { return reinterpret_cast<void **>(&mPtr); } |
| 145 |
|
| 146 |
private: |
| 147 |
template <class T2> friend class RefConst; |
| 148 |
|
| 149 |
|
| 150 |
inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); } |
| 151 |
inline void Release() { if (mPtr != nullptr) mPtr->Release(); } |
| 152 |
|
| 153 |
T * mPtr; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
template <class T> |
| 162 |
class RefConst |
| 163 |
{ |
| 164 |
public: |
| 165 |
|
| 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 |
|
| 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 |
|
| 182 |
inline operator const T * () const { return mPtr; } |
| 183 |
|
| 184 |
|
| 185 |
inline const T * operator -> () const { return mPtr; } |
| 186 |
inline const T & operator * () const { return *mPtr; } |
| 187 |
|
| 188 |
|
| 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 |
|
| 197 |
inline const T * GetPtr() const { return mPtr; } |
| 198 |
|
| 199 |
|
| 200 |
uint64 GetHash() const |
| 201 |
{ |
| 202 |
return Hash<const T *> { } (mPtr); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
void ** InternalGetPointer() { return const_cast<void **>(reinterpret_cast<const void **>(&mPtr)); } |
| 207 |
|
| 208 |
private: |
| 209 |
|
| 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 |
|
| 216 |
JPH_NAMESPACE_END |
| 217 |
|
| 218 |
JPH_SUPPRESS_WARNING_PUSH |
| 219 |
JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") |
| 220 |
|
| 221 |
namespace std |
| 222 |
{ |
| 223 |
|
| 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 |
|
| 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 |