Use move more consistently in QScopedValueRollback

Use move on the existing value as well so the constructor makes more
of a difference.

Change-Id: Iee2080da7b7d2d88eb108f0448c61423c7256979
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Allan Sandfeld Jensen 2019-03-25 15:29:17 +01:00
parent 4413f7070a
commit 66e1473096
2 changed files with 20 additions and 7 deletions

View File

@ -48,20 +48,20 @@ template <typename T>
class QScopedValueRollback
{
public:
explicit QScopedValueRollback(T &var) :
varRef(var), oldValue(var)
explicit QScopedValueRollback(T &var)
: varRef(var), oldValue(var)
{
}
explicit QScopedValueRollback(T &var, T value) :
varRef(var), oldValue(var)
explicit QScopedValueRollback(T &var, T value)
: varRef(var), oldValue(std::move(var))
{
varRef = qMove(value);
varRef = std::move(value);
}
~QScopedValueRollback()
{
varRef = qMove(oldValue);
varRef = std::move(oldValue);
}
void commit()
@ -70,7 +70,7 @@ public:
}
private:
T& varRef;
T &varRef;
T oldValue;
Q_DISABLE_COPY(QScopedValueRollback)

View File

@ -46,6 +46,7 @@ private Q_SLOTS:
void rollbackToPreviousCommit();
void exceptions();
void earlyExitScope();
void moveOnly();
private:
void earlyExitScope_helper(int exitpoint, int &member);
};
@ -190,5 +191,17 @@ void tst_QScopedValueRollback::earlyExitScope_helper(int exitpoint, int& member)
r.commit();
}
void tst_QScopedValueRollback::moveOnly()
{
std::unique_ptr<int> uniquePtr;
std::unique_ptr<int> newVal(new int(5));
QVERIFY(!uniquePtr);
{
QScopedValueRollback<std::unique_ptr<int>> r(uniquePtr, std::move(newVal));
QVERIFY(uniquePtr);
}
QVERIFY(!uniquePtr);
}
QTEST_MAIN(tst_QScopedValueRollback)
#include "tst_qscopedvaluerollback.moc"