Deprecate QScopedPointer::take()

We've decided that QScopedPointer shouldn't be movable,
because it would break the semantics of being "scoped"
(the pointer/pointee won't survive the scope).

Then, QScopedPointer shouldn't allow for take() either.
If you need those semantics, reach for unique_ptr.

[ChangeLog][QtCore][QScopedPointer] The take() function
has been deprecated. This was an API mistake, as it
allowed the pointer/pointee to escape from the scope,
defeating the point of the QScopedPointer class. If you
need such semantics, use std::unique_ptr (and call
release()).

Change-Id: I3236f085f763b04eb98e3242abc06f7c54fb3d8b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2020-11-01 16:53:26 +01:00
parent 5dd7e7b7a8
commit 612a01be65
5 changed files with 16 additions and 9 deletions

View File

@ -63,6 +63,7 @@
#endif
#include <algorithm>
#include <memory>
#include <stdlib.h>
QT_BEGIN_NAMESPACE
@ -1022,12 +1023,12 @@ bool QDir::cd(const QString &dirName)
}
}
QScopedPointer<QDirPrivate> dir(new QDirPrivate(*d_ptr.constData()));
std::unique_ptr<QDirPrivate> dir(new QDirPrivate(*d_ptr.constData()));
dir->setPath(newPath);
if (!dir->exists())
return false;
d_ptr = dir.take();
d_ptr = dir.release();
return true;
}
@ -1730,7 +1731,7 @@ bool QDir::isRelative() const
bool QDir::makeAbsolute()
{
const QDirPrivate *d = d_ptr.constData();
QScopedPointer<QDirPrivate> dir;
std::unique_ptr<QDirPrivate> dir;
if (!!d->fileEngine) {
QString absolutePath = d->fileEngine->fileName(QAbstractFileEngine::AbsoluteName);
if (QDir::isRelativePath(absolutePath))
@ -1743,7 +1744,7 @@ bool QDir::makeAbsolute()
dir.reset(new QDirPrivate(*d_ptr.constData()));
dir->setPath(d->absoluteDirEntry.filePath());
}
d_ptr = dir.take(); // actually detach
d_ptr = dir.release(); // actually detach
return true;
}

View File

@ -251,6 +251,8 @@ QT_BEGIN_NAMESPACE
/*!
\fn template <typename T, typename Cleanup> T *QScopedPointer<T, Cleanup>::take()
\obsolete Use std::unique_ptr and release() instead.
Returns the value of the pointer referenced by this object. The pointer of this
QScopedPointer object will be reset to \nullptr.

View File

@ -151,12 +151,15 @@ public:
Cleanup::cleanup(oldD);
}
#if QT_DEPRECATED_SINCE(6, 1)
QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().")
T *take() noexcept
{
T *oldD = d;
d = nullptr;
return oldD;
}
#endif
void swap(QScopedPointer<T, Cleanup> &other) noexcept
{

View File

@ -40,7 +40,7 @@
#include "qdbusargument_p.h"
#include "qdbusconnection.h"
#include <qscopedpointer.h>
#include <memory>
#include <stdlib.h>
@ -426,12 +426,12 @@ QDBusDemarshaller *QDBusDemarshaller::endCommon()
QDBusArgument QDBusDemarshaller::duplicate()
{
QScopedPointer<QDBusDemarshaller> d(new QDBusDemarshaller(capabilities));
std::unique_ptr<QDBusDemarshaller> d(new QDBusDemarshaller(capabilities));
d->iterator = iterator;
d->message = q_dbus_message_ref(message);
q_dbus_message_iter_next(&iterator);
return QDBusArgumentPrivate::create(d.take());
return QDBusArgumentPrivate::create(d.release());
}
QT_END_NAMESPACE

View File

@ -62,6 +62,7 @@
#include <stdio.h>
#include <limits>
#include <memory>
QT_BEGIN_NAMESPACE
@ -2554,7 +2555,7 @@ QDomNamedNodeMapPrivate::~QDomNamedNodeMapPrivate()
QDomNamedNodeMapPrivate* QDomNamedNodeMapPrivate::clone(QDomNodePrivate* p)
{
QScopedPointer<QDomNamedNodeMapPrivate> m(new QDomNamedNodeMapPrivate(p));
std::unique_ptr<QDomNamedNodeMapPrivate> m(new QDomNamedNodeMapPrivate(p));
m->readonly = readonly;
m->appendToParent = appendToParent;
@ -2567,7 +2568,7 @@ QDomNamedNodeMapPrivate* QDomNamedNodeMapPrivate::clone(QDomNodePrivate* p)
// we are no longer interested in ownership
m->ref.deref();
return m.take();
return m.release();
}
void QDomNamedNodeMapPrivate::clearMap()