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