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 <tim.blechmann@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 248ac4128fe86150532ff7146d0459abb5260946)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Jøger Hansegård 2025-01-02 10:30:59 +01:00 committed by Qt Cherry-pick Bot
parent 2378455647
commit 5653829bf8
2 changed files with 21 additions and 2 deletions

View File

@ -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;

View File

@ -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;