Plasma Engine  2.0
Loading...
Searching...
No Matches
SharedPtr_inl.h
1
2template <typename T>
4{
5 m_pInstance = nullptr;
6 m_pAllocator = nullptr;
7}
8
9template <typename T>
10template <typename U>
11PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(const plInternal::NewInstance<U>& instance)
12{
13 m_pInstance = instance.m_pInstance;
14 m_pAllocator = instance.m_pAllocator;
16 AddReferenceIfValid();
17}
18
19template <typename T>
20template <typename U>
21PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(U* pInstance, plAllocator* pAllocator)
22{
23 m_pInstance = pInstance;
24 m_pAllocator = pAllocator;
25
26 AddReferenceIfValid();
28
29template <typename T>
30PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(const plSharedPtr<T>& other)
31{
32 m_pInstance = other.m_pInstance;
33 m_pAllocator = other.m_pAllocator;
34
35 AddReferenceIfValid();
36}
37
38template <typename T>
39template <typename U>
40PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(const plSharedPtr<U>& other)
41{
42 m_pInstance = other.m_pInstance;
43 m_pAllocator = other.m_pAllocator;
44
45 AddReferenceIfValid();
46}
47
48template <typename T>
49template <typename U>
50PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(plSharedPtr<U>&& other)
51{
52 m_pInstance = other.m_pInstance;
53 m_pAllocator = other.m_pAllocator;
54
55 other.m_pInstance = nullptr;
56 other.m_pAllocator = nullptr;
57}
58
59template <typename T>
60template <typename U>
61PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(plUniquePtr<U>&& other)
62{
63 m_pInstance = other.Release(m_pAllocator);
64
65 AddReferenceIfValid();
66}
68template <typename T>
69PL_ALWAYS_INLINE plSharedPtr<T>::plSharedPtr(std::nullptr_t)
71 m_pInstance = nullptr;
72 m_pAllocator = nullptr;
74
75template <typename T>
77{
78 ReleaseReferenceIfValid();
80
81template <typename T>
82template <typename U>
84{
85 ReleaseReferenceIfValid();
86
87 m_pInstance = instance.m_pInstance;
88 m_pAllocator = instance.m_pAllocator;
89
90 AddReferenceIfValid();
92 return *this;
93}
94
95template <typename T>
97{
98 if (m_pInstance != other.m_pInstance)
99 {
100 ReleaseReferenceIfValid();
101
102 m_pInstance = other.m_pInstance;
103 m_pAllocator = other.m_pAllocator;
104
105 AddReferenceIfValid();
106 }
107
108 return *this;
109}
110
111template <typename T>
112template <typename U>
113PL_ALWAYS_INLINE plSharedPtr<T>& plSharedPtr<T>::operator=(const plSharedPtr<U>& other)
114{
115 if (m_pInstance != other.m_pInstance)
116 {
117 ReleaseReferenceIfValid();
118
119 m_pInstance = other.m_pInstance;
120 m_pAllocator = other.m_pAllocator;
121
122 AddReferenceIfValid();
123 }
124
125 return *this;
126}
127
128template <typename T>
129template <typename U>
131{
132 if (m_pInstance != other.m_pInstance)
133 {
134 ReleaseReferenceIfValid();
135
136 m_pInstance = other.m_pInstance;
137 m_pAllocator = other.m_pAllocator;
138
139 other.m_pInstance = nullptr;
140 other.m_pAllocator = nullptr;
141 }
142
143 return *this;
144}
145
146template <typename T>
147template <typename U>
149{
150 ReleaseReferenceIfValid();
151
152 m_pInstance = other.Release(m_pAllocator);
153
154 AddReferenceIfValid();
155
156 return *this;
157}
158
159template <typename T>
160PL_ALWAYS_INLINE plSharedPtr<T>& plSharedPtr<T>::operator=(std::nullptr_t)
161{
162 ReleaseReferenceIfValid();
163
164 return *this;
165}
166
167template <typename T>
168PL_ALWAYS_INLINE T* plSharedPtr<T>::Borrow() const
169{
170 return m_pInstance;
171}
172
173template <typename T>
174PL_ALWAYS_INLINE void plSharedPtr<T>::Clear()
175{
176 ReleaseReferenceIfValid();
177}
178
179template <typename T>
180PL_ALWAYS_INLINE T& plSharedPtr<T>::operator*() const
181{
182 return *m_pInstance;
183}
184
185template <typename T>
186PL_ALWAYS_INLINE T* plSharedPtr<T>::operator->() const
187{
188 return m_pInstance;
189}
190
191template <typename T>
192PL_ALWAYS_INLINE plSharedPtr<T>::operator const T*() const
193{
194 return m_pInstance;
195}
196
197template <typename T>
198PL_ALWAYS_INLINE plSharedPtr<T>::operator T*()
199{
200 return m_pInstance;
201}
202
203template <typename T>
204PL_ALWAYS_INLINE plSharedPtr<T>::operator bool() const
205{
206 return m_pInstance != nullptr;
207}
208
209template <typename T>
210PL_ALWAYS_INLINE bool plSharedPtr<T>::operator==(const plSharedPtr<T>& rhs) const
211{
212 return m_pInstance == rhs.m_pInstance;
213}
214
215template <typename T>
216PL_ALWAYS_INLINE bool plSharedPtr<T>::operator!=(const plSharedPtr<T>& rhs) const
217{
218 return m_pInstance != rhs.m_pInstance;
219}
220
221template <typename T>
222PL_ALWAYS_INLINE bool plSharedPtr<T>::operator<(const plSharedPtr<T>& rhs) const
223{
224 return m_pInstance < rhs.m_pInstance;
225}
226
227template <typename T>
228PL_ALWAYS_INLINE bool plSharedPtr<T>::operator<=(const plSharedPtr<T>& rhs) const
229{
230 return !(rhs < *this);
231}
232
233template <typename T>
234PL_ALWAYS_INLINE bool plSharedPtr<T>::operator>(const plSharedPtr<T>& rhs) const
235{
236 return rhs < *this;
237}
238
239template <typename T>
240PL_ALWAYS_INLINE bool plSharedPtr<T>::operator>=(const plSharedPtr<T>& rhs) const
241{
242 return !(*this < rhs);
243}
244
245template <typename T>
246PL_ALWAYS_INLINE bool plSharedPtr<T>::operator==(std::nullptr_t) const
247{
248 return m_pInstance == nullptr;
249}
250
251template <typename T>
252PL_ALWAYS_INLINE bool plSharedPtr<T>::operator!=(std::nullptr_t) const
253{
254 return m_pInstance != nullptr;
255}
256
257template <typename T>
258PL_ALWAYS_INLINE bool plSharedPtr<T>::operator<(std::nullptr_t) const
259{
260 return m_pInstance < nullptr;
261}
262
263template <typename T>
264PL_ALWAYS_INLINE bool plSharedPtr<T>::operator<=(std::nullptr_t) const
265{
266 return m_pInstance <= nullptr;
267}
268
269template <typename T>
270PL_ALWAYS_INLINE bool plSharedPtr<T>::operator>(std::nullptr_t) const
271{
272 return m_pInstance > nullptr;
273}
274
275template <typename T>
276PL_ALWAYS_INLINE bool plSharedPtr<T>::operator>=(std::nullptr_t) const
277{
278 return m_pInstance >= nullptr;
279}
280
281template <typename T>
282PL_ALWAYS_INLINE void plSharedPtr<T>::AddReferenceIfValid()
283{
284 if (m_pInstance != nullptr)
285 {
286 m_pInstance->AddRef();
287 }
288}
289
290template <typename T>
291PL_ALWAYS_INLINE void plSharedPtr<T>::ReleaseReferenceIfValid()
292{
293 if (m_pInstance != nullptr)
294 {
295 if (m_pInstance->ReleaseRef() == 0)
296 {
297 auto pNonConstInstance = const_cast<typename plTypeTraits<T>::NonConstType*>(m_pInstance);
298 PL_DELETE(m_pAllocator, pNonConstInstance);
299 }
300
301 m_pInstance = nullptr;
302 m_pAllocator = nullptr;
303 }
304}
Base class for all memory allocators.
Definition Allocator.h:23
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
void Clear()
Destroys the managed object if no one else references it anymore and resets the shared ptr.
Definition SharedPtr_inl.h:174
T * operator->() const
Provides access to the managed object.
Definition SharedPtr_inl.h:186
plSharedPtr< T > & operator=(const plInternal::NewInstance< U > &instance)
Sets the shared ptr from a freshly created instance through PL_NEW or PL_DEFAULT_NEW.
bool operator==(const plSharedPtr< T > &rhs) const
Compares the shared ptr against another shared ptr.
Definition SharedPtr_inl.h:210
T * Borrow() const
Borrows the managed object. The shared ptr stays unmodified.
Definition SharedPtr_inl.h:168
T & operator*() const
Provides access to the managed object.
Definition SharedPtr_inl.h:180
~plSharedPtr()
Destroys the managed object using the stored allocator if no one else references it anymore.
Definition SharedPtr_inl.h:76
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
Definition Allocator_inl.h:18
typename std::remove_const< T >::type NonConstType
removes const qualifier
Definition TypeTraits.h:209