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.h
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 LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QSCOPEGUARD_H
6#define QSCOPEGUARD_H
7
8#include <QtCore/qglobal.h>
9
10#include <type_traits>
11#include <utility>
12
14
15template <typename F>
17{
18public:
20 explicit QScopeGuard(F &&f) noexcept
21 : m_func(std::move(f))
22 {
23 }
24
26 explicit QScopeGuard(const F &f) noexcept
27 : m_func(f)
28 {
29 }
30
33 : m_func(std::move(other.m_func))
34 , m_invoke(std::exchange(other.m_invoke, false))
35 {
36 }
37
38 ~QScopeGuard() noexcept
39 {
40 if (m_invoke)
41 m_func();
42 }
43
44 void dismiss() noexcept
45 {
46 m_invoke = false;
47 }
48
49private:
50 Q_DISABLE_COPY(QScopeGuard)
51
52 F m_func;
53 bool m_invoke = true;
54};
55
56template <typename F> QScopeGuard(F(&)()) -> QScopeGuard<F(*)()>;
57
59template <typename F>
60[[nodiscard]] QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)
61{
62 return QScopeGuard<typename std::decay<F>::type>(std::forward<F>(f));
63}
64
66
67#endif // QSCOPEGUARD_H
Q_NODISCARD_CTOR QScopeGuard(F &&f) noexcept
Definition qscopeguard.h:20
Q_NODISCARD_CTOR QScopeGuard(const F &f) noexcept
Definition qscopeguard.h:26
~QScopeGuard() noexcept
Definition qscopeguard.h:38
Q_NODISCARD_CTOR QScopeGuard(QScopeGuard &&other) noexcept
Definition qscopeguard.h:32
void dismiss() noexcept
Definition qscopeguard.h:44
Combined button and popup list for selecting options.
#define Q_NODISCARD_CTOR
GLfloat GLfloat f
QScopeGuard(F(&)()) -> QScopeGuard< F(*)()>
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
QSharedPointer< T > other(t)
[5]