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 QQMLCONTEXT_P_H
5#define QQMLCONTEXT_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/qlist.h>
19#include <QtCore/qstring.h>
20#include <QtCore/qvariant.h>
21#include <QtCore/qpointer.h>
22#include <QtQml/qqmlcontext.h>
23#include <QtQml/qqmllist.h>
24
25#include <QtCore/private/qobject_p.h>
26#include <QtCore/qtaggedpointer.h>
27#include <QtQml/private/qqmlrefcount_p.h>
28
29QT_BEGIN_NAMESPACE
30
31class QQmlContextData;
32
33class QQmlContextPrivate : public QObjectPrivate
34{
35 Q_DECLARE_PUBLIC(QQmlContext)
36public:
37 static QQmlContextPrivate *get(QQmlContext *context) {
38 return static_cast<QQmlContextPrivate *>(QObjectPrivate::get(o: context));
39 }
40
41 static QQmlContext *get(QQmlContextPrivate *context) {
42 return static_cast<QQmlContext *>(context->q_func());
43 }
44
45 static qsizetype context_count(QQmlListProperty<QObject> *);
46 static QObject *context_at(QQmlListProperty<QObject> *, qsizetype);
47
48 void dropDestroyedQObject(const QString &name, QObject *destroyed);
49
50 int notifyIndex() const { return m_notifyIndex; }
51 void setNotifyIndex(int notifyIndex) { m_notifyIndex = notifyIndex; }
52
53 int numPropertyValues() const {
54 auto size = m_propertyValues.size();
55 Q_ASSERT(size <= std::numeric_limits<int>::max());
56 return int(size);
57 }
58 void appendPropertyValue(const QVariant &value) { m_propertyValues.append(t: value); }
59 void setPropertyValue(int index, const QVariant &value) { m_propertyValues[index] = value; }
60 QVariant propertyValue(int index) const { return m_propertyValues[index]; }
61
62 QList<QPointer<QObject>> instances() const { return m_instances; }
63 void appendInstance(QObject *instance) { m_instances.append(t: instance); }
64 void cleanInstances()
65 {
66 for (auto it = m_instances.begin(); it != m_instances.end();
67 it->isNull() ? (it = m_instances.erase(pos: it)) : ++it) {}
68 }
69
70 void emitDestruction();
71
72private:
73 friend class QQmlContextData;
74
75 QQmlContextPrivate(QQmlContextData *data) : m_data(data) {}
76 QQmlContextPrivate(QQmlContext *publicContext, const QQmlRefPointer<QQmlContextData> &parent,
77 QQmlEngine *engine = nullptr);
78
79 // Intentionally a bare pointer. QQmlContextData knows whether it owns QQmlContext or vice
80 // versa. If QQmlContext is the owner, QQmlContextData keeps an extra ref for its publicContext.
81 // The publicContext ref is released when doing QQmlContextData::setPublicContext(nullptr).
82 QQmlContextData *m_data;
83
84 QList<QVariant> m_propertyValues;
85 int m_notifyIndex = -1;
86
87 // Only used for debugging
88 QList<QPointer<QObject>> m_instances;
89};
90
91QT_END_NAMESPACE
92
93#endif // QQMLCONTEXT_P_H
94

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