qscopedvaluerollback: add convenience constructor
It's a common need to assign a variable to something when entering a code block, and then revert it upon exit. qscopedvaluerollback can be used for this. But as a convenience, this patch adds an extra constructor so that you can "protect" and set a variable in one go instead of using two lines. Change-Id: If4b89d3a5ba32ef2304bda058b1b6050932612ed Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
aefa611f0c
commit
77982c4823
@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the QtCore module of the Qt Toolkit.
|
||||
@ -69,6 +69,15 @@ QT_BEGIN_NAMESPACE
|
||||
Stores the previous value of \a var internally, for revert on destruction.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QScopedValueRollback::QScopedValueRollback(T &var, T value)
|
||||
|
||||
Assigns \a value to \ var and stores the previous value of \a var
|
||||
internally, for revert on destruction.
|
||||
|
||||
\since 5.4
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QScopedValueRollback::~QScopedValueRollback()
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the QtCore module of the Qt Toolkit.
|
||||
@ -56,6 +56,13 @@ public:
|
||||
oldValue = varRef;
|
||||
}
|
||||
|
||||
explicit QScopedValueRollback(T &var, T value) :
|
||||
varRef(var)
|
||||
{
|
||||
oldValue = varRef;
|
||||
varRef = value;
|
||||
}
|
||||
|
||||
~QScopedValueRollback()
|
||||
{
|
||||
varRef = oldValue;
|
||||
|
@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
@ -67,14 +67,17 @@ void tst_QScopedValueRollback::leavingScope()
|
||||
{
|
||||
int i = 0;
|
||||
bool b = false;
|
||||
bool b2 = false;
|
||||
QString s("This is useful");
|
||||
|
||||
//test rollback on going out of scope
|
||||
{
|
||||
QScopedValueRollback<int> ri(i);
|
||||
QScopedValueRollback<bool> rb(b);
|
||||
QScopedValueRollback<bool> rb2(b2, true);
|
||||
QScopedValueRollback<QString> rs(s);
|
||||
QCOMPARE(b, false);
|
||||
QCOMPARE(b2, true);
|
||||
QCOMPARE(i, 0);
|
||||
QCOMPARE(s, QString("This is useful"));
|
||||
b = true;
|
||||
@ -85,6 +88,7 @@ void tst_QScopedValueRollback::leavingScope()
|
||||
QCOMPARE(s, QString("Useless"));
|
||||
}
|
||||
QCOMPARE(b, false);
|
||||
QCOMPARE(b2, false);
|
||||
QCOMPARE(i, 0);
|
||||
QCOMPARE(s, QString("This is useful"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user