mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00:00
AK: Add anti-null assertions in RefPtr.
This gives us better error messages when dereferencing null RefPtrs.
This commit is contained in:
parent
cbc1272810
commit
15866714da
1 changed files with 28 additions and 4 deletions
32
AK/RefPtr.h
32
AK/RefPtr.h
|
@ -49,18 +49,21 @@ public:
|
||||||
RefPtr(const NonnullRefPtr<T>& other)
|
RefPtr(const NonnullRefPtr<T>& other)
|
||||||
: m_ptr(const_cast<T*>(other.ptr()))
|
: m_ptr(const_cast<T*>(other.ptr()))
|
||||||
{
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
template<typename U>
|
template<typename U>
|
||||||
RefPtr(const NonnullRefPtr<U>& other)
|
RefPtr(const NonnullRefPtr<U>& other)
|
||||||
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
|
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
|
||||||
{
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
m_ptr->ref();
|
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()))
|
||||||
{
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
}
|
}
|
||||||
template<typename U>
|
template<typename U>
|
||||||
RefPtr(RefPtr<U>&& other)
|
RefPtr(RefPtr<U>&& other)
|
||||||
|
@ -121,6 +124,7 @@ public:
|
||||||
{
|
{
|
||||||
RefPtr tmp = move(other);
|
RefPtr tmp = move(other);
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
|
ASSERT(m_ptr);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,6 +132,7 @@ public:
|
||||||
{
|
{
|
||||||
RefPtr tmp = other;
|
RefPtr tmp = other;
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
|
ASSERT(m_ptr);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +141,7 @@ public:
|
||||||
{
|
{
|
||||||
RefPtr tmp = other;
|
RefPtr tmp = other;
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
|
ASSERT(m_ptr);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,11 +198,29 @@ public:
|
||||||
T* ptr() { return m_ptr; }
|
T* ptr() { return m_ptr; }
|
||||||
const T* ptr() const { return m_ptr; }
|
const T* ptr() const { return m_ptr; }
|
||||||
|
|
||||||
T* operator->() { return m_ptr; }
|
T* operator->()
|
||||||
const T* operator->() const { return m_ptr; }
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
|
return m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
T& operator*() { return *m_ptr; }
|
const T* operator->() const
|
||||||
const T& operator*() const { return *m_ptr; }
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
|
return m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& operator*()
|
||||||
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
|
return *m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& operator*() const
|
||||||
|
{
|
||||||
|
ASSERT(m_ptr);
|
||||||
|
return *m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
operator const T*() const { return m_ptr; }
|
operator const T*() const { return m_ptr; }
|
||||||
operator T*() { return m_ptr; }
|
operator T*() { return m_ptr; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue