From 67c3c7a29c9bebf68a367aeb85162248b58675b3 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Fri, 21 Aug 2020 22:48:43 +0200 Subject: [PATCH] Smart pointers: port to explicit operator bool Enough with the restricted bool trick; use the established solution. [ChangeLog][Potentially Source-Incompatible Changes] QScopedPointer, QSharedPointer and QWeakPointer's conversion operator towards bool is now explicit. In some cases this may require an explicit cast towards bool that was not needed before (notably, when returning an object of these types from a function that actually returns bool). Change-Id: I02b89278e75b7e7493ee7e35460504719e00f028 Reviewed-by: Thiago Macieira --- src/corelib/tools/qscopedpointer.h | 12 ++---------- src/corelib/tools/qsharedpointer_impl.h | 6 ++---- src/opengl/qopengltextureblitter.cpp | 2 +- .../platforms/windows/qwindowsdialoghelpers.h | 2 +- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index af0c0ed336..4acec9c1f6 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -95,7 +95,6 @@ typedef QScopedPointerObjectDeleteLater QScopedPointerDeleteLater; template > class QScopedPointer { - typedef T *QScopedPointer:: *RestrictedBool; public: explicit QScopedPointer(T *p = nullptr) noexcept : d(p) { @@ -123,17 +122,10 @@ public: return !d; } -#if defined(Q_QDOC) - inline operator bool() const + explicit operator bool() const { - return isNull() ? nullptr : &QScopedPointer::d; + return !isNull(); } -#else - operator RestrictedBool() const noexcept - { - return isNull() ? nullptr : &QScopedPointer::d; - } -#endif T *data() const noexcept { diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 235cbe144b..35058a1c6e 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -283,7 +283,6 @@ namespace QtSharedPointer { template class QSharedPointer { - typedef T *QSharedPointer:: *RestrictedBool; typedef QtSharedPointer::ExternalRefCountData Data; template using IfCompatible = typename std::enable_if::value, bool>::type; @@ -301,7 +300,7 @@ public: T *data() const noexcept { return value; } T *get() const noexcept { return value; } bool isNull() const noexcept { return !data(); } - operator RestrictedBool() const noexcept { return isNull() ? nullptr : &QSharedPointer::value; } + explicit operator bool() const noexcept { return !isNull(); } bool operator !() const noexcept { return isNull(); } T &operator*() const { return *data(); } T *operator->() const noexcept { return data(); } @@ -539,7 +538,6 @@ public: template class QWeakPointer { - typedef T *QWeakPointer:: *RestrictedBool; typedef QtSharedPointer::ExternalRefCountData Data; template using IfCompatible = typename std::enable_if::value, bool>::type; @@ -554,7 +552,7 @@ public: typedef qptrdiff difference_type; bool isNull() const noexcept { return d == nullptr || d->strongref.loadRelaxed() == 0 || value == nullptr; } - operator RestrictedBool() const noexcept { return isNull() ? nullptr : &QWeakPointer::value; } + explicit operator bool() const noexcept { return !isNull(); } bool operator !() const noexcept { return isNull(); } #if QT_DEPRECATED_SINCE(5, 14) diff --git a/src/opengl/qopengltextureblitter.cpp b/src/opengl/qopengltextureblitter.cpp index b350e8e0e3..0f389547cc 100644 --- a/src/opengl/qopengltextureblitter.cpp +++ b/src/opengl/qopengltextureblitter.cpp @@ -440,7 +440,7 @@ bool QOpenGLTextureBlitter::create() bool QOpenGLTextureBlitter::isCreated() const { Q_D(const QOpenGLTextureBlitter); - return d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram; + return !d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram.isNull(); } /*! diff --git a/src/plugins/platforms/windows/qwindowsdialoghelpers.h b/src/plugins/platforms/windows/qwindowsdialoghelpers.h index 8686749011..55167ad36d 100644 --- a/src/plugins/platforms/windows/qwindowsdialoghelpers.h +++ b/src/plugins/platforms/windows/qwindowsdialoghelpers.h @@ -80,7 +80,7 @@ public: protected: QWindowsDialogHelperBase() = default; QWindowsNativeDialogBase *nativeDialog() const; - inline bool hasNativeDialog() const { return m_nativeDialog; } + inline bool hasNativeDialog() const { return !m_nativeDialog.isNull(); } void timerEvent(QTimerEvent *) override; private: