summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2013-07-27 00:58:27 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-07-28 23:09:10 +0200
commit5b9006bbdba7dcab01b8e640554a7d7a4b64f76b (patch)
tree9fac766b361f86514184382ac05e13be95291f5f /src
parent69b31f7b657a7ca611ad980c2974597de160598c (diff)
Implement move-ctor and move-assignment-op for QScopedPointer
It makes sense for a QScopedPointer to be movable, for instance for allowing instances to be returned from a function. Ownwership of the managed pointer is still tied to one (and one only) QScopedPointer instance. Moreover, a move assignment operator makes sense as well, as it implementing the equivalent of this->reset(other.take()); only when other is a rvalue and not a lvalue (so either it's a temporary or it's getting explicitly moved in with std::move). This makes QScopedPointer API's a bit closer to std::unique_ptr's one. Task-number: QTBUG-29754 Change-Id: If1ac0c688327a67af4ad5b7ad45b439b022ed1c6 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qscopedpointer.cpp27
-rw-r--r--src/corelib/tools/qscopedpointer.h13
2 files changed, 40 insertions, 0 deletions
diff --git a/src/corelib/tools/qscopedpointer.cpp b/src/corelib/tools/qscopedpointer.cpp
index 0848754de5..cf2f9bee0c 100644
--- a/src/corelib/tools/qscopedpointer.cpp
+++ b/src/corelib/tools/qscopedpointer.cpp
@@ -130,6 +130,33 @@ QT_BEGIN_NAMESPACE
Constructs this QScopedPointer instance and sets its pointer to \a p.
*/
+#ifndef Q_QDOC // QTBUG-32675, qdoc can't parse rvalue refs
+/*!
+ \fn QScopedPointer::QScopedPointer(QScopedPointer<T, Cleanup> &&other)
+
+ Move-constructs a QScopedPointer instance, making it point at the same
+ object that \a other was pointing to. \a other is reset to point to \c{NULL}.
+
+ \since 5.2
+*/
+
+/*!
+ \fn QScopedPointer<T, Cleanup> &operator=(QScopedPointer<T, Cleanup> &&other)
+
+ Move-assigns \a other to this QScopedPointer instance, transferring the
+ ownership of the managed pointer to this instance.
+
+ If \a other and this instance are actually the same object, this operator
+ does nothing.
+
+ Otherwise, this instance is set to point to the object \a other
+ is pointing to, and \a other is set to point to \c{NULL}.
+ If this instance was pointing to an object, that object is destroyed,
+
+ \since 5.2
+*/
+#endif
+
/*!
\fn QScopedPointer::~QScopedPointer()
diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h
index 2155c56e27..c4a148c4e4 100644
--- a/src/corelib/tools/qscopedpointer.h
+++ b/src/corelib/tools/qscopedpointer.h
@@ -98,6 +98,19 @@ public:
Cleanup::cleanup(oldD);
}
+#ifdef Q_COMPILER_RVALUE_REFS
+ inline QScopedPointer(QScopedPointer<T, Cleanup> &&other)
+ : d(other.take())
+ {
+ }
+
+ inline QScopedPointer<T, Cleanup> &operator=(QScopedPointer<T, Cleanup> &&other)
+ {
+ reset(other.take());
+ return *this;
+ }
+#endif
+
inline T &operator*() const
{
Q_ASSERT(d);