From 8fd64d22ac7892b061a09c42c72aacf033b80876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Wed, 12 Oct 2011 17:32:26 +0200 Subject: Make usage of internal QVariant space. Each QVariant instance has internal storage which may be used for well-know basic types. This patch changes the behavior by delegating type dependent operation to QMetaType class which knows more types than QVariant itself. The patch significantly reduce amount of code in QVariant implementation. There are few side effects of this patch: - better performance: * for Core types when using Gui (QGuiVariant is able to construct Core types) * for small custom types (QVariant::Private::Data is used for all types that has size small enough) - comparing two QVariants can give different result for small custom types (binary comparison instead of pointer comparison) Change-Id: Ic17fa500d6a882110bfba896fd456c8e6c7a63a9 Reviewed-by: Olivier Goffart --- .../auto/corelib/kernel/qvariant/tst_qvariant.cpp | 28 +++++++++++++++++----- .../auto/testlib/selftests/cmptest/tst_cmptest.cpp | 12 ++++++---- 2 files changed, 30 insertions(+), 10 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index c4fed9088f..21bdfa7791 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -325,6 +325,10 @@ void tst_QVariant::isNull() QVariant var; QVERIFY( var.isNull() ); + QString str1; + QVariant var1( str1 ); + QVERIFY( var1.isNull() ); + QVariant var2( QString::null ); QVERIFY( var2.isNull() ); @@ -2017,10 +2021,12 @@ void tst_QVariant::userType() QVariant userVar3; qVariantSetValue(userVar3, data2); - QVERIFY(userVar2 != userVar3); + QVERIFY(userVar2 == userVar3); userVar3 = userVar2; QVERIFY(userVar2 == userVar3); } + // At this point all QVariants got destroyed but we have 2 MyType instances. + QCOMPARE(instanceCount, 2); { QVariant userVar; qVariantSetValue(userVar, &data); @@ -2056,10 +2062,9 @@ void tst_QVariant::userType() QVariant myCarrier; qVariantSetValue(myCarrier, data); QCOMPARE(instanceCount, 3); - { QVariant second = myCarrier; - QCOMPARE(instanceCount, 3); + QCOMPARE(instanceCount, 4); second.detach(); QCOMPARE(instanceCount, 4); } @@ -2103,6 +2108,7 @@ void tst_QVariant::userType() QCOMPARE(qvariant_cast(myCarrier), 42); } + // At this point all QVariants got destroyed and MyType objects too. QCOMPARE(instanceCount, 0); } @@ -2701,7 +2707,7 @@ void tst_QVariant::task172061_invalidDate() const struct WontCompare { - int x; + int x,y,z,q,w,e,r,t; }; Q_DECLARE_METATYPE(WontCompare); @@ -2952,7 +2958,12 @@ template void playWithVariant(const T &orig, bool isNull, const QString { QVariant v2 = v; - QCOMPARE(v2, v); + if (!(QTypeInfo::isStatic && QTypeInfo::isComplex)) { + // Type is movable so standard comparison algorithm in QVariant should work + // In a custom type QVariant is not aware of ==operator so it won't be called, + // which may cause problems especially visible when using a not-movable type + QCOMPARE(v2, v); + } QVERIFY(v2.isValid()); QCOMPARE(v2.isNull(), isNull); QCOMPARE(v2.toString(), toString); @@ -2964,7 +2975,12 @@ template void playWithVariant(const T &orig, bool isNull, const QString v = QVariant(); QCOMPARE(v3, v); v = v2; - QCOMPARE(v, v2); + if (!(QTypeInfo::isStatic && QTypeInfo::isComplex)) { + // Type is movable so standard comparison algorithm in QVariant should work + // In a custom type QVariant is not aware of ==operator so it won't be called, + // which may cause problems especially visible when using a not-movable type + QCOMPARE(v2, v); + } QCOMPARE(qvariant_cast(v2), qvariant_cast(v)); QCOMPARE(v2.toString(), toString); v3 = qVariantFromValue(orig); diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp index 4d38aa1646..70396a75b3 100644 --- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp @@ -80,8 +80,10 @@ void tst_Cmptest::compare_pointerfuncs() Q_DECLARE_METATYPE(QVariant) -class PhonyClass -{}; +struct PhonyClass +{ + int i; +}; void tst_Cmptest::compare_tostring_data() { @@ -108,9 +110,11 @@ void tst_Cmptest::compare_tostring_data() << QVariant(QVariant::Type(qRegisterMetaType("PhonyClass"))) ; + PhonyClass fake1 = {1}; + PhonyClass fake2 = {2}; QTest::newRow("both non-null user type") - << QVariant(qRegisterMetaType("PhonyClass"), (const void*)0) - << QVariant(qRegisterMetaType("PhonyClass"), (const void*)0) + << QVariant(qRegisterMetaType("PhonyClass"), (const void*)&fake1) + << QVariant(qRegisterMetaType("PhonyClass"), (const void*)&fake2) ; } -- cgit v1.2.3