Skip to content

Commit b4f13b5

Browse files
committed
Add RefCountersAllocationImpl for SelfDestroy
1 parent 6414a66 commit b4f13b5

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

Common/interface/RefCountedObjectImpl.hpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Diligent
4343
{
4444

4545
// This class controls the lifetime of a refcounted object
46-
class RefCountersImpl final : public IReferenceCounters
46+
class RefCountersImpl : public IReferenceCounters
4747
{
4848
public:
4949
inline virtual ReferenceCounterValueType AddStrongRef() override final
@@ -215,14 +215,15 @@ class RefCountersImpl final : public IReferenceCounters
215215
return m_NumWeakReferences.load();
216216
}
217217

218-
private:
218+
protected:
219219
template <typename AllocatorType, typename ObjectType>
220220
friend class MakeNewRCObj;
221221

222222
RefCountersImpl() noexcept
223223
{
224224
}
225225

226+
private:
226227
class ObjectWrapperBase
227228
{
228229
public:
@@ -448,17 +449,16 @@ class RefCountersImpl final : public IReferenceCounters
448449
}
449450
}
450451

451-
void SelfDestroy()
452-
{
453-
delete this;
454-
}
452+
protected:
453+
virtual void SelfDestroy() = 0;
455454

456455
~RefCountersImpl()
457456
{
458457
VERIFY(m_NumStrongReferences.load() == 0 && m_NumWeakReferences.load() == 0,
459458
"There exist outstanding references to the object being destroyed");
460459
}
461460

461+
private:
462462
// No copies/moves
463463
// clang-format off
464464
RefCountersImpl (const RefCountersImpl&) = delete;
@@ -492,6 +492,21 @@ class RefCountersImpl final : public IReferenceCounters
492492
std::atomic<ObjectState> m_ObjectState{ObjectState::NotInitialized};
493493
};
494494

495+
class RefCountersAllocationImpl : public RefCountersImpl
496+
{
497+
private:
498+
template <typename AllocatorType, typename ObjectType>
499+
friend class MakeNewRCObj;
500+
501+
RefCountersAllocationImpl() noexcept
502+
{
503+
}
504+
505+
void SelfDestroy() override
506+
{
507+
delete this;
508+
}
509+
};
495510

496511
/// Base class for all reference counting objects
497512
template <typename Base>
@@ -654,7 +669,7 @@ class MakeNewRCObj
654669
{
655670
// Constructor of RefCountersImpl class is private and only accessible
656671
// by methods of MakeNewRCObj
657-
pNewRefCounters = new RefCountersImpl{};
672+
pNewRefCounters = new RefCountersAllocationImpl{};
658673
pRefCounters = pNewRefCounters;
659674
}
660675
ObjectType* pObj = nullptr;

Primitives/interface/ReferenceCounters.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ typedef Int32 ReferenceCounterValueType;
4444
class IReferenceCounters
4545
{
4646
public:
47+
virtual ~IReferenceCounters() {};
48+
4749
/// Increments the number of strong references by 1.
4850

4951
/// \return The number of strong references after incrementing the counter.

0 commit comments

Comments
 (0)