blob: eef9a360936aa709e2031f37145b09e0d128eeda (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <utility>
#include <QObject>
namespace Utils {
/*!
guardedCallback is a helper function that creates a callback that will call the supplied
callback only if the guard QObject is still alive. It will also delete its copy of the
callback when the guard object is destroyed.
\param guardObject The QObject used to guard the callback.
\param method The callback to call if the guardObject is still alive.
*/
template<class F>
auto guardedCallback(QObject *guard, const F &method)
{
struct Guardian
{
Guardian(QObject *guard, const F &f) : func(f)
{
conn = QObject::connect(guard, &QObject::destroyed, [this] { func.reset(); });
}
~Guardian()
{
QObject::disconnect(conn);
}
std::optional<F> func;
QMetaObject::Connection conn;
};
return [guard = Guardian(guard, method)](auto &&...args) {
if (guard.func)
(*guard.func)(std::forward<decltype(args)>(args)...);
};
}
} // namespace Utils
|