diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h index f24c411ad5..c074ed140d 100644 --- a/AK/WeakPtr.h +++ b/AK/WeakPtr.h @@ -173,16 +173,6 @@ private: RefPtr m_link; }; -template -template -inline ErrorOr> Weakable::try_make_weak_ptr() const -{ - if (!m_link) - m_link = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WeakLink(const_cast(static_cast(*this))))); - - return WeakPtr(m_link); -} - template struct Formatter> : Formatter { ErrorOr format(FormatBuilder& builder, WeakPtr const& value) @@ -191,21 +181,6 @@ struct Formatter> : Formatter { } }; -template -ErrorOr> try_make_weak_ptr_if_nonnull(T const* ptr) -{ - if (ptr) { - return ptr->template try_make_weak_ptr(); - } - return WeakPtr {}; -} - -template -WeakPtr make_weak_ptr_if_nonnull(T const* ptr) -{ - return MUST(try_make_weak_ptr_if_nonnull(ptr)); -} - template struct Traits> : public DefaultTraits> { using PeekType = T*; diff --git a/AK/Weakable.h b/AK/Weakable.h index 8d5da57983..a3df203b60 100644 --- a/AK/Weakable.h +++ b/AK/Weakable.h @@ -6,10 +6,8 @@ #pragma once -#include #include #include -#include namespace AK { @@ -61,11 +59,11 @@ public: template WeakPtr make_weak_ptr() const { - return MUST(try_make_weak_ptr()); - } + if (!m_link) + m_link = adopt_ref(*new (nothrow) WeakLink(const_cast(static_cast(*this)))); - template - ErrorOr> try_make_weak_ptr() const; + return m_link; + } protected: Weakable() = default; @@ -75,7 +73,7 @@ protected: revoke_weak_ptrs(); } - void revoke_weak_ptrs() + void revoke_weak_ptrs() const { if (auto link = move(m_link)) link->revoke();