1// Copyright (C) 2020 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 QQMLGUARDEDCONTEXTDATA_P_H
5#define QQMLGUARDEDCONTEXTDATA_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 <QtQml/private/qtqmlglobal_p.h>
19#include <QtQml/private/qqmlcontextdata_p.h>
20
21QT_BEGIN_NAMESPACE
22
23class QQmlGuardedContextData
24{
25 Q_DISABLE_COPY(QQmlGuardedContextData);
26public:
27 QQmlGuardedContextData() = default;
28 ~QQmlGuardedContextData() { unlink(); }
29
30 QQmlGuardedContextData(QQmlGuardedContextData &&) = default;
31 QQmlGuardedContextData &operator=(QQmlGuardedContextData &&) = default;
32
33 QQmlGuardedContextData(QQmlRefPointer<QQmlContextData> data)
34 {
35 setContextData(std::move(data));
36 }
37
38 QQmlGuardedContextData &operator=(QQmlRefPointer<QQmlContextData> d)
39 {
40 setContextData(std::move(d));
41 return *this;
42 }
43
44 QQmlRefPointer<QQmlContextData> contextData() const { return m_contextData; }
45 void setContextData(QQmlRefPointer<QQmlContextData> contextData)
46 {
47 if (m_contextData.data() == contextData.data())
48 return;
49 unlink();
50
51 if (contextData) {
52 m_contextData = std::move(contextData);
53 m_next = m_contextData->m_contextGuards;
54 if (m_next)
55 m_next->m_prev = &m_next;
56
57 m_contextData->m_contextGuards = this;
58 m_prev = &m_contextData->m_contextGuards;
59 }
60 }
61
62 bool isNull() const { return !m_contextData; }
63
64 operator const QQmlRefPointer<QQmlContextData> &() const { return m_contextData; }
65 QQmlContextData &operator*() const { return m_contextData.operator*(); }
66 QQmlContextData *operator->() const { return m_contextData.operator->(); }
67
68 QQmlGuardedContextData *next() const { return m_next; }
69
70private:
71 void reset()
72 {
73 m_contextData.reset();
74 m_next = nullptr;
75 m_prev = nullptr;
76 }
77
78 void unlink()
79 {
80 if (m_prev) {
81 *m_prev = m_next;
82 if (m_next)
83 m_next->m_prev = m_prev;
84 reset();
85 }
86 }
87
88 QQmlRefPointer<QQmlContextData> m_contextData;
89 QQmlGuardedContextData *m_next = nullptr;
90 QQmlGuardedContextData **m_prev = nullptr;
91};
92
93QT_END_NAMESPACE
94
95#endif // QQMLGUARDEDCONTEXTDATA_P_H
96

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