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 QQMLVME_P_H
5#define QQMLVME_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 <private/qrecursionwatcher_p.h>
19
20#include <QtCore/QStack>
21#include <QtCore/QString>
22#include <QtCore/qelapsedtimer.h>
23#include <QtCore/qdeadlinetimer.h>
24#include <QtCore/qcoreapplication.h>
25#include <QtCore/qtypeinfo.h>
26
27#include <private/qqmlengine_p.h>
28#include <private/qfinitestack_p.h>
29
30#include <atomic>
31
32QT_BEGIN_NAMESPACE
33
34class QObject;
35
36class QQmlInstantiationInterrupt {
37public:
38 inline QQmlInstantiationInterrupt();
39 inline QQmlInstantiationInterrupt(std::atomic<bool> *runWhile,
40 QDeadlineTimer deadline = QDeadlineTimer::Forever);
41 inline QQmlInstantiationInterrupt(QDeadlineTimer deadline);
42
43 inline bool shouldInterrupt() const;
44private:
45 enum Mode { None, Time, Flag };
46 Mode mode;
47 QDeadlineTimer deadline;
48 std::atomic<bool> *runWhile = nullptr;
49};
50
51class Q_QML_PRIVATE_EXPORT QQmlVME
52{
53public:
54 static void enableComponentComplete();
55 static void disableComponentComplete();
56 static bool componentCompleteEnabled();
57
58private:
59 static bool s_enableComponentComplete;
60};
61
62// Used to check that a QQmlVME that is interrupted mid-execution
63// is still valid. Checks all the objects and contexts have not been
64// deleted.
65//
66// VME stands for Virtual Machine Execution. QML files used to
67// be compiled to a byte code data structure that a virtual machine executed
68// (for constructing the tree of QObjects and setting properties).
69class QQmlVMEGuard
70{
71public:
72 QQmlVMEGuard();
73 ~QQmlVMEGuard();
74
75 void guard(QQmlObjectCreator *);
76 void clear();
77
78 bool isOK() const;
79
80private:
81 int m_objectCount;
82 QQmlGuard<QObject> *m_objects;
83 int m_contextCount;
84 QQmlGuardedContextData *m_contexts;
85};
86
87QQmlInstantiationInterrupt::QQmlInstantiationInterrupt()
88 : mode(None)
89{
90}
91
92QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(std::atomic<bool> *runWhile, QDeadlineTimer deadline)
93 : mode(Flag), deadline(deadline), runWhile(runWhile)
94{
95}
96
97QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(QDeadlineTimer deadline)
98 : mode(Time), deadline(deadline)
99{
100}
101
102bool QQmlInstantiationInterrupt::shouldInterrupt() const
103{
104 switch (mode) {
105 case None:
106 return false;
107 case Time:
108 return deadline.hasExpired();
109 case Flag:
110 return !runWhile->load(m: std::memory_order_acquire) || deadline.hasExpired();
111 }
112 Q_UNREACHABLE_RETURN(false);
113}
114
115QT_END_NAMESPACE
116
117#endif // QQMLVME_P_H
118

source code of qtdeclarative/src/qml/qml/qqmlvme_p.h