Port QDate/QDateTime/QRectF away from QQmlVmeVariant
authorFrank Meerkoetter <frank.meerkoetter@basyskom.com>
Thu, 16 Jul 2015 20:34:42 +0000 (22:34 +0200)
committerSimon Hausmann <simon.hausmann@theqtcompany.com>
Tue, 18 Aug 2015 20:25:34 +0000 (20:25 +0000)
Store QDate/QDateTime/QRectF 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: I1b5c4e24c1e46d19c5c861941655efb7a972a6a5
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
src/qml/qml/qqmlvmemetaobject.cpp
src/qml/qml/qqmlvmemetaobject_p.h

index 60f56dff5fd8258bb22cbee18f6e2c47d5e8cc07..d4ad0f2a9adac94f0a0b6ba97c5b8d8435749ca2 100644 (file)
@@ -635,6 +635,29 @@ void QQmlVMEMetaObject::writeProperty(int id, const QUrl& v)
     vp->putIndexed(id, sv);
 }
 
+// TODO: can we store the QDate in a QV4::Value primitive?
+void QQmlVMEMetaObject::writeProperty(int id, const QDate& 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 QDateTime& 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())
@@ -657,6 +680,17 @@ void QQmlVMEMetaObject::writeProperty(int id, const QSizeF& v)
     vp->putIndexed(id, sv);
 }
 
+void QQmlVMEMetaObject::writeProperty(int id, const QRectF& 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);
+}
+
 int QQmlVMEMetaObject::readPropertyAsInt(int id)
 {
     if (!ensurePropertiesAllocated())
@@ -733,6 +767,38 @@ QUrl QQmlVMEMetaObject::readPropertyAsUrl(int id)
     return v->d()->data.value<QUrl>();
 }
 
+QDate QQmlVMEMetaObject::readPropertyAsDate(int id)
+{
+    if (!ensurePropertiesAllocated())
+        return QDate();
+
+    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::Date) {
+        writeProperty(id, QDate());
+        return QDate();
+    }
+    return v->d()->data.value<QDate>();
+}
+
+QDateTime QQmlVMEMetaObject::readPropertyAsDateTime(int id)
+{
+    if (!ensurePropertiesAllocated())
+        return QDateTime();
+
+    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::DateTime) {
+        writeProperty(id, QDateTime());
+        return QDateTime();
+    }
+    return v->d()->data.value<QDateTime>();
+}
+
 QSizeF QQmlVMEMetaObject::readPropertyAsSizeF(int id)
 {
     if (!ensurePropertiesAllocated())
@@ -765,6 +831,21 @@ QPointF QQmlVMEMetaObject::readPropertyAsPointF(int id)
     return v->d()->data.value<QPointF>();
 }
 
+QRectF QQmlVMEMetaObject::readPropertyAsRectF(int id)
+{
+    if (!ensurePropertiesAllocated())
+        return QRectF();
+
+    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::RectF) {
+        writeProperty(id, QRectF());
+        return QRectF();
+    }
+    return v->d()->data.value<QRectF>();
+}
 
 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
 {
@@ -887,13 +968,13 @@ int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
                             *reinterpret_cast<QUrl *>(a[0]) = readPropertyAsUrl(id);
                             break;
                         case QVariant::Date:
-                            *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
+                            *reinterpret_cast<QDate *>(a[0]) = readPropertyAsDate(id);
                             break;
                         case QVariant::DateTime:
-                            *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
+                            *reinterpret_cast<QDateTime *>(a[0]) = readPropertyAsDateTime(id);
                             break;
                         case QVariant::RectF:
-                            *reinterpret_cast<QRectF *>(a[0]) = data[id].asQRectF();
+                            *reinterpret_cast<QRectF *>(a[0]) = readPropertyAsRectF(id);
                             break;
                         case QVariant::SizeF:
                             *reinterpret_cast<QSizeF *>(a[0]) = readPropertyAsSizeF(id);
@@ -944,16 +1025,16 @@ int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
                             writeProperty(id, *reinterpret_cast<QUrl *>(a[0]));
                             break;
                         case QVariant::Date:
-                            needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
-                            data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
+                            needActivate = *reinterpret_cast<QDate *>(a[0]) != readPropertyAsDate(id);
+                            writeProperty(id, *reinterpret_cast<QDate *>(a[0]));
                             break;
                         case QVariant::DateTime:
-                            needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
-                            data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
+                            needActivate = *reinterpret_cast<QDateTime *>(a[0]) != readPropertyAsDateTime(id);
+                            writeProperty(id, *reinterpret_cast<QDateTime *>(a[0]));
                             break;
                         case QVariant::RectF:
-                            needActivate = *reinterpret_cast<QRectF *>(a[0]) != data[id].asQRectF();
-                            data[id].setValue(*reinterpret_cast<QRectF *>(a[0]));
+                            needActivate = *reinterpret_cast<QRectF *>(a[0]) != readPropertyAsRectF(id);
+                            writeProperty(id, *reinterpret_cast<QRectF *>(a[0]));
                             break;
                         case QVariant::SizeF:
                             needActivate = *reinterpret_cast<QSizeF *>(a[0]) != readPropertyAsSizeF(id);
index 445b54e61c423c9624545dfb8b9cc75c7e9b6960..96b606203af6147764c608643a8e6e8c3912cf56 100644 (file)
@@ -211,6 +211,9 @@ public:
     QSizeF readPropertyAsSizeF(int id);
     QPointF readPropertyAsPointF(int id);
     QUrl readPropertyAsUrl(int id);
+    QDate readPropertyAsDate(int id);
+    QDateTime readPropertyAsDateTime(int id);
+    QRectF readPropertyAsRectF(int id);
 
     void writeProperty(int id, int v);
     void writeProperty(int id, bool v);
@@ -219,6 +222,9 @@ public:
     void writeProperty(int id, const QPointF& v);
     void writeProperty(int id, const QSizeF& v);
     void writeProperty(int id, const QUrl& v);
+    void writeProperty(int id, const QDate& v);
+    void writeProperty(int id, const QDateTime& v);
+    void writeProperty(int id, const QRectF& v);
 
     void ensureQObjectWrapper();