Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4persistent_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4#ifndef QV4PERSISTENT_H
5#define QV4PERSISTENT_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 "qv4value_p.h"
19#include "qv4managed_p.h"
20
22
23namespace QV4 {
24
25struct Page;
26
27struct Q_QML_EXPORT PersistentValueStorage
28{
31
32 Value *allocate();
33 static void free(Value *v)
34 {
35 if (v)
36 freeUnchecked(v);
37 }
38
39 void mark(MarkStack *markStack);
40
41 struct Iterator {
42 Iterator(Page *p, int idx);
43 Iterator(const Iterator &o);
44 Iterator & operator=(const Iterator &o);
45 ~Iterator();
47 int index;
48 Iterator &operator++();
49 bool operator !=(const Iterator &other) {
50 return p != other.p || index != other.index;
51 }
52 Value &operator *();
53 };
54 Iterator begin() { return Iterator(firstPage, 0); }
55 Iterator end() { return Iterator(nullptr, 0); }
56
58
59 static ExecutionEngine *getEngine(const Value *v);
60
61 ExecutionEngine *engine;
62 Page *firstPage = nullptr;
64private:
65 static void freeUnchecked(Value *v);
66};
67
68class Q_QML_EXPORT PersistentValue
69{
70public:
71 constexpr PersistentValue() noexcept = default;
72 PersistentValue(const PersistentValue &other);
73 PersistentValue &operator=(const PersistentValue &other);
74
75 PersistentValue(PersistentValue &&other) noexcept : val(std::exchange(other.val, nullptr)) {}
76 void swap(PersistentValue &other) noexcept { qt_ptr_swap(val, other.val); }
77 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(PersistentValue)
78 ~PersistentValue() { PersistentValueStorage::free(val); }
79
80 PersistentValue &operator=(const WeakValue &other);
81 PersistentValue &operator=(Object *object);
82
83 PersistentValue(ExecutionEngine *engine, const Value &value);
84 PersistentValue(ExecutionEngine *engine, ReturnedValue value);
85 PersistentValue(ExecutionEngine *engine, Object *object);
86
87 void set(ExecutionEngine *engine, const Value &value);
88 void set(ExecutionEngine *engine, ReturnedValue value);
89 void set(ExecutionEngine *engine, Heap::Base *obj);
90
91 ReturnedValue value() const {
92 return (val ? val->asReturnedValue() : Encode::undefined());
93 }
94 Value *valueRef() const {
95 return val;
96 }
97 Managed *asManaged() const {
98 if (!val)
99 return nullptr;
100 return val->managed();
101 }
102 template<typename T>
103 T *as() const {
104 if (!val)
105 return nullptr;
106 return val->as<T>();
107 }
108
109 ExecutionEngine *engine() const {
110 if (!val)
111 return nullptr;
112 return PersistentValueStorage::getEngine(val);
113 }
114
115 bool isUndefined() const { return !val || val->isUndefined(); }
116 bool isNullOrUndefined() const { return !val || val->isNullOrUndefined(); }
117 void clear() {
118 PersistentValueStorage::free(val);
119 val = nullptr;
120 }
121 bool isEmpty() { return !val; }
122
123private:
124 Value *val = nullptr;
125};
126
127class Q_QML_EXPORT WeakValue
128{
129public:
130 WeakValue() {}
131 WeakValue(const WeakValue &other);
132 WeakValue(ExecutionEngine *engine, const Value &value);
133 WeakValue &operator=(const WeakValue &other);
134 ~WeakValue();
135
136 void set(ExecutionEngine *engine, const Value &value);
137
138 void set(ExecutionEngine *engine, ReturnedValue value);
139
140 void set(ExecutionEngine *engine, Heap::Base *obj);
141
142 ReturnedValue value() const {
143 return (val ? val->asReturnedValue() : Encode::undefined());
144 }
145 Value *valueRef() const {
146 return val;
147 }
148 Managed *asManaged() const {
149 if (!val)
150 return nullptr;
151 return val->managed();
152 }
153 template <typename T>
154 T *as() const {
155 if (!val)
156 return nullptr;
157 return val->as<T>();
158 }
159
160 ExecutionEngine *engine() const {
161 if (!val)
162 return nullptr;
163 return PersistentValueStorage::getEngine(val);
164 }
165
166 bool isUndefined() const { return !val || val->isUndefined(); }
167 bool isNullOrUndefined() const { return !val || val->isNullOrUndefined(); }
168 void clear() { free(); }
169
170 void markOnce(MarkStack *markStack);
171
172private:
173 Value *val = nullptr;
174
175private:
176 Q_NEVER_INLINE void allocVal(ExecutionEngine *engine);
177
178 void free();
179};
180
181} // namespace QV4
182
183QT_END_NAMESPACE
184
185#endif
QV4::ExecutionEngine * engine
Definition qv4mm_p.h:495
Combined button and popup list for selecting options.
Definition qjsvalue.h:24
QML_NEARLY_ALWAYS_INLINE void unlink(Page *p)
Page * allocatePage(PersistentValueStorage *storage)
Page * getPage(const Value *val)
static void releasePage(Page *p)
static const int kEntriesPerPage
QML_NEARLY_ALWAYS_INLINE void insertInFront(PersistentValueStorage *storage, Page *p)
WTF::PageAllocation alloc
ExecutionEngine * engine
PersistentValueStorage * storage
Value values[1]
Iterator & operator=(const Iterator &o)
bool operator!=(const Iterator &other)
static void free(Value *v)
void mark(MarkStack *markStack)
static ExecutionEngine * getEngine(const Value *v)
const SparseArrayNode * begin() const
const SparseArrayNode * end() const