UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
ReferenceCounter.h
1#pragma once
2#include <UnCompute/Base/Base.h>
3#include <UnCompute/Memory/IAllocator.h>
4
5namespace UN
6{
7 class IObject;
8
40 class ReferenceCounter final
41 {
42 std::atomic<Int32> m_StrongRefCount;
43 mutable IAllocator* m_pAllocator;
44
45 public:
52 inline explicit ReferenceCounter(IAllocator* pAllocator)
53 : m_StrongRefCount(0)
54 , m_pAllocator(pAllocator)
55 {
56 }
57
61 inline UInt32 AddStrongRef()
62 {
63 return ++m_StrongRefCount;
64 }
65
75 template<class F>
76 inline UInt32 ReleaseStrongRef(F&& destroyCallback)
77 {
78 if (--m_StrongRefCount == 0)
79 {
80 destroyCallback();
81 m_pAllocator->Deallocate(this);
82 }
83
84 return m_StrongRefCount;
85 }
86
88 inline UInt32 GetStrongRefCount() const
89 {
90 return m_StrongRefCount;
91 }
92 };
93} // namespace UN
An interface for memory allocators.
Definition: IAllocator.h:8
virtual void Deallocate(void *pointer)=0
Deallocate memory.
The reference counter that holds the number of references to the object.
Definition: ReferenceCounter.h:41
ReferenceCounter(IAllocator *pAllocator)
Create a new reference counter with specified allocator.
Definition: ReferenceCounter.h:52
UInt32 ReleaseStrongRef(F &&destroyCallback)
Remove a strong reference from the counter.
Definition: ReferenceCounter.h:76
UInt32 AddStrongRef()
Add a strong reference to the counter.
Definition: ReferenceCounter.h:61
UInt32 GetStrongRefCount() const
Get number of strong references.
Definition: ReferenceCounter.h:88