From 5653829bf8503c38f705893ebbf6b11dd69b211b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= Date: Thu, 2 Jan 2025 10:30:59 +0100 Subject: [PATCH] Make QUniqueHandle::reset() default to invalid handle Having a defaulted argument to QUniqueHandle::reset() gives a convenient way of releasing ownership without assigning a new handle, and makes the QUniqueHandle API more similar to std::unique_ptr. Task-number: QTBUG-132507 Pick-to: 6.8 Change-Id: I842d4545d7cc2da8fe2df08280d0d816ed4be7fd Reviewed-by: Tim Blechmann Reviewed-by: Thiago Macieira (cherry picked from commit 248ac4128fe86150532ff7146d0459abb5260946) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/quniquehandle_p.h | 2 +- .../tools/quniquehandle/tst_quniquehandle.cpp | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/quniquehandle_p.h b/src/corelib/tools/quniquehandle_p.h index 7af1536c2ed..881dea07ba3 100644 --- a/src/corelib/tools/quniquehandle_p.h +++ b/src/corelib/tools/quniquehandle_p.h @@ -151,7 +151,7 @@ public: return m_handle; } - void reset(const Type& handle) noexcept + void reset(const Type& handle = HandleTraits::invalidValue()) noexcept { if (handle == m_handle) return; diff --git a/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp b/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp index ed46999e737..bc8f4685610 100644 --- a/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp +++ b/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp @@ -186,7 +186,7 @@ private slots: QCOMPARE_EQ(valid.get(), resource); } - void reset_resetsPreviousValueAndTakesOwnership() const + void reset_resetsPreviousValueAndTakesOwnership_whenCalledWithHandle() const { const auto resource0 = GlobalResource::open(); const auto resource1 = GlobalResource::open(); @@ -198,6 +198,25 @@ private slots: QVERIFY(GlobalResource::isOpen(resource1)); } + void reset_resetsOwningHandleToInvalid_whenCalledWithNoArguments() const + { + const auto resource0 = GlobalResource::open(); + + Handle h{ resource0 }; + h.reset(); + + QVERIFY(!GlobalResource::isOpen(resource0)); + QVERIFY(!h); + } + + void reset_doesNothing_whenCalledOnInvalidHandle() const + { + Handle h{ }; + h.reset(); + + QVERIFY(!h); + } + void release_returnsInvalidResource_whenCalledOnInvalidHandle() const { Handle h;