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#ifndef QV4PERSISTENT_H
4#define QV4PERSISTENT_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include "qv4value_p.h"
18#include "qv4managed_p.h"
19
20QT_BEGIN_NAMESPACE
21
22namespace QV4 {
23
24struct Q_QML_EXPORT PersistentValueStorage
25{
26 PersistentValueStorage(ExecutionEngine *engine);
27 ~PersistentValueStorage();
28
29 Value *allocate();
30 static void free(Value *v)
31 {
32 if (v)
33 freeUnchecked(v);
34 }
35
36 void mark(MarkStack *markStack);
37
38 struct Iterator {
39 Iterator(void *p, int idx);
40 Iterator(const Iterator &o);
41 Iterator & operator=(const Iterator &o);
42 ~Iterator();
43 void *p;
44 int index;
45 Iterator &operator++();
46 bool operator !=(const Iterator &other) {
47 return p != other.p || index != other.index;
48 }
49 Value &operator *();
50 };
51 Iterator begin() { return Iterator(firstPage, 0); }
52 Iterator end() { return Iterator(nullptr, 0); }
53
54 void clearFreePageHint();
55
56 static ExecutionEngine *getEngine(const Value *v);
57
58 ExecutionEngine *engine;
59 void *firstPage;
60 void *freePageHint = nullptr;
61private:
62 static void freeUnchecked(Value *v);
63 static void freePage(void *page);
64};
65
66class Q_QML_EXPORT PersistentValue
67{
68public:
69 constexpr PersistentValue() noexcept = default;
70 PersistentValue(const PersistentValue &other);
71 PersistentValue &operator=(const PersistentValue &other);
72
73 PersistentValue(PersistentValue &&other) noexcept : val(std::exchange(obj&: other.val, new_val: nullptr)) {}
74 void swap(PersistentValue &other) noexcept { qt_ptr_swap(lhs&: val, rhs&: other.val); }
75 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(PersistentValue)
76 ~PersistentValue() { PersistentValueStorage::free(v: val); }
77
78 PersistentValue &operator=(const WeakValue &other);
79 PersistentValue &operator=(Object *object);
80
81 PersistentValue(ExecutionEngine *engine, const Value &value);
82 PersistentValue(ExecutionEngine *engine, ReturnedValue value);
83 PersistentValue(ExecutionEngine *engine, Object *object);
84
85 void set(ExecutionEngine *engine, const Value &value);
86 void set(ExecutionEngine *engine, ReturnedValue value);
87 void set(ExecutionEngine *engine, Heap::Base *obj);
88
89 ReturnedValue value() const {
90 return (val ? val->asReturnedValue() : Encode::undefined());
91 }
92 Value *valueRef() const {
93 return val;
94 }
95 Managed *asManaged() const {
96 if (!val)
97 return nullptr;
98 return val->managed();
99 }
100 template<typename T>
101 T *as() const {
102 if (!val)
103 return nullptr;
104 return val->as<T>();
105 }
106
107 ExecutionEngine *engine() const {
108 if (!val)
109 return nullptr;
110 return PersistentValueStorage::getEngine(v: val);
111 }
112
113 bool isUndefined() const { return !val || val->isUndefined(); }
114 bool isNullOrUndefined() const { return !val || val->isNullOrUndefined(); }
115 void clear() {
116 PersistentValueStorage::free(v: val);
117 val = nullptr;
118 }
119 bool isEmpty() { return !val; }
120
121private:
122 Value *val = nullptr;
123};
124
125class Q_QML_EXPORT WeakValue
126{
127public:
128 WeakValue() {}
129 WeakValue(const WeakValue &other);
130 WeakValue(ExecutionEngine *engine, const Value &value);
131 WeakValue &operator=(const WeakValue &other);
132 ~WeakValue();
133
134 void set(ExecutionEngine *engine, const Value &value);
135
136 void set(ExecutionEngine *engine, ReturnedValue value);
137
138 void set(ExecutionEngine *engine, Heap::Base *obj);
139
140 ReturnedValue value() const {
141 return (val ? val->asReturnedValue() : Encode::undefined());
142 }
143 Value *valueRef() const {
144 return val;
145 }
146 Managed *asManaged() const {
147 if (!val)
148 return nullptr;
149 return val->managed();
150 }
151 template <typename T>
152 T *as() const {
153 if (!val)
154 return nullptr;
155 return val->as<T>();
156 }
157
158 ExecutionEngine *engine() const {
159 if (!val)
160 return nullptr;
161 return PersistentValueStorage::getEngine(v: val);
162 }
163
164 bool isUndefined() const { return !val || val->isUndefined(); }
165 bool isNullOrUndefined() const { return !val || val->isNullOrUndefined(); }
166 void clear() { free(); }
167
168 void markOnce(MarkStack *markStack);
169
170private:
171 Value *val = nullptr;
172
173private:
174 Q_NEVER_INLINE void allocVal(ExecutionEngine *engine);
175
176 void free();
177};
178
179} // namespace QV4
180
181QT_END_NAMESPACE
182
183#endif
184

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtdeclarative/src/qml/jsruntime/qv4persistent_p.h