Plasma Engine  2.0
Loading...
Searching...
No Matches
RefCounted.h
1
2#pragma once
3
4#include <Foundation/Threading/AtomicUtils.h>
5
6class PL_FOUNDATION_DLL plRefCountingImpl
7{
8public:
10 plRefCountingImpl() = default; // [tested]
11
12 plRefCountingImpl(const plRefCountingImpl& rhs) // [tested]
13 {
14 // do not copy the ref count
15 }
16
17 void operator=(const plRefCountingImpl& rhs) // [tested]
18 {
19 // do not copy the ref count
20 }
21
23 inline plInt32 AddRef() const // [tested]
24 {
25 return plAtomicUtils::Increment(m_iRefCount);
26 }
27
29 inline plInt32 ReleaseRef() const // [tested]
30 {
31 return plAtomicUtils::Decrement(m_iRefCount);
32 }
33
35 inline bool IsReferenced() const // [tested]
36 {
37 return m_iRefCount > 0;
38 }
39
41 inline plInt32 GetRefCount() const // [tested]
42 {
43 return m_iRefCount;
44 }
45
46private:
47 mutable plInt32 m_iRefCount = 0;
48};
49
51class PL_FOUNDATION_DLL plRefCounted : public plRefCountingImpl
52{
53public:
55 virtual ~plRefCounted() = default;
56};
57
63template <typename T>
65{
66public:
69 : m_pReferencedObject(nullptr)
70 {
71 }
72
74 plScopedRefPointer(T* pReferencedObject)
75 : m_pReferencedObject(pReferencedObject)
76 {
77 AddReferenceIfValid();
78 }
79
81 {
82 m_pReferencedObject = other.m_pReferencedObject;
83
84 AddReferenceIfValid();
85 }
86
88 ~plScopedRefPointer() { ReleaseReferenceIfValid(); }
89
92 void operator=(T* pNewReference)
93 {
94 if (pNewReference == m_pReferencedObject)
95 return;
96
97 ReleaseReferenceIfValid();
98
99 m_pReferencedObject = pNewReference;
100
101 AddReferenceIfValid();
102 }
103
107 {
108 if (other.m_pReferencedObject == m_pReferencedObject)
109 return;
110
111 ReleaseReferenceIfValid();
112
113 m_pReferencedObject = other.m_pReferencedObject;
114
115 AddReferenceIfValid();
116 }
117
119 operator const T*() const { return m_pReferencedObject; }
120
122 operator T*() { return m_pReferencedObject; }
123
125 const T* operator->() const
126 {
127 PL_ASSERT_DEV(m_pReferencedObject != nullptr, "Pointer is nullptr.");
128 return m_pReferencedObject;
129 }
130
133 {
134 PL_ASSERT_DEV(m_pReferencedObject != nullptr, "Pointer is nullptr.");
135 return m_pReferencedObject;
136 }
137
138private:
140 inline void AddReferenceIfValid()
141 {
142 if (m_pReferencedObject != nullptr)
143 {
144 m_pReferencedObject->AddRef();
145 }
146 }
147
149 inline void ReleaseReferenceIfValid()
150 {
151 if (m_pReferencedObject != nullptr)
152 {
153 m_pReferencedObject->ReleaseRef();
154 }
155 }
156
157 T* m_pReferencedObject;
158};
159
160
161template <typename TYPE>
163{
164public:
165 TYPE m_Content;
166};
Definition RefCounted.h:163
Base class for reference counted objects.
Definition RefCounted.h:52
virtual ~plRefCounted()=default
Adds a virtual destructor.
Definition RefCounted.h:7
plInt32 ReleaseRef() const
Decrements the reference counter. Returns the new reference count.
Definition RefCounted.h:29
plRefCountingImpl()=default
Constructor.
bool IsReferenced() const
Returns true if the reference count is greater than 0, false otherwise.
Definition RefCounted.h:35
plInt32 GetRefCount() const
Returns the current reference count.
Definition RefCounted.h:41
plInt32 AddRef() const
Increments the reference counter. Returns the new reference count.
Definition RefCounted.h:23
Stores a pointer to a reference counted object and automatically increases / decreases the reference ...
Definition RefCounted.h:65
plScopedRefPointer()
Constructor.
Definition RefCounted.h:68
void operator=(const plScopedRefPointer< T > &other)
Assignment operator, decreases the ref count of the currently referenced object and increases the ref...
Definition RefCounted.h:106
T * operator->()
Returns the referenced object (may be nullptr)
Definition RefCounted.h:132
const T * operator->() const
Returns the referenced object (may be nullptr).
Definition RefCounted.h:125
void operator=(T *pNewReference)
Assignment operator, decreases the ref count of the currently referenced object and increases the ref...
Definition RefCounted.h:92
plScopedRefPointer(T *pReferencedObject)
Constructor, increases the ref count of the given object.
Definition RefCounted.h:74
~plScopedRefPointer()
Destructor - releases the reference on the ref-counted object (if there is one).
Definition RefCounted.h:88
static plInt32 Decrement(plInt32 &ref_iDest)
Decrements dest as an atomic operation and returns the new value.
Definition AtomicUtils_posix.h:31
static plInt32 Increment(plInt32 &ref_iDest)
Increments dest as an atomic operation and returns the new value.
Definition AtomicUtils_posix.h:20