BAN: RefPtr can be constructed from other types

This commit is contained in:
Bananymous 2023-07-10 09:41:39 +03:00
parent 19dab08275
commit 74af46cb4a
1 changed files with 26 additions and 1 deletions

View File

@ -53,7 +53,6 @@ namespace BAN
if (m_pointer)
m_pointer->ref();
}
~RefPtr() { clear(); }
template<typename U>
@ -75,6 +74,10 @@ namespace BAN
RefPtr(const RefPtr& other) { *this = other; }
RefPtr(RefPtr&& other) { *this = move(other); }
template<typename U>
RefPtr(const RefPtr<U>& other) { *this = other; }
template<typename U>
RefPtr(RefPtr<U>&& other) { *this = move(other); }
RefPtr& operator=(const RefPtr& other)
{
@ -93,6 +96,25 @@ namespace BAN
return *this;
}
template<typename U>
RefPtr& operator=(const RefPtr<U>& other)
{
clear();
m_pointer = other.m_pointer;
if (m_pointer)
m_pointer->ref();
return *this;
}
template<typename U>
RefPtr& operator=(RefPtr<U>&& other)
{
clear();
m_pointer = other.m_pointer;
other.m_pointer = nullptr;
return *this;
}
T* ptr() { ASSERT(!empty()); return m_pointer; }
const T* ptr() const { ASSERT(!empty()); return m_pointer; }
@ -114,6 +136,9 @@ namespace BAN
private:
T* m_pointer = nullptr;
template<typename U>
friend class RefPtr;
};
}