mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:47:46 +00:00
AK+Kernel: Add an OOM-fallible try variant make_weak_ptr()
This will allow us to propagate allocation errors that may be raised by the construction of the WeakLink.
This commit is contained in:
parent
d6ea6c39a7
commit
98c20b65cc
3 changed files with 28 additions and 14 deletions
16
AK/WeakPtr.h
16
AK/WeakPtr.h
|
@ -151,10 +151,10 @@ private:
|
|||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
|
||||
inline ErrorOr<WeakPtr<U>> Weakable<T>::try_make_weak_ptr() const
|
||||
{
|
||||
if (!m_link)
|
||||
m_link = adopt_ref(*new WeakLink(const_cast<T&>(static_cast<T const&>(*this))));
|
||||
m_link = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WeakLink(const_cast<T&>(static_cast<T const&>(*this)))));
|
||||
|
||||
return WeakPtr<U>(m_link);
|
||||
}
|
||||
|
@ -168,12 +168,18 @@ struct Formatter<WeakPtr<T>> : Formatter<const T*> {
|
|||
};
|
||||
|
||||
template<typename T>
|
||||
WeakPtr<T> make_weak_ptr_if_nonnull(const T* ptr)
|
||||
ErrorOr<WeakPtr<T>> try_make_weak_ptr_if_nonnull(T const* ptr)
|
||||
{
|
||||
if (ptr) {
|
||||
return ptr->template make_weak_ptr<T>();
|
||||
return ptr->template try_make_weak_ptr<T>();
|
||||
}
|
||||
return {};
|
||||
return WeakPtr<T> {};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
WeakPtr<T> make_weak_ptr_if_nonnull(T const* ptr)
|
||||
{
|
||||
return MUST(try_make_weak_ptr_if_nonnull(ptr));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -106,7 +106,9 @@ private:
|
|||
|
||||
public:
|
||||
template<typename U = T>
|
||||
WeakPtr<U> make_weak_ptr() const;
|
||||
WeakPtr<U> make_weak_ptr() const { return MUST(try_make_weak_ptr<U>()); }
|
||||
template<typename U = T>
|
||||
ErrorOr<WeakPtr<U>> try_make_weak_ptr() const;
|
||||
|
||||
protected:
|
||||
Weakable() = default;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue