From 77982c482323116c59949147291fe4f8ac901f5d Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 14 May 2014 10:52:37 +0200 Subject: 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 Reviewed-by: Thiago Macieira --- src/corelib/tools/qscopedvaluerollback.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/corelib/tools/qscopedvaluerollback.h') 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; -- cgit v1.2.3