Merge pull request #1731 from marauder2k9-torque/RefBase-changes

Update refBase.h
This commit is contained in:
Brian Roberts 2026-05-11 16:13:04 -05:00 committed by GitHub
commit a416573d69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -32,6 +32,7 @@
#include <memory> #include <memory>
#include <type_traits> #include <type_traits>
#include <atomic>
class WeakRefBase; class WeakRefBase;
@ -140,34 +141,35 @@ class StrongRefBase : public WeakRefBase
friend class StrongObjectRef; friend class StrongObjectRef;
public: public:
StrongRefBase() StrongRefBase() = default;
{
mRefCount = 0;
}
virtual ~StrongRefBase() = default; virtual ~StrongRefBase() = default;
U32 getRefCount() const { return mRefCount; } U32 getRefCount() const
/// object destroy self call (from StrongRefPtr). Override if this class has specially allocated memory.
virtual void destroySelf() { delete this; }
/// Increments the reference count.
void incRefCount()
{ {
mRefCount++; return mRefCount.load(std::memory_order_relaxed);
}
virtual void destroySelf()
{
delete this;
}
void incRefCount()
{
mRefCount.fetch_add(1, std::memory_order_relaxed);
} }
/// Decrements the reference count.
void decRefCount() void decRefCount()
{ {
AssertFatal(mRefCount, "Decrementing a reference with refcount 0!"); if (mRefCount.fetch_sub(1, std::memory_order_acq_rel) == 1)
if (!--mRefCount) {
std::atomic_thread_fence(std::memory_order_acquire);
destroySelf(); destroySelf();
} }
}
protected: protected:
U32 mRefCount; ///< reference counter for StrongRefPtr objects std::atomic<U32> mRefCount{ 0 };
}; };
@ -308,8 +310,14 @@ private:
release(); release();
if (!ptr) return; if (!ptr) return;
// Always hold the identity if constexpr (std::is_base_of_v<StrongRefBase, T>)
{
mControl = ptr->getSharedControl();
}
else
{
mControl = ptr->getWeakControl().lock(); mControl = ptr->getWeakControl().lock();
}
if (!mControl) return; if (!mControl) return;
mPtr = ptr; mPtr = ptr;
@ -318,7 +326,9 @@ private:
// runtime check: only strong ref if T inherits StrongRefBase // runtime check: only strong ref if T inherits StrongRefBase
if constexpr (std::is_base_of_v<StrongRefBase, T>) if constexpr (std::is_base_of_v<StrongRefBase, T>)
{ {
mPtr->incRefCount(); T* live = getPointer();
if (live)
live->incRefCount();
} }
} }
@ -328,7 +338,9 @@ private:
{ {
if constexpr (std::is_base_of_v<StrongRefBase, T>) if constexpr (std::is_base_of_v<StrongRefBase, T>)
{ {
mPtr->decRefCount(); T* live = getPointer();
if (live)
live->decRefCount();
} }
} }