From 39b3c0ef7ef599b95ac74de594deb06097a115c9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 19 Jan 2020 10:29:21 +0100 Subject: [PATCH] AK: Make it possible to swap() a NonnullRefPtr with itself The generic swap() is not able to swap a NonnullRefPtr with itself, due to its use of a temporary and NonnullRefPtr asserting when trying to move() from an already move()'d instance. --- AK/NonnullRefPtr.h | 6 ++++++ AK/Tests/TestNonnullRefPtr.cpp | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 46686927e9..a30a3c96bc 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -267,6 +267,12 @@ inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr< return stream << value.ptr(); } +template +inline void swap(NonnullRefPtr& a, NonnullRefPtr& b) +{ + a.swap(b); +} + } using AK::adopt; diff --git a/AK/Tests/TestNonnullRefPtr.cpp b/AK/Tests/TestNonnullRefPtr.cpp index a8c77ae1ea..1cd183c0e3 100644 --- a/AK/Tests/TestNonnullRefPtr.cpp +++ b/AK/Tests/TestNonnullRefPtr.cpp @@ -73,4 +73,11 @@ TEST_CASE(assign_owner_of_self) EXPECT_EQ(child->ref_count(), 1); } +TEST_CASE(swap_with_self) +{ + auto object = adopt(*new Object); + swap(object, object); + EXPECT_EQ(object->ref_count(), 1); +} + TEST_MAIN(String)