Port QUrl/QSizeF away from QQmlVmeVariant
authorFrank Meerkoetter <frank.meerkoetter@basyskom.com>
Thu, 16 Jul 2015 20:04:39 +0000 (22:04 +0200)
committerSimon Hausmann <simon.hausmann@theqtcompany.com>
Tue, 18 Aug 2015 20:25:29 +0000 (20:25 +0000)
Store QUrl/QSizeF in a javascript array. The values are wrapped inside
a QV4::Variant. This is part of a series sliming down the memory
usage of properties.

Change-Id: I62338fe7fe101496340a8d89f33030d0df5121b7
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
src/qml/qml/qqmlvmemetaobject.cpp
src/qml/qml/qqmlvmemetaobject_p.h

index a359cb7..60f56df 100644 (file)
@@ -624,6 +624,28 @@ void QQmlVMEMetaObject::writeProperty(int id, const QString& v)
     vp->putIndexed(id, s);
 }
 
+void QQmlVMEMetaObject::writeProperty(int id, const QUrl& v)
+{
+    if (!ensurePropertiesAllocated())
+        return;
+
+    QV4::Scope scope(properties.engine());
+    QV4::ScopedObject vp(scope, properties.value());
+    QV4::ScopedValue sv(scope, properties.engine()->newVariantObject(QVariant::fromValue(v)));
+    vp->putIndexed(id, sv);
+}
+
+void QQmlVMEMetaObject::writeProperty(int id, const QPointF& v)
+{
+    if (!ensurePropertiesAllocated())
+        return;
+
+    QV4::Scope scope(properties.engine());
+    QV4::ScopedObject vp(scope, properties.value());
+    QV4::ScopedValue sv(scope, properties.engine()->newVariantObject(QVariant::fromValue(v)));
+    vp->putIndexed(id, sv);
+}
+
 void QQmlVMEMetaObject::writeProperty(int id, const QSizeF& v)
 {
     if (!ensurePropertiesAllocated())
@@ -695,6 +717,22 @@ QString QQmlVMEMetaObject::readPropertyAsString(int id)
     return sv->stringValue()->toQString();
 }
 
+QUrl QQmlVMEMetaObject::readPropertyAsUrl(int id)
+{
+    if (!ensurePropertiesAllocated())
+        return QUrl();
+
+    QV4::Scope scope(properties.engine());
+    QV4::ScopedObject vp(scope, properties.value());
+    QV4::ScopedValue sv(scope, vp->getIndexed(id));
+    const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
+    if (!v || v->d()->data.type() != QVariant::Url) {
+        writeProperty(id, QUrl());
+        return QUrl();
+    }
+    return v->d()->data.value<QUrl>();
+}
+
 QSizeF QQmlVMEMetaObject::readPropertyAsSizeF(int id)
 {
     if (!ensurePropertiesAllocated())
@@ -711,6 +749,23 @@ QSizeF QQmlVMEMetaObject::readPropertyAsSizeF(int id)
     return v->d()->data.value<QSizeF>();
 }
 
+QPointF QQmlVMEMetaObject::readPropertyAsPointF(int id)
+{
+    if (!ensurePropertiesAllocated())
+        return QPointF();
+
+    QV4::Scope scope(properties.engine());
+    QV4::ScopedObject vp(scope, properties.value());
+    QV4::ScopedValue sv(scope, vp->getIndexed(id));
+    const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
+    if (!v || v->d()->data.type() != QVariant::PointF) {
+        writeProperty(id, QPointF());
+        return QPointF();
+    }
+    return v->d()->data.value<QPointF>();
+}
+
+
 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
 {
     int id = _id;
@@ -829,7 +884,7 @@ int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
                             *reinterpret_cast<QString *>(a[0]) = readPropertyAsString(id);
                             break;
                         case QVariant::Url:
-                            *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
+                            *reinterpret_cast<QUrl *>(a[0]) = readPropertyAsUrl(id);
                             break;
                         case QVariant::Date:
                             *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
@@ -844,7 +899,7 @@ int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
                             *reinterpret_cast<QSizeF *>(a[0]) = readPropertyAsSizeF(id);
                             break;
                         case QVariant::PointF:
-                            *reinterpret_cast<QPointF *>(a[0]) = data[id].asQPointF();
+                            *reinterpret_cast<QPointF *>(a[0]) = readPropertyAsPointF(id);
                             break;
                         case QMetaType::QObjectStar:
                             *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
@@ -885,8 +940,8 @@ int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
                             writeProperty(id, *reinterpret_cast<QString *>(a[0]));
                             break;
                         case QVariant::Url:
-                            needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
-                            data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
+                            needActivate = *reinterpret_cast<QUrl *>(a[0]) != readPropertyAsUrl(id);
+                            writeProperty(id, *reinterpret_cast<QUrl *>(a[0]));
                             break;
                         case QVariant::Date:
                             needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
@@ -905,8 +960,8 @@ int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
                             writeProperty(id, *reinterpret_cast<QSizeF *>(a[0]));
                             break;
                         case QVariant::PointF:
-                            needActivate = *reinterpret_cast<QPointF *>(a[0]) != data[id].asQPointF();
-                            data[id].setValue(*reinterpret_cast<QPointF *>(a[0]));
+                            needActivate = *reinterpret_cast<QPointF *>(a[0]) != readPropertyAsPointF(id);
+                            writeProperty(id, *reinterpret_cast<QPointF *>(a[0]));
                             break;
                         case QMetaType::QObjectStar:
                             needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
index c4ca128..445b54e 100644 (file)
@@ -209,12 +209,16 @@ public:
     double readPropertyAsDouble(int id);
     QString readPropertyAsString(int id);
     QSizeF readPropertyAsSizeF(int id);
+    QPointF readPropertyAsPointF(int id);
+    QUrl readPropertyAsUrl(int id);
 
     void writeProperty(int id, int v);
     void writeProperty(int id, bool v);
     void writeProperty(int id, double v);
     void writeProperty(int id, const QString& v);
+    void writeProperty(int id, const QPointF& v);
     void writeProperty(int id, const QSizeF& v);
+    void writeProperty(int id, const QUrl& v);
 
     void ensureQObjectWrapper();