From 37fbe667e6b5a528592b244124189981ba4aada9 Mon Sep 17 00:00:00 2001 From: Frank Meerkoetter Date: Sat, 25 Jul 2015 19:43:43 +0200 Subject: Adapt the ValueTypeProvider interface and port its types The ValueTypeProvider system allows non-QML modules to add new basic QML types. It needs to be changed when porting away from QQmlVmeVariant as underlying type of properties. Change-Id: I2b52d7e6f578647a39832896c28553404b9a679f Reviewed-by: Simon Hausmann --- src/quick/util/qquickglobal.cpp | 132 ++++++++++++---------------------------- 1 file changed, 40 insertions(+), 92 deletions(-) (limited to 'src/quick/util/qquickglobal.cpp') diff --git a/src/quick/util/qquickglobal.cpp b/src/quick/util/qquickglobal.cpp index fd172006e0..adf3a68e92 100644 --- a/src/quick/util/qquickglobal.cpp +++ b/src/quick/util/qquickglobal.cpp @@ -397,41 +397,30 @@ public: return 0; } - template - bool typedInit(void *data, size_t dataSize) - { - ASSERT_VALID_SIZE(dataSize, sizeof(T)); - T *t = reinterpret_cast(data); - new (t) T(); - return true; - } - - bool init(int type, void *data, size_t dataSize) + bool init(int type, QVariant& dst) { switch (type) { case QMetaType::QColor: - return typedInit(data, dataSize); + dst.setValue(QColor()); + return true; case QMetaType::QFont: - return typedInit(data, dataSize); + dst.setValue(QFont()); + return true; case QMetaType::QVector2D: - return typedInit(data, dataSize); + dst.setValue(QVector2D()); + return true; case QMetaType::QVector3D: - return typedInit(data, dataSize); + dst.setValue(QVector3D()); + return true; case QMetaType::QVector4D: - return typedInit(data, dataSize); + dst.setValue(QVector4D()); + return true; case QMetaType::QQuaternion: - return typedInit(data, dataSize); + dst.setValue(QQuaternion()); + return true; case QMetaType::QMatrix4x4: - { - if (dataSize >= sizeof(QMatrix4x4)) - return typedInit(data, dataSize); - - // special case: init matrix-containing qvariant. - Q_ASSERT(dataSize >= sizeof(QVariant)); - QVariant *matvar = reinterpret_cast(data); - new (matvar) QVariant(QMatrix4x4()); + dst.setValue(QMatrix4x4()); return true; - } default: break; } @@ -691,12 +680,12 @@ public: } template - bool typedEqual(const void *lhs, const void *rhs) + bool typedEqual(const void *lhs, const QVariant& rhs) { - return (*(reinterpret_cast(lhs)) == *(reinterpret_cast(rhs))); + return (*(reinterpret_cast(lhs)) == rhs.value()); } - bool equal(int type, const void *lhs, const void *rhs, size_t rhsSize) + bool equal(int type, const void *lhs, const QVariant &rhs) { switch (type) { case QMetaType::QColor: @@ -712,14 +701,7 @@ public: case QMetaType::QQuaternion: return typedEqual(lhs, rhs); case QMetaType::QMatrix4x4: - { - if (rhsSize >= sizeof(QMatrix4x4)) - return typedEqual(lhs, rhs); - - Q_ASSERT(rhsSize >= sizeof(QVariant)); - QMatrix4x4 rhsmat = reinterpret_cast(rhs)->value(); - return typedEqual(lhs, &rhsmat); - } + return typedEqual(lhs, rhs); default: break; } @@ -778,50 +760,34 @@ public: } template - bool typedRead(int srcType, const void *src, size_t srcSize, int dstType, void *dst) + bool typedRead(const QVariant& src, int dstType, void *dst) { T *dstT = reinterpret_cast(dst); - if (srcType == dstType) { - ASSERT_VALID_SIZE(srcSize, sizeof(T)); - const T *srcT = reinterpret_cast(src); - *dstT = *srcT; + if (src.type() == static_cast(dstType)) { + *dstT = src.value(); } else { *dstT = T(); } return true; } - bool read(int srcType, const void *src, size_t srcSize, int dstType, void *dst) + bool read(const QVariant &src, void *dst, int dstType) { switch (dstType) { case QMetaType::QColor: - return typedRead(srcType, src, srcSize, dstType, dst); + return typedRead(src, dstType, dst); case QMetaType::QFont: - return typedRead(srcType, src, srcSize, dstType, dst); + return typedRead(src, dstType, dst); case QMetaType::QVector2D: - return typedRead(srcType, src, srcSize, dstType, dst); + return typedRead(src, dstType, dst); case QMetaType::QVector3D: - return typedRead(srcType, src, srcSize, dstType, dst); + return typedRead(src, dstType, dst); case QMetaType::QVector4D: - return typedRead(srcType, src, srcSize, dstType, dst); + return typedRead(src, dstType, dst); case QMetaType::QQuaternion: - return typedRead(srcType, src, srcSize, dstType, dst); + return typedRead(src, dstType, dst); case QMetaType::QMatrix4x4: - { - if (srcSize >= sizeof(QMatrix4x4)) - return typedRead(srcType, src, srcSize, dstType, dst); - - // the source data may be stored in a QVariant. - QMatrix4x4 *dstMat = reinterpret_cast(dst); - if (srcType == dstType) { - Q_ASSERT(srcSize >= sizeof(QVariant)); - const QVariant *srcMatVar = reinterpret_cast(src); - *dstMat = srcMatVar->value(); - } else { - *dstMat = QMatrix4x4(); - } - return true; - } + return typedRead(src, dstType, dst); default: break; } @@ -829,51 +795,33 @@ public: } template - bool typedWrite(const void *src, void *dst, size_t dstSize) + bool typedWrite(const void *src, QVariant& dst) { - ASSERT_VALID_SIZE(dstSize, sizeof(T)); const T *srcT = reinterpret_cast(src); - T *dstT = reinterpret_cast(dst); - if (*dstT != *srcT) { - *dstT = *srcT; + if (dst.value() != *srcT) { + dst = *srcT; return true; } return false; } - bool write(int type, const void *src, void *dst, size_t dstSize) + bool write(int type, const void *src, QVariant& dst) { switch (type) { case QMetaType::QColor: - return typedWrite(src, dst, dstSize); + return typedWrite(src, dst); case QMetaType::QFont: - return typedWrite(src, dst, dstSize); + return typedWrite(src, dst); case QMetaType::QVector2D: - return typedWrite(src, dst, dstSize); + return typedWrite(src, dst); case QMetaType::QVector3D: - return typedWrite(src, dst, dstSize); + return typedWrite(src, dst); case QMetaType::QVector4D: - return typedWrite(src, dst, dstSize); + return typedWrite(src, dst); case QMetaType::QQuaternion: - return typedWrite(src, dst, dstSize); + return typedWrite(src, dst); case QMetaType::QMatrix4x4: - { - if (dstSize >= sizeof(QMatrix4x4)) - return typedWrite(src, dst, dstSize); - - // special case: storing matrix into variant - // eg, QVMEMO QVMEVariant data cell is big enough to store - // QVariant, but not large enough to store QMatrix4x4. - Q_ASSERT(dstSize >= sizeof(QVariant)); - const QMatrix4x4 *srcMat = reinterpret_cast(src); - QVariant *dstMatVar = reinterpret_cast(dst); - QMatrix4x4 dstMatVal = dstMatVar->value(); - if (dstMatVal != *srcMat) { - *dstMatVar = QVariant(*srcMat); - return true; - } - return false; - } + return typedWrite(src, dst); default: break; } -- cgit v1.2.3