Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qscopeguard.qdoc
Go to the documentation of this file.
1// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5#include "qscopeguard.h"
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \class QScopeGuard
11 \since 5.12
12 \inmodule QtCore
13 \brief Provides a scope guard for calling a function at the end of
14 a scope.
15
16 QScopeGuard<F> is a class of which the sole purpose is to run the function
17 \a f in its destructor. This is useful for guaranteeing
18 your cleanup code is executed, whether the function is exited normally,
19 exited early by a return statement, or exited by an exception.
20
21 \note Exceptions are not supported. The callable shouldn't throw when
22 executed, copied or moved.
23
24 \sa QScopedValueRollback
25*/
26
27/*!
28 \fn template <typename F> QScopeGuard<F>::QScopeGuard(F &&f)
29 \fn template <typename F> QScopeGuard<F>::QScopeGuard(const F &f)
30
31 Create a scope guard that will execute \a f at the end of the scope.
32
33 If \e F is a lambda, its type cannot be written. In that case you need to
34 either rely on class template argument deduction (C++17 feature) and leave
35 the template parameter out completely or use the helper function
36 qScopeGuard() instead of this constructor.
37
38 \since 5.15
39*/
40
41/*! \fn template <typename F> void QScopeGuard<F>::dismiss()
42
43 Disarms the scope guard, so that the function \e F will not be called at
44 the end of the scope.
45*/
46
47/*!
48 \fn [qScopeGuard] template <typename F> QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)
49 \inmodule QtCore
50 \relates QScopeGuard
51 \brief The qScopeGuard function can be used to call a function at the end
52 of the scope.
53 \ingroup misc
54
55 Create a scope guard that will execute \a f at the end of the scope.
56
57 This helper function is provided so that you can easily construct a
58 QScopeGuard without having to name the template parameter for the type of
59 the callable. If \e F is a lambda then you cannot write its type and relying
60 on this helper or class template argument deduction is necessary.
61
62 Example usage is as follows:
63
64 \snippet code/src_corelib_tools_qscopeguard.cpp 0
65
66*/
67
68QT_END_NAMESPACE