diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 13a71e42178..5af4d083479 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -100,11 +100,8 @@ public: RETURN_TYPESTATE(unconsumed) NonnullOwnPtr& operator=(NonnullOwnPtr&& other) { - if (this != &other) { - delete m_ptr; - m_ptr = other.leak_ptr(); - ASSERT(m_ptr); - } + NonnullOwnPtr ptr(move(other)); + swap(ptr); return *this; } @@ -112,11 +109,8 @@ public: RETURN_TYPESTATE(unconsumed) NonnullOwnPtr& operator=(NonnullOwnPtr&& other) { - if (this != static_cast(&other)) { - delete m_ptr; - m_ptr = other.leak_ptr(); - ASSERT(m_ptr); - } + NonnullOwnPtr ptr(move(other)); + swap(ptr); return *this; } @@ -150,6 +144,17 @@ public: operator bool() const = delete; bool operator!() const = delete; + void swap(NonnullOwnPtr& other) + { + ::swap(m_ptr, other.m_ptr); + } + + template + void swap(NonnullOwnPtr& other) + { + ::swap(m_ptr, other.m_ptr); + } + private: void clear() { @@ -183,6 +188,12 @@ inline const LogStream& operator<<(const LogStream& stream, const NonnullOwnPtr< return stream << value.ptr(); } +template +inline void swap(NonnullOwnPtr& a, NonnullOwnPtr& b) +{ + a.swap(b); +} + } using AK::make;