summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorSérgio Martins <sergio.martins@kdab.com>2017-09-24 20:45:09 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2018-07-21 17:47:35 +0000
commit9229452e574e603aa86fd5adb28da33cf5879db9 (patch)
tree6cd8f36cb60a438377f07519c3e01cf6aad95ba8 /src/corelib
parent89bca67499c6316ef63c9d788c5508ce6a79b27c (diff)
Introduce QScopeGuard
A RAII style class which calls a function at end of scope. Example usage: auto cleanup = qScopeGuard([] { <my cleanup code> ; }); [ChangeLog][QtCore] Introduced QScopeGuard. Task-number: QTBUG-62894 Change-Id: Ife67f5c76255a1fafbae03367263da0bac9a0070 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp67
-rw-r--r--src/corelib/tools/qscopeguard.h96
-rw-r--r--src/corelib/tools/qscopeguard.qdoc55
-rw-r--r--src/corelib/tools/tools.pri1
4 files changed, 219 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp
new file mode 100644
index 0000000000..c8c5f694c6
--- /dev/null
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp
@@ -0,0 +1,67 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+//! [0]
+void myComplexCodeWithMultipleReturnPoints(int v)
+{
+ // The lambda will be executed right before your function returns
+ auto cleanup = qScopeGuard([] { code you want executed goes HERE; });
+
+ if (v == -1)
+ return;
+
+ int v2 = code_that_might_through_exceptions();
+
+ if (v2 == -1)
+ return;
+
+ (...)
+}
+//! [0]
diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h
new file mode 100644
index 0000000000..31100fcabb
--- /dev/null
+++ b/src/corelib/tools/qscopeguard.h
@@ -0,0 +1,96 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSCOPEGUARD_H
+#define QSCOPEGUARD_H
+
+#include <QtCore/qglobal.h>
+
+
+QT_BEGIN_NAMESPACE
+
+
+template <typename F> class QScopeGuard;
+template <typename F> QScopeGuard<F> qScopeGuard(F f);
+
+template <typename F>
+class QScopeGuard
+{
+public:
+ QScopeGuard(QScopeGuard &&other) Q_DECL_NOEXCEPT
+ : m_func(std::move(other.m_func))
+ , m_invoke(other.m_invoke)
+ {
+ other.dismiss();
+ }
+
+ ~QScopeGuard()
+ {
+ if (m_invoke)
+ m_func();
+ }
+
+ void dismiss() Q_DECL_NOEXCEPT
+ {
+ m_invoke = false;
+ }
+
+private:
+ explicit QScopeGuard(F f) Q_DECL_NOEXCEPT
+ : m_func(std::move(f))
+ {
+ }
+
+ Q_DISABLE_COPY(QScopeGuard)
+
+ F m_func;
+ bool m_invoke = true;
+ friend QScopeGuard qScopeGuard<F>(F);
+};
+
+
+template <typename F>
+QScopeGuard<F> qScopeGuard(F f)
+{
+ return QScopeGuard<F>(std::move(f));
+}
+
+QT_END_NAMESPACE
+
+#endif // QSCOPEGUARD_H
diff --git a/src/corelib/tools/qscopeguard.qdoc b/src/corelib/tools/qscopeguard.qdoc
new file mode 100644
index 0000000000..7cbc3e9c7b
--- /dev/null
+++ b/src/corelib/tools/qscopeguard.qdoc
@@ -0,0 +1,55 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qscopeguard.h"
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \fn const QScopeGuard<F> qScopeGuard(F f)
+ \inmodule QtCore
+ \brief The qScopeGuard function can be used to call a function at the end of the scope.
+ \since 5.12
+ \ingroup misc
+
+ QScopeGuard<F> is a class which sole purpose is to run a function F in its destructor.
+ This is useful for guaranteeing your cleanup code is executed whether the function is exited normally,
+ exited early by a return statement, or exited by an exception.
+
+ If F is a lambda then you cannot instantiate the template directly, therefore the qScopeGuard() helper
+ is provided and QScopeGuard<F> is made a private implementation detail.
+
+ Example usage is as follows:
+
+ \snippet code/src_corelib_tools_qscopeguard.cpp 0
+
+ \note Exceptions are not supported. The callable shouldn't throw when executed, copied or moved.
+
+ \sa QScopedValueRollback
+*/
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index 30f7edb5f2..961a0a39ac 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -45,6 +45,7 @@ HEADERS += \
tools/qregexp.h \
tools/qringbuffer_p.h \
tools/qrefcount.h \
+ tools/qscopeguard.h \
tools/qscopedpointer.h \
tools/qscopedpointer_p.h \
tools/qscopedvaluerollback.h \