1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QANIMATIONJOBUTIL_P_H
5#define QANIMATIONJOBUTIL_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qcompilerdetection.h>
19#include <QtCore/qtconfigmacros.h>
20
21#include <type_traits>
22
23QT_REQUIRE_CONFIG(qml_animation);
24
25#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU_ONLY >= 1300
26# define ACTION_IF_DISABLE_DANGLING_POINTER_WARNING QT_WARNING_DISABLE_GCC("-Wdangling-pointer")
27#else
28# define ACTION_IF_DISABLE_DANGLING_POINTER_WARNING
29#endif
30
31// SelfDeletable is used for self-destruction detection along with
32// ACTION_IF_DELETED and RETURN_IF_DELETED macros. While using, the objects
33// under test should have a member m_selfDeletable of type SelfDeletable
34struct SelfDeletable {
35 ~SelfDeletable() {
36 if (m_wasDeleted)
37 *m_wasDeleted = true;
38 }
39 bool *m_wasDeleted = nullptr;
40};
41
42// \param p pointer to object under test, which should have a member m_selfDeletable of type SelfDeletable
43// \param func statements or functions that to be executed under test.
44// \param action post process if p was deleted under test.
45#define ACTION_IF_DELETED(p, func, action) \
46do { \
47 QT_WARNING_PUSH \
48 ACTION_IF_DISABLE_DANGLING_POINTER_WARNING \
49 static_assert(std::is_same<decltype((p)->m_selfDeletable), SelfDeletable>::value, "m_selfDeletable must be SelfDeletable");\
50 bool *prevWasDeleted = (p)->m_selfDeletable.m_wasDeleted; \
51 bool wasDeleted = false; \
52 (p)->m_selfDeletable.m_wasDeleted = &wasDeleted; \
53 {func;} \
54 if (wasDeleted) { \
55 if (prevWasDeleted) \
56 *prevWasDeleted = true; \
57 {action;} \
58 } \
59 (p)->m_selfDeletable.m_wasDeleted = prevWasDeleted; \
60 QT_WARNING_POP \
61} while (false)
62
63#define RETURN_IF_DELETED(func) \
64ACTION_IF_DELETED(this, func, return)
65
66#endif // QANIMATIONJOBUTIL_P_H
67

source code of qtdeclarative/src/qml/animations/qanimationjobutil_p.h