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 QV4SEQUENCEWRAPPER_P_H
5#define QV4SEQUENCEWRAPPER_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/qglobal.h>
19#include <QtCore/qvariant.h>
20#include <QtQml/qqml.h>
21
22#include <private/qv4referenceobject_p.h>
23#include <private/qv4value_p.h>
24#include <private/qv4object_p.h>
25
26QT_BEGIN_NAMESPACE
27
28namespace QV4 {
29
30struct Sequence;
31struct SequenceOwnPropertyKeyIterator;
32
33struct Q_QML_EXPORT SequencePrototype : public QV4::Object
34{
35 V4_PROTOTYPE(arrayPrototype)
36 void init();
37
38 static ReturnedValue method_valueOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
39 static ReturnedValue method_shift(const FunctionObject *b, const Value *thisObject, const Value *, int);
40 static ReturnedValue method_getLength(
41 const FunctionObject *b, const Value *thisObject, const Value *, int);
42 static ReturnedValue method_setLength(
43 const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
44
45 static ReturnedValue newSequence(
46 QV4::ExecutionEngine *engine, QMetaType type, QMetaSequence metaSequence, const void *data,
47 Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
48 static ReturnedValue fromVariant(QV4::ExecutionEngine *engine, const QVariant &vd);
49 static ReturnedValue fromData(
50 QV4::ExecutionEngine *engine, QMetaType type, QMetaSequence metaSequence, const void *data);
51
52 static QMetaType metaTypeForSequence(const Sequence *object);
53 static QVariant toVariant(const Sequence *object);
54 static QVariant toVariant(const Value &array, QMetaType targetType);
55
56 enum RawCopyResult
57 {
58 Copied,
59 WasEqual,
60 TypeMismatch
61 };
62
63 static void *rawContainerPtr(const Sequence *sequence, QMetaType typeHint);
64 static RawCopyResult setRawContainer(
65 Sequence *sequence, const void *container, QMetaType typeHint);
66 static RawCopyResult getRawContainer(
67 const Sequence *sequence, void *container, QMetaType typeHint);
68};
69
70namespace Heap {
71
72struct Sequence : ReferenceObject
73{
74 void init(QMetaType listType, QMetaSequence metaSequence, const void *container);
75 void init(QMetaType listType, QMetaSequence metaSequence, const void *container,
76 Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
77
78 Sequence *detached();
79 void destroy();
80
81 void *storagePointer();
82 const void *storagePointer() const { return m_container; }
83
84 bool isStoredInline() const
85 {
86 if (isReference())
87 return true;
88
89 const QMetaType valueType = valueMetaType();
90 switch (valueType.id()) {
91 case QMetaType::QVariant:
92 case QMetaType::QVariantHash:
93 case QMetaType::QVariantMap:
94 case QMetaType::QVariantList:
95 case QMetaType::QObjectStar:
96 return false;
97 default:
98 break;
99 }
100
101 return !valueType.flags().testFlag(flag: QMetaType::PointerToQObject);
102 }
103
104 bool isReadOnly() const { return m_object && !canWriteBack(); }
105
106 bool setVariant(const QVariant &variant);
107 QVariant toVariant() const;
108
109 QMetaType listType() const { return QMetaType(m_listType); }
110 QMetaType valueMetaType() const { return QMetaType(m_metaSequence->valueMetaType); }
111 QMetaSequence metaSequence() const { return QMetaSequence(m_metaSequence); }
112
113private:
114 friend struct QV4::Sequence;
115 friend struct QV4::SequencePrototype;
116 friend struct QV4::SequenceOwnPropertyKeyIterator;
117
118 void initTypes(QMetaType listType, QMetaSequence metaSequence);
119 void createElementWrappers(const void *container);
120 void createInlineStorage(const void *container);
121
122 bool loadReference();
123 bool storeReference();
124
125 union {
126 void *m_container; // if stored inline
127 uint m_size; // if stored out of line
128 };
129 const QtPrivate::QMetaTypeInterface *m_listType;
130 const QtMetaContainerPrivate::QMetaSequenceInterface *m_metaSequence;
131};
132
133}
134
135struct Q_QML_EXPORT Sequence : public QV4::ReferenceObject
136{
137 V4_OBJECT2(Sequence, QV4::ReferenceObject)
138 Q_MANAGED_TYPE(V4Sequence)
139 V4_PROTOTYPE(sequencePrototype)
140 V4_NEEDS_DESTROY
141public:
142 static QV4::ReturnedValue virtualGet(
143 const QV4::Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty);
144 static qint64 virtualGetLength(const Managed *m);
145 static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, Value *receiver);
146 static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id);
147 static bool virtualIsEqualTo(Managed *that, Managed *other);
148 static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target);
149 static int virtualMetacall(Object *object, QMetaObject::Call call, int index, void **a);
150};
151
152}
153
154#define QT_DECLARE_SEQUENTIAL_CONTAINER(LOCAL, FOREIGN, VALUE) \
155 struct LOCAL \
156 { \
157 Q_GADGET \
158 QML_ANONYMOUS \
159 QML_SEQUENTIAL_CONTAINER(VALUE) \
160 QML_FOREIGN(FOREIGN) \
161 QML_ADDED_IN_VERSION(2, 0) \
162 }
163
164// We use the original QT_COORD_TYPE name because that will match up with relevant other
165// types in plugins.qmltypes (if you use either float or double, that is; otherwise you're
166// on your own).
167#ifdef QT_COORD_TYPE
168QT_DECLARE_SEQUENTIAL_CONTAINER(QStdRealVectorForeign, std::vector<qreal>, QT_COORD_TYPE);
169QT_DECLARE_SEQUENTIAL_CONTAINER(QRealListForeign, QList<qreal>, QT_COORD_TYPE);
170#else
171QT_DECLARE_SEQUENTIAL_CONTAINER(QRealStdVectorForeign, std::vector<qreal>, double);
172QT_DECLARE_SEQUENTIAL_CONTAINER(QRealListForeign, QList<qreal>, double);
173#endif
174
175QT_DECLARE_SEQUENTIAL_CONTAINER(QDoubleStdVectorForeign, std::vector<double>, double);
176QT_DECLARE_SEQUENTIAL_CONTAINER(QFloatStdVectorForeign, std::vector<float>, float);
177QT_DECLARE_SEQUENTIAL_CONTAINER(QIntStdVectorForeign, std::vector<int>, int);
178QT_DECLARE_SEQUENTIAL_CONTAINER(QBoolStdVectorForeign, std::vector<bool>, bool);
179QT_DECLARE_SEQUENTIAL_CONTAINER(QStringStdVectorForeign, std::vector<QString>, QString);
180QT_DECLARE_SEQUENTIAL_CONTAINER(QUrlStdVectorForeign, std::vector<QUrl>, QUrl);
181
182#undef QT_DECLARE_SEQUENTIAL_CONTAINER
183
184QT_END_NAMESPACE
185
186#endif // QV4SEQUENCEWRAPPER_P_H
187

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