mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:07:44 +00:00
AK: Make it more more pleasant to copy RefPtr's.
I had a silly ambition that we would avoid unnecessary ref count churn by forcing explicit use of "copy_ref()" wherever a copy was actually needed. This was making RefPtr a bit clunky to work with, for no real benefit. This patch adds the missing copy construction/assignment stuff to RefPtr.
This commit is contained in:
parent
32fb7ecef4
commit
560d037c41
1 changed files with 30 additions and 0 deletions
30
AK/RefPtr.h
30
AK/RefPtr.h
|
@ -42,6 +42,17 @@ public:
|
||||||
: m_ptr(other.leak_ref())
|
: m_ptr(other.leak_ref())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
RefPtr(const NonnullRefPtr<T>& other)
|
||||||
|
: m_ptr(const_cast<T*>(other.ptr()))
|
||||||
|
{
|
||||||
|
m_ptr->ref();
|
||||||
|
}
|
||||||
|
template<typename U>
|
||||||
|
RefPtr(const NonnullRefPtr<U>& other)
|
||||||
|
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
|
||||||
|
{
|
||||||
|
m_ptr->ref();
|
||||||
|
}
|
||||||
template<typename U>
|
template<typename U>
|
||||||
RefPtr(NonnullRefPtr<U>&& other)
|
RefPtr(NonnullRefPtr<U>&& other)
|
||||||
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
||||||
|
@ -100,6 +111,16 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RefPtr& operator=(const NonnullRefPtr<T>& other)
|
||||||
|
{
|
||||||
|
if (m_ptr != other.ptr())
|
||||||
|
deref_if_not_null(m_ptr);
|
||||||
|
m_ptr = const_cast<T*>(other.ptr());
|
||||||
|
ASSERT(m_ptr);
|
||||||
|
ref_if_not_null(m_ptr);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
RefPtr& operator=(const NonnullRefPtr<U>& other)
|
RefPtr& operator=(const NonnullRefPtr<U>& other)
|
||||||
{
|
{
|
||||||
|
@ -111,6 +132,15 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RefPtr& operator=(const RefPtr& other)
|
||||||
|
{
|
||||||
|
if (m_ptr != other.ptr())
|
||||||
|
deref_if_not_null(m_ptr);
|
||||||
|
m_ptr = const_cast<T*>(other.ptr());
|
||||||
|
ref_if_not_null(m_ptr);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
RefPtr& operator=(const RefPtr<U>& other)
|
RefPtr& operator=(const RefPtr<U>& other)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue