Deprecate QWeakPointer::data()

It's a dangerous API to have. Upgrade to a shared pointer
if accessing the raw pointer is required.

[ChangeLog][QtCore][QWeakPointer] The data() function has
been deprecated.

Change-Id: Ie5d34f4fb500b3cfa14d2c0b1b08484df072129c
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Giuseppe D'Angelo 2019-05-10 20:59:52 +02:00
parent 5497183c71
commit b7a7d61efc
3 changed files with 6 additions and 7 deletions

View File

@ -83,7 +83,7 @@ public:
{ wp.assign(static_cast<QObjectType*>(p)); return *this; }
inline T* data() const
{ return static_cast<T*>( wp.data()); }
{ return static_cast<T*>(wp.d == nullptr || wp.d->strongref.load() == 0 ? nullptr : wp.value); }
inline T* operator->() const
{ return data(); }
inline T& operator*() const

View File

@ -343,12 +343,6 @@
if you obtain a non-null object, you may use the pointer. See
QWeakPointer::toStrongRef() for an example.
QWeakPointer also provides the QWeakPointer::data() method that returns
the tracked pointer without ensuring that it remains valid. This function
is provided if you can guarantee by external means that the object will
not get deleted (or if you only need the pointer value) and the cost of
creating a QSharedPointer using toStrongRef() is too high.
\omit
\section1 QWeakPointer internals
@ -853,6 +847,7 @@
/*!
\fn template <class T> T *QWeakPointer<T>::data() const
\since 4.6
\obsolete Use toStrongRef() instead, and data() on the returned QSharedPointer.
Returns the value of the pointer being tracked by this QWeakPointer,
\b without ensuring that it cannot get deleted. To have that guarantee,

View File

@ -565,7 +565,11 @@ public:
bool isNull() const noexcept { return d == nullptr || d->strongref.load() == 0 || value == nullptr; }
operator RestrictedBool() const noexcept { return isNull() ? nullptr : &QWeakPointer::value; }
bool operator !() const noexcept { return isNull(); }
#if QT_DEPRECATED_SINCE(5, 14)
QT_DEPRECATED_X("Use toStrongRef() instead, and data() on the returned QSharedPointer")
T *data() const noexcept { return d == nullptr || d->strongref.load() == 0 ? nullptr : value; }
#endif
inline QWeakPointer() noexcept : d(nullptr), value(nullptr) { }
inline ~QWeakPointer() { if (d && !d->weakref.deref()) delete d; }