UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
Object.h
1#pragma once
2#include <UnCompute/Memory/ReferenceCounter.h>
3
4namespace UN
5{
7 class IObject
8 {
9 public:
10 virtual ~IObject() = default;
11
15 virtual UInt32 AddRef() = 0;
16
23 virtual UInt32 Release() = 0;
24
31 virtual void AttachRefCounter(ReferenceCounter* counter) = 0;
32
37 };
38
49 template<class TInterface, std::enable_if_t<std::is_base_of_v<IObject, TInterface>, bool> = true>
50 class Object : public TInterface
51 {
52 ReferenceCounter* m_RefCounter = nullptr;
53
54 protected:
55 template<class F>
56 inline UInt32 Release(F&& destroyCallback)
57 {
58 return m_RefCounter->ReleaseStrongRef([this, &destroyCallback] {
59 destroyCallback();
60 this->~Object();
61 });
62 }
63
64 public:
65 Object() = default;
66
67 virtual ~Object() = default;
68
69 Object(const Object&) = delete;
70 Object(Object&&) = delete;
71
75 inline UInt32 AddRef() override
76 {
77 return m_RefCounter->AddStrongRef();
78 }
79
82 inline UInt32 Release() override
83 {
84 return Release([] {});
85 }
86
90 inline void AttachRefCounter(ReferenceCounter* pRefCounter) override
91 {
92 m_RefCounter = pRefCounter;
93 }
94
97 {
98 return m_RefCounter;
99 }
100 };
101} // namespace UN
Base interface for dynamic reference counted objects.
Definition: Object.h:8
virtual UInt32 Release()=0
Remove a strong reference from object's reference counter.
virtual UInt32 AddRef()=0
Add a strong reference to object's reference counter.
virtual ReferenceCounter * GetRefCounter()=0
Get reference counter that belongs to this object.
virtual void AttachRefCounter(ReferenceCounter *counter)=0
Attach a ReferenceCounter to this object.
Base class for dynamic reference counted objects.
Definition: Object.h:51
UInt32 Release() override
Definition: Object.h:82
void AttachRefCounter(ReferenceCounter *pRefCounter) override
Attach a reference counter to the object.
Definition: Object.h:90
ReferenceCounter * GetRefCounter() override
Get reference counter attached to this object.
Definition: Object.h:96
UInt32 AddRef() override
Add a strong reference to the object.
Definition: Object.h:75
The reference counter that holds the number of references to the object.
Definition: ReferenceCounter.h:41
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