Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended QSharedPointer and QWeakPointer with isShared and needsDetach #112

Open
wants to merge 1 commit into
base: 6.7.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/corelib/tools/qsharedpointer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -463,6 +471,11 @@ template <class T> class QSharedPointer
size_t owner_hash() const noexcept
{ return std::hash<Data *>()(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) {}
Expand Down Expand Up @@ -717,6 +730,11 @@ class QWeakPointer
size_t owner_hash() const noexcept
{ return std::hash<Data *>()(d); }

bool isShared() const noexcept
{ return !d || d->isShared(); }
bool needsDetach() const noexcept
{ return !d || d->needsDetach(); }

private:
friend struct QtPrivate::EnableInternalData;
template <class X> friend class QSharedPointer;
Expand Down