Skip to content

Commit

Permalink
AK: Make it possible to swap() a NonnullRefPtr with itself
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Jan 19, 2020
1 parent 604c5cb commit 39b3c0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions AK/NonnullRefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr<
return stream << value.ptr();
}

template<typename T, typename U>
inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b)
{
a.swap(b);
}

}

using AK::adopt;
Expand Down
7 changes: 7 additions & 0 deletions AK/Tests/TestNonnullRefPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 39b3c0e

Please sign in to comment.