From 51f1d68addd23516d7e6e252e476feac0a95d0c0 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 28 Oct 2009 15:55:47 +0100 Subject: Fix warning in qvariant.h header + make sure QVariant in QVariant works as expected Also add more test Reviewed-by: Thierry --- src/corelib/kernel/qvariant.h | 39 ++---- tests/auto/qvariant/tst_qvariant.cpp | 245 +++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+), 27 deletions(-) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 4cce529..a1ab4e9 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -456,9 +456,9 @@ template inline void qVariantSetValue(QVariant &v, const T &t) { //if possible we reuse the current QVariant private - const int type = qMetaTypeId(reinterpret_cast(0)); + const uint type = qMetaTypeId(reinterpret_cast(0)); QVariant::Private &d = v.data_ptr(); - if (v.isDetached() && (type <= int(QVariant::Char) || type == d.type)) { + if (v.isDetached() && (type <= uint(QVariant::Char) || type == d.type)) { d.type = type; d.is_null = false; T *old = reinterpret_cast(d.is_shared ? d.data.shared->ptr : &d.data.ptr); @@ -470,6 +470,13 @@ inline void qVariantSetValue(QVariant &v, const T &t) } } +template <> +inline void qVariantSetValue(QVariant &v, const QVariant &t) +{ + v = t; +} + + inline QVariant::QVariant() {} inline bool QVariant::isValid() const { return d.type != Invalid; } @@ -559,9 +566,7 @@ inline bool operator!=(const QVariant &v1, const QVariantComparisonHelper &v2) #endif #ifndef QT_MOC -#if !defined qdoc && defined Q_CC_MSVC && _MSC_VER < 1300 - -template T qvariant_cast(const QVariant &v, T * = 0) +template inline T qvariant_cast(const QVariant &v) { const int vid = qMetaTypeId(static_cast(0)); if (vid == v.userType()) @@ -574,28 +579,9 @@ template T qvariant_cast(const QVariant &v, T * = 0) return T(); } -template -inline T qVariantValue(const QVariant &variant, T *t = 0) -{ return qvariant_cast(variant, t); } - -template -inline bool qVariantCanConvert(const QVariant &variant, T *t = 0) +template<> inline QVariant qvariant_cast(const QVariant &v) { - return variant.canConvert(static_cast(qMetaTypeId(t))); -} -#else - -template T qvariant_cast(const QVariant &v) -{ - const int vid = qMetaTypeId(static_cast(0)); - if (vid == v.userType()) - return *reinterpret_cast(v.constData()); - if (vid < int(QMetaType::User)) { - T t; - if (qvariant_cast_helper(v, QVariant::Type(vid), &t)) - return t; - } - return T(); + return v; } template @@ -609,7 +595,6 @@ inline bool qVariantCanConvert(const QVariant &variant) qMetaTypeId(static_cast(0)))); } #endif -#endif Q_DECLARE_SHARED(QVariant) Q_DECLARE_TYPEINFO(QVariant, Q_MOVABLE_TYPE); diff --git a/tests/auto/qvariant/tst_qvariant.cpp b/tests/auto/qvariant/tst_qvariant.cpp index de4d7b4..61e9a4f 100644 --- a/tests/auto/qvariant/tst_qvariant.cpp +++ b/tests/auto/qvariant/tst_qvariant.cpp @@ -270,6 +270,8 @@ private slots: void task256984_setValue(); void numericalConvert(); + void moreCustomTypes(); + void variantInVariant(); }; Q_DECLARE_METATYPE(QDate) @@ -3135,7 +3137,250 @@ void tst_QVariant::numericalConvert() } +template void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool) +{ + QVariant v = QVariant::fromValue(orig); + QVERIFY(v.isValid()); + QCOMPARE(v.isNull(), isNull); + QCOMPARE(v.toString(), toString); + QCOMPARE(v.toDouble(), toDouble); + QCOMPARE(v.toBool(), toBool); + QCOMPARE(qvariant_cast(v), orig); + + { + QVariant v2 = v; + QCOMPARE(v2, v); + QVERIFY(v2.isValid()); + QCOMPARE(v2.isNull(), isNull); + QCOMPARE(v2.toString(), toString); + QCOMPARE(v2.toDouble(), toDouble); + QCOMPARE(v2.toBool(), toBool); + QCOMPARE(qvariant_cast(v2), orig); + + QVariant v3; + v = QVariant(); + QCOMPARE(v3, v); + v = v2; + QCOMPARE(v, v2); + QCOMPARE(qvariant_cast(v2), qvariant_cast(v)); + QCOMPARE(v2.toString(), toString); + v3 = qVariantFromValue(orig); + + QVERIFY(v3.isValid()); + QCOMPARE(v3.isNull(), isNull); + QCOMPARE(v3.toString(), toString); + QCOMPARE(v3.toDouble(), toDouble); + QCOMPARE(v3.toBool(), toBool); + QCOMPARE(qvariant_cast(v3), qvariant_cast(v)); + } + + QVERIFY(v.isValid()); + QCOMPARE(v.isNull(), isNull); + QCOMPARE(v.toString(), toString); + QCOMPARE(v.toDouble(), toDouble); + QCOMPARE(v.toBool(), toBool); + QCOMPARE(qvariant_cast(v), orig); + + if (qMetaTypeId() != qMetaTypeId()) { + QCOMPARE(v.userType(), qMetaTypeId()); + QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId())); + } +} + + +struct MyPrimitive +{ + char x, y; + bool operator==(const MyPrimitive &o) const + { + return x == o.x && y == o.y; + } +}; +Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE); + +struct MyData +{ + void *ptr; + MyData() : ptr(this) {} + ~MyData() { Q_ASSERT(ptr == this); } + MyData(const MyData& o) : ptr(this) { Q_ASSERT(o.ptr == &o); } + MyData &operator=(const MyData &o) + { + Q_ASSERT(ptr == this); + Q_ASSERT(o.ptr == &o); + return *this; + } + bool operator==(const MyData &o) const + { + Q_ASSERT(ptr == this); + Q_ASSERT(o.ptr == &o); + return true; + } +}; + +struct MyMovable +{ + static int count; + int v; + MyMovable() { v = count++; } + ~MyMovable() { count--; } + MyMovable(const MyMovable &o) : v(o.v) { count++; } + + bool operator==(const MyMovable &o) const + { + return v == o.v; + } +}; + +int MyMovable::count = 0; + + +Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE); + +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(MyPrimitive) +Q_DECLARE_METATYPE(MyData) +Q_DECLARE_METATYPE(MyMovable) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(MyPrimitive *) +Q_DECLARE_METATYPE(MyData *) +Q_DECLARE_METATYPE(MyMovable *) + +void tst_QVariant::moreCustomTypes() +{ + { + QList listSize; + playWithVariant(listSize, false, QString(), 0, false); + listSize << QSize(4,5) << QSize(89,23) << QSize(5,6); + playWithVariant(listSize, false, QString(), 0, false); + } + + { + QString str; + playWithVariant(str, true, QString(), 0, false); + str = QString::fromLatin1("123456789.123"); + playWithVariant(str, false, str, 123456789.123, true); + } + + { + QSize size; + playWithVariant(size, false, QString(), 0, false); + playWithVariant(QSize(45,78), false, QString(), 0, false); + } + + { + MyData d; + playWithVariant(d, false, QString(), 0, false); + playWithVariant(&d, false, QString(), 0, false); + QList l; + playWithVariant(l, false, QString(), 0, false); + l << MyData() << MyData(); + playWithVariant(l, false, QString(), 0, false); + } + + { + MyPrimitive d = { 4, 5 }; + playWithVariant(d, false, QString(), 0, false); + playWithVariant(&d, false, QString(), 0, false); + QList l; + playWithVariant(l, false, QString(), 0, false); + l << d; + playWithVariant(l, false, QString(), 0, false); + } + + { + MyMovable d; + playWithVariant(d, false, QString(), 0, false); + playWithVariant(&d, false, QString(), 0, false); + QList l; + playWithVariant(l, false, QString(), 0, false); + l << MyMovable() << d; + playWithVariant(l, false, QString(), 0, false); + } + QCOMPARE(MyMovable::count, 0); + + { + playWithVariant(12.12, false, "12.12", 12.12, true); + playWithVariant(12.12f, false, "12.12", 12.12f, true); + playWithVariant('a', false, "a", 'a', true); + playWithVariant((unsigned char)('a'), false, "a", 'a', true); + playWithVariant( quint8(12), false, "\xc", 12, true); + playWithVariant( qint8(13), false, "\xd", 13, true); + playWithVariant(quint16(14), false, "14", 14, true); + playWithVariant( qint16(15), false, "15", 15, true); + playWithVariant(quint32(16), false, "16", 16, true); + playWithVariant( qint32(17), false, "17", 17, true); + playWithVariant(quint64(18), false, "18", 18, true); + playWithVariant( qint64(19), false, "19", 19, true); + playWithVariant( qint8(-12), false, "\xf4", -12, true); + playWithVariant( qint16(-13), false, "-13", -13, true); + playWithVariant( qint32(-14), false, "-14", -14, true); + playWithVariant( qint64(-15), false, "-15", -15, true); + playWithVariant(quint64(0), false, "0", 0, false); + playWithVariant( true, false, "true", 1, true); + playWithVariant( false, false, "false", 0, false); + + playWithVariant(QString("hello\n"), false, "hello\n", 0, true); + } + + { + int i = 5; + playWithVariant((void *)(&i), false, QString(), 0, false); + playWithVariant((void *)(0), false, QString(), 0, false); + } + + { + QVariant v1 = QVariant::fromValue(5); + QVariant v2 = QVariant::fromValue(5.0); + QVariant v3 = QVariant::fromValue(quint16(5)); + QVariant v4 = 5; + QVariant v5 = QVariant::fromValue(MyPrimitive()); + QVariant v6 = QVariant::fromValue(MyMovable()); + QVariant v7 = QVariant::fromValue(MyData()); + playWithVariant(v1, false, "5", 5, true); + playWithVariant(v2, false, "5", 5, true); + playWithVariant(v3, false, "5", 5, true); + playWithVariant(v4, false, "5", 5, true); + + playWithVariant(v5, false, QString(), 0, false); + } +} + + +void tst_QVariant::variantInVariant() +{ + QVariant var1 = 5; + QCOMPARE(var1.type(), QVariant::Int); + QVariant var2 = var1; + QCOMPARE(var2, var1); + QCOMPARE(var2.type(), QVariant::Int); + QVariant var3 = QVariant::fromValue(var1); + QCOMPARE(var3, var1); + QCOMPARE(var3.type(), QVariant::Int); + QVariant var4 = qvariant_cast(var1); + QCOMPARE(var4, var1); + QCOMPARE(var4.type(), QVariant::Int); + QVariant var5; + var5 = var1; + QCOMPARE(var5, var1); + QCOMPARE(var5.type(), QVariant::Int); + QVariant var6; + var6.setValue(var1); + QCOMPARE(var6, var1); + QCOMPARE(var6.type(), QVariant::Int); + + QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2)); + QCOMPARE(qvariant_cast(var3), QVariant::fromValue(var4)); + QCOMPARE(qvariant_cast(var5), qvariant_cast(var6)); + + QString str("hello"); + QVariant var8 = qvariant_cast(QVariant::fromValue(QVariant::fromValue(str))); + QCOMPARE((int)var8.type(), (int)QVariant::String); + QCOMPARE(qvariant_cast(QVariant(qvariant_cast(var8))), str); +} QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc" -- cgit v0.12