mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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
Notes:
sideshowbarker
2024-07-19 12:56:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/15866714da0
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)
|
||||
: m_ptr(const_cast<T*>(other.ptr()))
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
m_ptr->ref();
|
||||
}
|
||||
template<typename U>
|
||||
RefPtr(const NonnullRefPtr<U>& other)
|
||||
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
m_ptr->ref();
|
||||
}
|
||||
template<typename U>
|
||||
RefPtr(NonnullRefPtr<U>&& other)
|
||||
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
}
|
||||
template<typename U>
|
||||
RefPtr(RefPtr<U>&& other)
|
||||
|
@ -121,6 +124,7 @@ public:
|
|||
{
|
||||
RefPtr tmp = move(other);
|
||||
swap(tmp);
|
||||
ASSERT(m_ptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -128,6 +132,7 @@ public:
|
|||
{
|
||||
RefPtr tmp = other;
|
||||
swap(tmp);
|
||||
ASSERT(m_ptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -136,6 +141,7 @@ public:
|
|||
{
|
||||
RefPtr tmp = other;
|
||||
swap(tmp);
|
||||
ASSERT(m_ptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -192,11 +198,29 @@ public:
|
|||
T* ptr() { return m_ptr; }
|
||||
const T* ptr() const { return m_ptr; }
|
||||
|
||||
T* operator->() { return m_ptr; }
|
||||
const T* operator->() const { return m_ptr; }
|
||||
T* operator->()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
T& operator*() { return *m_ptr; }
|
||||
const T& operator*() const { return *m_ptr; }
|
||||
const T* operator->() const
|
||||
{
|
||||
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 T*() { return m_ptr; }
|
||||
|
|
Loading…
Reference in a new issue