SharedPtr: Fixes and support for casting to other SharedPtr types
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-31 19:59:06 +01:00
parent 3a84e4998c
commit e9a009478f
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -42,7 +42,7 @@ template <typename T> class SharedPtr
SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count) SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count)
{ {
m_ref_count->ref(); if (m_ref_count) m_ref_count->ref();
} }
SharedPtr(SharedPtr<T>&& other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count) SharedPtr(SharedPtr<T>&& other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count)
@ -51,6 +51,12 @@ template <typename T> class SharedPtr
other.m_ref_count = nullptr; other.m_ref_count = nullptr;
} }
template <typename Tp> operator SharedPtr<Tp>()
{
if (m_ref_count) m_ref_count->ref();
return { (Tp*)m_ptr, m_ref_count };
}
~SharedPtr() ~SharedPtr()
{ {
if (m_ref_count && m_ref_count->unref()) if (m_ref_count && m_ref_count->unref())
@ -73,7 +79,7 @@ template <typename T> class SharedPtr
m_ptr = other.m_ptr; m_ptr = other.m_ptr;
m_ref_count = other.m_ref_count; m_ref_count = other.m_ref_count;
m_ref_count->ref(); if (m_ref_count) m_ref_count->ref();
return *this; return *this;
} }