Repository: LadybirdBrowser/ladybird
File: AK/WeakPtr.h (C/C++)

Code before:
        return *unsafe_ptr();
    }

    operator bool() const { return m_link ? !m_link->is_null() : false; }

    [[nodiscard]] bool is_null() const { return !m_link || m_link->is_null(); }
    [[nodiscard]] bool has_value() const { return !is_null(); }

    void clear() { m_link = nullptr; }

    [[nodiscard]] RefPtr<WeakLink> take_link() { return move(m_link); }

private:
    WeakPtr(RefPtr<WeakLink> const& link)
        : m_link(link)
    {
    }

    RefPtr<WeakLink> m_link;
};

template<typename T, typename Callback>
auto weak_callback(T& obj, Callback&& callback)
{
    return [weak = obj.template make_weak_ptr<T>(), cb = forward<Callback>(callback)](auto&&... args) {
        if (auto strong = weak.strong_ref())
            cb(*strong, forward<decltype(args)>(args)...);
    };
}

template<typename T>
struct Formatter<WeakPtr<T>> : Formatter<T const*> {
    ErrorOr<void> format(FormatBuilder& builder, WeakPtr<T> const& value)
    {
        return Formatter<T const*>::format(builder, value.ptr());
    }
};

template<typename T>
struct Traits<WeakPtr<T>> : public DefaultTraits<WeakPtr<T>> {
    using PeekType = T*;
    using ConstPeekType = T const*;
    static unsigned hash(WeakPtr<T> const& p) { return ptr_hash(p.ptr()); }
    static bool equals(WeakPtr<T> const& a, WeakPtr<T> const& b) { return a.ptr() == b.ptr(); }
    static constexpr bool may_have_slow_equality_check() { return false; }
};

}

#if USING_AK_GLOBALLY
using AK::weak_callback;

Review comment (bug):
In #8155, the `Request` type is not ref-counted, so `strong_ref` would fail to compile. Something like this seems to work:
```c++
template<typename T, typename Callback>
static auto weak_callback(T& obj, Callback&& callback)
{
    return [weak = obj.template make_weak_ptr<T>(), callback = forward<Callback>(callback)](auto&&... args) {
        if constexpr (requires { obj.ref(); obj.unref(); }) {
            if (auto strong = weak.strong_ref())
                callback(*strong, forward<decltype(args)>(args)...);
        } else {
            if (weak)
                callback(*weak, forward<decltype(args)>(args)...);
        }
    };
}
```

Code after:
        VERIFY(has_value());
        return *unsafe_ptr();
    }

    operator bool() const { return m_link ? !m_link->is_null() : false; }

    [[nodiscard]] bool is_null() const { return !m_link || m_link->is_null(); }
    [[nodiscard]] bool has_value() const { return !is_null(); }

    void clear() { m_link = nullptr; }

    [[nodiscard]] RefPtr<WeakLink> take_link() { return move(m_link); }

private:
    WeakPtr(RefPtr<WeakLink> const& link)
        : m_link(link)
    {
    }

    RefPtr<WeakLink> m_link;
};

template<typename T, typename Callback>
auto weak_callback(T& obj, Callback&& callback)
requires(!IsBaseOf<AtomicRefCountedBase, T>)
{
    return [weak = obj.template make_weak_ptr<T>(), cb = forward<Callback>(callback)](auto&&... args) {
        if (weak)
            cb(*weak, forward<decltype(args)>(args)...);
    };
}

template<typename T>
struct Formatter<WeakPtr<T>> : Formatter<T const*> {
    ErrorOr<void> format(FormatBuilder& builder, WeakPtr<T> const& value)
    {
        return Formatter<T const*>::format(builder, value.ptr());
    }
};

template<typename T>
struct Traits<WeakPtr<T>> : public DefaultTraits<WeakPtr<T>> {
    using PeekType = T*;
    using ConstPeekType = T const*;
    static unsigned hash(WeakPtr<T> const& p) { return ptr_hash(p.ptr()); }
    static bool equals(WeakPtr<T> const& a, WeakPtr<T> const& b) { return a.ptr() == b.ptr(); }
    static constexpr bool may_have_slow_equality_check() { return false; }
};

}