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