mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-14 08:04:40 +00:00
Merge pull request #1731 from marauder2k9-torque/RefBase-changes
Update refBase.h
This commit is contained in:
commit
a416573d69
1 changed files with 39 additions and 27 deletions
|
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
class WeakRefBase;
|
class WeakRefBase;
|
||||||
|
|
||||||
|
|
@ -137,37 +138,38 @@ private:
|
||||||
/// when all strong references go away, object is destroyed).
|
/// when all strong references go away, object is destroyed).
|
||||||
class StrongRefBase : public WeakRefBase
|
class StrongRefBase : public WeakRefBase
|
||||||
{
|
{
|
||||||
friend class StrongObjectRef;
|
friend class StrongObjectRef;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StrongRefBase()
|
StrongRefBase() = default;
|
||||||
{
|
virtual ~StrongRefBase() = default;
|
||||||
mRefCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~StrongRefBase() = default;
|
U32 getRefCount() const
|
||||||
|
{
|
||||||
|
return mRefCount.load(std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
U32 getRefCount() const { return mRefCount; }
|
virtual void destroySelf()
|
||||||
|
{
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
|
||||||
/// object destroy self call (from StrongRefPtr). Override if this class has specially allocated memory.
|
void incRefCount()
|
||||||
virtual void destroySelf() { delete this; }
|
{
|
||||||
|
mRefCount.fetch_add(1, std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
/// Increments the reference count.
|
void decRefCount()
|
||||||
void incRefCount()
|
{
|
||||||
{
|
if (mRefCount.fetch_sub(1, std::memory_order_acq_rel) == 1)
|
||||||
mRefCount++;
|
{
|
||||||
}
|
std::atomic_thread_fence(std::memory_order_acquire);
|
||||||
|
destroySelf();
|
||||||
/// Decrements the reference count.
|
}
|
||||||
void decRefCount()
|
}
|
||||||
{
|
|
||||||
AssertFatal(mRefCount, "Decrementing a reference with refcount 0!");
|
|
||||||
if (!--mRefCount)
|
|
||||||
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->getWeakControl().lock();
|
{
|
||||||
|
mControl = ptr->getSharedControl();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue