diff options
author | Jędrzej Nowacki <jedrzej.nowacki@nokia.com> | 2012-05-04 15:19:17 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-08 11:47:29 +0200 |
commit | 1e432f0a88b25098819fc9684b54606ba3a05123 (patch) | |
tree | dc01912358b31857ce9edea706a57e0c7015d726 | |
parent | 4169e20662d0bcf52d6a278f85f44f9dacad7a68 (diff) |
Introduce a new built-in type: signed char.
C++ distinguish between "char", "signed char" and
"unsigned char", they are three independent types.
Fix QVariant behavior on ARM. On ARM "char" may mean
"unsigned char", but we depends on the sign during
a numerical conversions.
Change-Id: I610ce3fb88ed5964b67f3ae442d264fe16b2d261
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 7 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype.h | 7 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 32 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.h | 1 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 6 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 27 |
6 files changed, 70 insertions, 10 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 2756dd5241..448b6e3938 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -161,6 +161,7 @@ struct DefinedTypesFilter { \value ULong \c{unsigned long} \value ULongLong ULongLong \value UShort \c{unsigned short} + \value SChar \c{signed char} \value UChar \c{unsigned char} \value Float \c float \value QObjectStar QObject * @@ -718,6 +719,9 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data) case QMetaType::UShort: stream << *static_cast<const ushort *>(data); break; + case QMetaType::SChar: + stream << *static_cast<const signed char *>(data); + break; case QMetaType::UChar: stream << *static_cast<const uchar *>(data); break; @@ -937,6 +941,9 @@ bool QMetaType::load(QDataStream &stream, int type, void *data) case QMetaType::UShort: stream >> *static_cast<ushort *>(data); break; + case QMetaType::SChar: + stream >> *static_cast<signed char *>(data); + break; case QMetaType::UChar: stream >> *static_cast<uchar *>(data); break; diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 595da53562..33e7fa43f1 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -77,6 +77,7 @@ QT_BEGIN_NAMESPACE F(UShort, 36, ushort) \ F(UChar, 37, uchar) \ F(Float, 38, float) \ + F(SChar, 49, signed char) \ #define QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(F)\ F(VoidStar, 31, void*) \ @@ -160,8 +161,7 @@ QT_BEGIN_NAMESPACE F(UChar, -1, uchar, "unsigned char") \ F(LongLong, -1, qlonglong, "long long") \ F(ULongLong, -1, qulonglong, "unsigned long long") \ - F(Char, -1, char, "qint8") \ - F(Char, -1, char, "signed char") \ + F(SChar, -1, signed char, "qint8") \ F(UChar, -1, uchar, "quint8") \ F(Short, -1, short, "qint16") \ F(UShort, -1, ushort, "quint16") \ @@ -205,7 +205,7 @@ public: QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID) FirstCoreType = Bool, - LastCoreType = QJsonDocument, + LastCoreType = SChar, FirstGuiType = QFont, LastGuiType = QPolygonF, FirstWidgetsType = QIcon, @@ -821,7 +821,6 @@ QT_END_NAMESPACE Q_DECLARE_BUILTIN_METATYPE(Name, MetaTypeName) QT_FOR_EACH_STATIC_TYPE(QT_DECLARE_BUILTIN_METATYPE_ITER) -Q_DECLARE_BUILTIN_METATYPE(signed char, Char) #undef QT_DECLARE_BUILTIN_METATYPE_ITER diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 0b3e0418d0..30ca728208 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -152,6 +152,8 @@ static qlonglong qMetaTypeNumber(const QVariant::Private *d) return d->data.ll; case QMetaType::Char: return qlonglong(d->data.c); + case QMetaType::SChar: + return qlonglong(d->data.sc); case QMetaType::Short: return qlonglong(d->data.s); case QMetaType::Long: @@ -199,6 +201,7 @@ static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok) case QVariant::Double: case QVariant::Int: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::Short: case QMetaType::Long: case QMetaType::Float: @@ -232,6 +235,7 @@ static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok) case QVariant::Double: case QVariant::Int: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::Short: case QMetaType::Long: case QMetaType::Float: @@ -289,6 +293,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) *str = QString(*v_cast<QChar>(d)); break; case QMetaType::Char: + case QMetaType::SChar: case QMetaType::UChar: *str = QChar::fromLatin1(d->data.c); break; @@ -350,6 +355,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) case QVariant::Int: case QVariant::LongLong: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::Short: case QMetaType::Long: case QMetaType::Float: @@ -487,6 +493,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) *ba = QByteArray::number(d->data.f, 'g', FLT_DIG); break; case QMetaType::Char: + case QMetaType::SChar: case QMetaType::UChar: *ba = QByteArray(1, d->data.c); break; @@ -535,6 +542,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok); return *ok; } + case QMetaType::SChar: { + signed char s = qConvertToNumber(d, ok); + *static_cast<signed char*>(result) = s; + return *ok; + } case QMetaType::UChar: { *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok); return *ok; @@ -555,6 +567,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) case QVariant::Int: case QVariant::LongLong: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::Short: case QMetaType::Long: case QMetaType::Float: @@ -591,6 +604,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) case QVariant::LongLong: case QVariant::Int: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::Short: case QMetaType::Long: *f = double(qMetaTypeNumber(d)); @@ -626,6 +640,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) case QVariant::LongLong: case QVariant::Int: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::Short: case QMetaType::Long: *f = float(qMetaTypeNumber(d)); @@ -2440,11 +2455,17 @@ static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] = bool QVariant::canConvert(int targetTypeId) const { // TODO Reimplement this function, currently it works but it is a historical mess. - const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type); + uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type); + if (currentType == QMetaType::SChar || currentType == QMetaType::Char) + currentType = QMetaType::UInt; + if (targetTypeId == QMetaType::SChar || currentType == QMetaType::Char) + targetTypeId = QMetaType::UInt; if (uint(targetTypeId) == uint(QMetaType::Float)) targetTypeId = QVariant::Double; + if (currentType == uint(targetTypeId)) return true; + if (targetTypeId < 0 || targetTypeId >= QMetaType::User) return false; @@ -2452,12 +2473,16 @@ bool QVariant::canConvert(int targetTypeId) const if (currentType > int(QMetaType::QUuid) || targetTypeId > int(QMetaType::QUuid)) { switch (uint(targetTypeId)) { case QVariant::Int: - return currentType == QVariant::KeySequence - || currentType == QMetaType::ULong + if (currentType == QVariant::KeySequence) + return true; + // fall through + case QVariant::UInt: + return currentType == QMetaType::ULong || currentType == QMetaType::Long || currentType == QMetaType::UShort || currentType == QMetaType::UChar || currentType == QMetaType::Char + || currentType == QMetaType::SChar || currentType == QMetaType::Short; case QVariant::Image: return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap; @@ -2482,6 +2507,7 @@ bool QVariant::canConvert(int targetTypeId) const return currentType == QVariant::Color || currentType == QVariant::Pixmap; case QMetaType::Long: case QMetaType::Char: + case QMetaType::SChar: case QMetaType::UChar: case QMetaType::ULong: case QMetaType::Short: diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index cd8ac9823d..67cec6875a 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -360,6 +360,7 @@ class Q_CORE_EXPORT QVariant char c; uchar uc; short s; + signed char sc; ushort us; int i; uint u; diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index e9de52b821..e09517678e 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -273,6 +273,7 @@ void tst_QMetaType::qMetaTypeId() QCOMPARE(::qMetaTypeId<char>(), QMetaType::type("char")); QCOMPARE(::qMetaTypeId<uchar>(), QMetaType::type("unsigned char")); QCOMPARE(::qMetaTypeId<signed char>(), QMetaType::type("signed char")); + QVERIFY(::qMetaTypeId<signed char>() != ::qMetaTypeId<char>()); QCOMPARE(::qMetaTypeId<qint8>(), QMetaType::type("qint8")); } @@ -446,6 +447,9 @@ template<> struct TestValueFactory<QMetaType::ULong> { template<> struct TestValueFactory<QMetaType::UShort> { static ushort *create() { return new ushort(0x1234); } }; +template<> struct TestValueFactory<QMetaType::SChar> { + static signed char *create() { return new signed char(-12); } +}; template<> struct TestValueFactory<QMetaType::UChar> { static uchar *create() { return new uchar('u'); } }; @@ -1012,7 +1016,7 @@ void tst_QMetaType::typedefs() { QCOMPARE(QMetaType::type("long long"), int(QMetaType::LongLong)); QCOMPARE(QMetaType::type("unsigned long long"), int(QMetaType::ULongLong)); - QCOMPARE(QMetaType::type("qint8"), int(QMetaType::Char)); + QCOMPARE(QMetaType::type("qint8"), int(QMetaType::SChar)); QCOMPARE(QMetaType::type("quint8"), int(QMetaType::UChar)); QCOMPARE(QMetaType::type("qint16"), int(QMetaType::Short)); QCOMPARE(QMetaType::type("quint16"), int(QMetaType::UShort)); diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 569e448d88..47392a35a5 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -605,9 +605,18 @@ void tst_QVariant::canConvert_data() var = QVariant((uint)1); QTest::newRow("UInt") << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + var = QVariant((int)1); + QTest::newRow("Int") + << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; var = QVariant((qulonglong)1); QTest::newRow("ULongLong") << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + var = QVariant::fromValue('a'); + QTest::newRow("Char") + << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + var = QVariant::fromValue<signed char>(-1); + QTest::newRow("SChar") + << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; #undef N #undef Y @@ -696,6 +705,9 @@ void tst_QVariant::toInt_data() QTest::newRow( "invalid" ) << QVariant() << 0 << false; QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true; + QTest::newRow( "char" ) << QVariant::fromValue('a') << int('a') << true; + signed char signedChar = -13; + QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << -13 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true; QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true; @@ -744,6 +756,9 @@ void tst_QVariant::toUInt_data() QTest::addColumn<bool>("valueOK"); QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true; + QTest::newRow( "char" ) << QVariant::fromValue('a') << uint('a') << true; + signed char signedChar = 12; + QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << uint(12) << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true; QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true; @@ -1710,8 +1725,16 @@ void tst_QVariant::writeToReadFromOldDataStream() void tst_QVariant::checkDataStream() { - QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 49"); - const QByteArray settingsHex("00000031ffffffffff"); + const int typeId = QMetaType::LastCoreType + 1; + QVERIFY(!QMetaType::isRegistered(typeId)); + + QByteArray errorMessage("Trying to construct an instance of an invalid type, type id: "); + errorMessage.append(QString::number(typeId, 10)); + + QTest::ignoreMessage(QtWarningMsg, errorMessage.constData()); + QByteArray settingsHex("000000"); + settingsHex.append(QString::number(typeId, 16)); + settingsHex.append("ffffffffff"); const QByteArray settings = QByteArray::fromHex(settingsHex); QDataStream in(settings); QVariant v; |