summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@digia.com>2014-05-14 10:52:37 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-05-21 02:15:10 +0200
commit77982c482323116c59949147291fe4f8ac901f5d (patch)
tree2219212bb348996bd2f99cb387237e6151c4099f /src
parentaefa611f0ceac2a945cc7df15683c5072e21153f (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qscopedvaluerollback.cpp11
-rw-r--r--src/corelib/tools/qscopedvaluerollback.h9
2 files changed, 18 insertions, 2 deletions
diff --git a/src/corelib/tools/qscopedvaluerollback.cpp b/src/corelib/tools/qscopedvaluerollback.cpp
index 3201a3c87a..f37a232985 100644
--- a/src/corelib/tools/qscopedvaluerollback.cpp
+++ b/src/corelib/tools/qscopedvaluerollback.cpp
@@ -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.
@@ -70,6 +70,15 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \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()
Assigns the previous value to the managed variable.
diff --git a/src/corelib/tools/qscopedvaluerollback.h b/src/corelib/tools/qscopedvaluerollback.h
index dfaf1984be..2a5bd8c223 100644
--- a/src/corelib/tools/qscopedvaluerollback.h
+++ b/src/corelib/tools/qscopedvaluerollback.h
@@ -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;