From caee2d3e8f66b1ac31df993eab92828f39392402 Mon Sep 17 00:00:00 2001 From: "l.bermes@asctechnologies.com" Date: Mon, 28 Oct 2024 16:16:26 +0100 Subject: [PATCH] Extended QSharedPointer and QWeakPointer with isShared and needsDetach This like it is used in QArrayDataPointer and allows to evaluate if cloing of an object is needed when it needs to be changed without affecting other references --- src/corelib/tools/qsharedpointer_impl.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index ceb4553b635..d3f757148d7 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -114,6 +114,14 @@ namespace QtSharedPointer { void destroy() { destroyer(this); } + bool isShared() const noexcept { + return strongref.loadRelaxed() != 1; + } + + bool needsDetach() const noexcept { + return strongref.loadRelaxed() > 1; + } + #ifndef QT_NO_QOBJECT Q_CORE_EXPORT static ExternalRefCountData *getAndRef(const QObject *); QT6_ONLY( @@ -463,6 +471,11 @@ template class QSharedPointer size_t owner_hash() const noexcept { return std::hash()(d); } + bool isShared() const noexcept + { return !d || d->isShared(); } + bool needsDetach() const noexcept + { return !d || d->needsDetach(); } + private: Q_NODISCARD_CTOR explicit QSharedPointer(Qt::Initialization) {} @@ -717,6 +730,11 @@ class QWeakPointer size_t owner_hash() const noexcept { return std::hash()(d); } + bool isShared() const noexcept + { return !d || d->isShared(); } + bool needsDetach() const noexcept + { return !d || d->needsDetach(); } + private: friend struct QtPrivate::EnableInternalData; template friend class QSharedPointer;