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 Q_QML_PRIVATE_EXPORT SequencePrototype : public QV4::Object
32{
33 V4_PROTOTYPE(arrayPrototype)
34 void init();
35
36 static ReturnedValue method_valueOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
37 static ReturnedValue method_sort(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
38
39 static ReturnedValue newSequence(
40 QV4::ExecutionEngine *engine, QMetaType sequenceType, const void *data,
41 Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
42 static ReturnedValue fromVariant(QV4::ExecutionEngine *engine, const QVariant &vd);
43 static ReturnedValue fromData(QV4::ExecutionEngine *engine, QMetaType type, const void *data);
44
45 static QMetaType metaTypeForSequence(const Sequence *object);
46 static QVariant toVariant(const Sequence *object);
47 static QVariant toVariant(const Value &array, QMetaType typeHint);
48 static void *getRawContainerPtr(const Sequence *object, QMetaType typeHint);
49};
50
51namespace Heap {
52
53struct Sequence : ReferenceObject
54{
55 void init(const QQmlType &qmlType, const void *container);
56 void init(const QQmlType &qmlType, const void *container,
57 Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
58
59 Sequence *detached() const;
60 void destroy();
61
62 bool hasData() const { return m_container != nullptr; }
63 void *storagePointer();
64 const void *storagePointer() const { return m_container; }
65
66 bool isReadOnly() const { return m_object && !canWriteBack(); }
67
68 bool setVariant(const QVariant &variant);
69 QVariant toVariant() const;
70
71 const QQmlTypePrivate *typePrivate() const { return m_typePrivate; }
72
73private:
74 void *m_container;
75 const QQmlTypePrivate *m_typePrivate;
76};
77
78}
79
80struct Q_QML_PRIVATE_EXPORT Sequence : public QV4::ReferenceObject
81{
82 V4_OBJECT2(Sequence, QV4::ReferenceObject)
83 Q_MANAGED_TYPE(V4Sequence)
84 V4_PROTOTYPE(sequencePrototype)
85 V4_NEEDS_DESTROY
86public:
87 static const QMetaType valueMetaType(const Heap::Sequence *p);
88 static QV4::ReturnedValue virtualGet(
89 const QV4::Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty);
90 static qint64 virtualGetLength(const Managed *m);
91 static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, Value *receiver);
92 static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id);
93 static bool virtualIsEqualTo(Managed *that, Managed *other);
94 static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target);
95 static int virtualMetacall(Object *object, QMetaObject::Call call, int index, void **a);
96
97 qsizetype size() const;
98 QVariant at(qsizetype index) const;
99 void append(const QVariant &item);
100 void append(qsizetype num, const QVariant &item);
101 void replace(qsizetype index, const QVariant &item);
102 void removeLast(qsizetype num);
103
104 QV4::ReturnedValue containerGetIndexed(qsizetype index, bool *hasProperty) const;
105 bool containerPutIndexed(qsizetype index, const QV4::Value &value);
106 bool containerDeleteIndexedProperty(qsizetype index);
107 bool containerIsEqualTo(Managed *other);
108 bool sort(const FunctionObject *f, const Value *, const Value *argv, int argc);
109 void *getRawContainerPtr() const;
110 bool loadReference() const;
111 bool storeReference();
112};
113
114}
115
116#define QT_DECLARE_SEQUENTIAL_CONTAINER(LOCAL, FOREIGN, VALUE) \
117 struct LOCAL \
118 { \
119 Q_GADGET \
120 QML_ANONYMOUS \
121 QML_SEQUENTIAL_CONTAINER(VALUE) \
122 QML_FOREIGN(FOREIGN) \
123 QML_ADDED_IN_VERSION(2, 0) \
124 }
125
126// We use the original QT_COORD_TYPE name because that will match up with relevant other
127// types in plugins.qmltypes (if you use either float or double, that is; otherwise you're
128// on your own).
129#ifdef QT_COORD_TYPE
130QT_DECLARE_SEQUENTIAL_CONTAINER(QStdRealVectorForeign, std::vector<qreal>, QT_COORD_TYPE);
131QT_DECLARE_SEQUENTIAL_CONTAINER(QRealListForeign, QList<qreal>, QT_COORD_TYPE);
132#else
133QT_DECLARE_SEQUENTIAL_CONTAINER(QRealStdVectorForeign, std::vector<qreal>, double);
134QT_DECLARE_SEQUENTIAL_CONTAINER(QRealListForeign, QList<qreal>, double);
135#endif
136
137QT_DECLARE_SEQUENTIAL_CONTAINER(QDoubleStdVectorForeign, std::vector<double>, double);
138QT_DECLARE_SEQUENTIAL_CONTAINER(QFloatStdVectorForeign, std::vector<float>, float);
139QT_DECLARE_SEQUENTIAL_CONTAINER(QIntStdVectorForeign, std::vector<int>, int);
140QT_DECLARE_SEQUENTIAL_CONTAINER(QBoolStdVectorForeign, std::vector<bool>, bool);
141QT_DECLARE_SEQUENTIAL_CONTAINER(QStringStdVectorForeign, std::vector<QString>, QString);
142QT_DECLARE_SEQUENTIAL_CONTAINER(QUrlStdVectorForeign, std::vector<QUrl>, QUrl);
143
144#undef QT_DECLARE_SEQUENTIAL_CONTAINER
145
146QT_END_NAMESPACE
147
148#endif // QV4SEQUENCEWRAPPER_P_H
149

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