Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
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 A scope guard can be disabled using dismiss(), in which case the function
22 is not run at all, or executed ahead of destruction, using commit().
23
24 \note Exceptions are not supported. The callable shouldn't throw when
25 executed, copied or moved.
26
27 \sa QScopedValueRollback
28*/
29
30/*!
31 \fn template <typename F> QScopeGuard<F>::QScopeGuard(F &&f)
32 \fn template <typename F> QScopeGuard<F>::QScopeGuard(const F &f)
33
34 Creates a scope guard that will execute \a f at the end of the scope.
35
36 If \e F is a lambda, its type cannot be written. In that case you need to
37 either rely on class template argument deduction (C++17 feature) and leave
38 the template parameter out completely or use the helper function
39 qScopeGuard() instead of this constructor.
40
41 \since 5.15
42*/
43
44/*! \fn template <typename F> void QScopeGuard<F>::dismiss()
45
46 Disarms the scope guard, so that the function \e F will not be called at
47 the end of the scope.
48
49 \sa commit()
50*/
51
52/*!
53 \since 6.11
54 \fn template <typename F> void QScopeGuard<F>::commit()
55
56 Calls the function \e F and then disarms the scope guard. The guard must be
57 armed (not been \l{dismiss()}ed) when calling this function.
58
59 \sa dismiss()
60*/
61
62/*!
63 \fn [qScopeGuard] template <typename F> QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)
64 \inmodule QtCore
65 \relates QScopeGuard
66 \brief The qScopeGuard function can be used to call a function at the end
67 of the scope.
68 \ingroup misc
69
70 Create a scope guard that will execute \a f at the end of the scope.
71
72 This helper function is provided so that you can easily construct a
73 QScopeGuard without having to name the template parameter for the type of
74 the callable. If \e F is a lambda then you cannot write its type and relying
75 on this helper or class template argument deduction is necessary.
76
77 Example usage is as follows:
78
79 \snippet code/src_corelib_tools_qscopeguard.cpp 0
80
81*/
82
83QT_END_NAMESPACE