QScopedPointer: deprecate swap

Follow up of 612a01be6513894ab1ec5a36b699a2142ba7f35c
(deprecating QSP::take()): for the same reasons, swap()
functions do not belong to QScopedPointer, or they would
allow the pointer to escape:

  QScopedPointer a;
  {
    QScopedPointer b = ~~~;
    qSwap(a, b);
  }
  // b's pointer escaped its scope

Deprecate them as well.

[ChangeLog][QtCore][QScopedPointer] QScopedPointer swapping
functions have been deprecated, as they would allow the
managed pointer to escape the scope. If you need those semantics,
use std::unique_ptr instead.

Change-Id: I2b0938b62f2ef5a3561f61f595a3fb4c505a8f08
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2021-02-25 12:49:21 +01:00
parent 712117f8b8
commit fe9d7bf759
2 changed files with 21 additions and 1 deletions

View File

@ -267,6 +267,10 @@ QT_BEGIN_NAMESPACE
*/ */
/*! \fn template <typename T, typename Cleanup> void QScopedPointer<T, Cleanup>::swap(QScopedPointer<T, Cleanup> &lhs, QScopedPointer<T, Cleanup> &rhs) /*! \fn template <typename T, typename Cleanup> void QScopedPointer<T, Cleanup>::swap(QScopedPointer<T, Cleanup> &lhs, QScopedPointer<T, Cleanup> &rhs)
\obsolete Use std::unique_ptr instead; this function may let a pointer
escape its scope.
Swaps \a lhs with \a rhs. Swaps \a lhs with \a rhs.
*/ */
@ -333,7 +337,11 @@ QT_BEGIN_NAMESPACE
*/ */
/*! \fn template <typename T, typename Cleanup> void QScopedArrayPointer<T, Cleanup>::swap(QScopedArrayPointer<T, Cleanup> &other) /*! \fn template <typename T, typename Cleanup> void QScopedArrayPointer<T, Cleanup>::swap(QScopedArrayPointer<T, Cleanup> &other)
Swap this pointer with \a other.
\obsolete Use std::unique_ptr instead; this function may let a pointer
escape its scope.
Swap this pointer with \a other.
*/ */
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -169,10 +169,13 @@ public:
} }
#endif #endif
#if QT_DEPRECATED_SINCE(6, 2)
QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
void swap(QScopedPointer<T, Cleanup> &other) noexcept void swap(QScopedPointer<T, Cleanup> &other) noexcept
{ {
qSwap(d, other.d); qSwap(d, other.d);
} }
#endif
typedef T *pointer; typedef T *pointer;
@ -206,8 +209,11 @@ public:
return !rhs.isNull(); return !rhs.isNull();
} }
#if QT_DEPRECATED_SINCE(6, 2)
QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
friend void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) noexcept friend void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) noexcept
{ p1.swap(p2); } { p1.swap(p2); }
#endif
protected: protected:
T *d; T *d;
@ -240,8 +246,11 @@ public:
return this->d[i]; return this->d[i];
} }
#if QT_DEPRECATED_SINCE(6, 2)
QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
void swap(QScopedArrayPointer &other) noexcept // prevent QScopedPointer <->QScopedArrayPointer swaps void swap(QScopedArrayPointer &other) noexcept // prevent QScopedPointer <->QScopedArrayPointer swaps
{ QScopedPointer<T, Cleanup>::swap(other); } { QScopedPointer<T, Cleanup>::swap(other); }
#endif
private: private:
explicit inline QScopedArrayPointer(void *) explicit inline QScopedArrayPointer(void *)
@ -259,9 +268,12 @@ private:
Q_DISABLE_COPY(QScopedArrayPointer) Q_DISABLE_COPY(QScopedArrayPointer)
}; };
#if QT_DEPRECATED_SINCE(6, 2)
template <typename T, typename Cleanup> template <typename T, typename Cleanup>
QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) noexcept inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) noexcept
{ lhs.swap(rhs); } { lhs.swap(rhs); }
#endif
QT_END_NAMESPACE QT_END_NAMESPACE