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>
This commit is contained in:
Sérgio Martins 2017-09-24 20:45:09 +01:00 committed by Simon Hausmann
parent 89bca67499
commit 9229452e57
6 changed files with 308 additions and 0 deletions

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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 \

View File

@ -0,0 +1,4 @@
CONFIG += testcase
TARGET = tst_qscopeguard
QT = core testlib
SOURCES = tst_qscopeguard.cpp

View File

@ -0,0 +1,85 @@
/****************************************************************************
**
** 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 test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtCore/QScopeGuard>
/*!
\class tst_QScopedGuard
\internal
\since 5.11
\brief Tests class QScopedCleanup and function qScopeGuard
*/
class tst_QScopedGuard : public QObject
{
Q_OBJECT
private Q_SLOTS:
void leavingScope();
void exceptions();
};
static int s_globalState = 0;
void tst_QScopedGuard::leavingScope()
{
auto cleanup = qScopeGuard([] { s_globalState++; QCOMPARE(s_globalState, 3); });
QCOMPARE(s_globalState, 0);
{
auto cleanup = qScopeGuard([] { s_globalState++; });
QCOMPARE(s_globalState, 0);
}
QCOMPARE(s_globalState, 1);
s_globalState++;
}
void tst_QScopedGuard::exceptions()
{
s_globalState = 0;
bool caught = false;
QT_TRY
{
auto cleanup = qScopeGuard([&caught] { s_globalState++; });
QT_THROW(std::bad_alloc()); //if Qt compiled without exceptions this is noop
s_globalState = 100;
}
QT_CATCH(...)
{
caught = true;
QCOMPARE(s_globalState, 1);
}
QVERIFY((caught && s_globalState == 1) || (!caught && s_globalState == 101));
}
QTEST_MAIN(tst_QScopedGuard)
#include "tst_qscopeguard.moc"