From 794150e5bda0c203a5373c3fa2f9785f9941f6dd Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Thu, 14 May 2020 12:59:42 +0200 Subject: [PATCH] QMetaType: Support char16_t and char32_t Change-Id: Ieec6d4bc64967d875ea12b31638aab05bc682ea3 Reviewed-by: Lars Knoll --- src/corelib/kernel/qmetatype.h | 5 +- src/corelib/serialization/qdatastream.cpp | 49 +++++++++++++++++++ src/corelib/serialization/qdatastream.h | 5 ++ .../corelib/kernel/qmetatype/tst_qmetatype.h | 6 +++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 96133c13c5..05fe4450df 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -85,6 +85,8 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId(); F(Long, 32, long) \ F(Short, 33, short) \ F(Char, 34, char) \ + F(Char16, 56, char16_t) \ + F(Char32, 57, char32_t) \ F(ULong, 35, ulong) \ F(UShort, 36, ushort) \ F(UChar, 37, uchar) \ @@ -451,7 +453,7 @@ public: QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID) FirstCoreType = Bool, - LastCoreType = QCborMap, + LastCoreType = Char32, FirstGuiType = QFont, LastGuiType = QColorSpace, FirstWidgetsType = QSizePolicy, @@ -482,6 +484,7 @@ public: Nullptr = 51, QVariantMap = 8, QVariantList = 9, QVariantHash = 28, QCborSimpleType = 52, QCborValue = 53, QCborArray = 54, QCborMap = 55, + Char16 = 56, Char32 = 57, // Gui types QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68, diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp index df286085bc..3026e554bf 100644 --- a/src/corelib/serialization/qdatastream.cpp +++ b/src/corelib/serialization/qdatastream.cpp @@ -1025,6 +1025,31 @@ QDataStream &QDataStream::operator>>(char *&s) return readBytes(s, len); } +/*! + \overload + + Reads a char from the stream into char \a chr. +*/ +QDataStream &QDataStream::operator>>(char16_t &c) +{ + quint16 u; + *this >> u; + c = char16_t(u); + return *this; +} + +/*! + \overload + + Reads a char from the stream into char \a chr. +*/ +QDataStream &QDataStream::operator>>(char32_t &c) +{ + quint32 u; + *this >> u; + c = char32_t(u); + return *this; +} /*! Reads the buffer \a s from the stream and returns a reference to @@ -1337,6 +1362,30 @@ QDataStream &QDataStream::operator<<(const char *s) return *this; } +/*! + \overload + \since 6.0 + + Writes a character, \a c, to the stream. Returns a reference to + the stream +*/ +QDataStream &QDataStream::operator<<(char16_t c) +{ + return *this << qint16(c); +} + +/*! + \overload + \since 6.0 + + Writes a character, \a c, to the stream. Returns a reference to + the stream +*/ +QDataStream &QDataStream::operator<<(char32_t c) +{ + return *this << qint32(c); +} + /*! Writes the length specifier \a len and the buffer \a s to the stream and returns a reference to the stream. diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h index c7b0008039..4568ba3a84 100644 --- a/src/corelib/serialization/qdatastream.h +++ b/src/corelib/serialization/qdatastream.h @@ -163,6 +163,8 @@ public: QDataStream &operator>>(float &f); QDataStream &operator>>(double &f); QDataStream &operator>>(char *&str); + QDataStream &operator>>(char16_t &c); + QDataStream &operator>>(char32_t &c); QDataStream &operator<<(qint8 i); QDataStream &operator<<(quint8 i); @@ -178,6 +180,9 @@ public: QDataStream &operator<<(float f); QDataStream &operator<<(double f); QDataStream &operator<<(const char *str); + QDataStream &operator<<(char16_t c); + QDataStream &operator<<(char32_t c); + QDataStream &readBytes(char *&, uint &len); int readRawData(char *, int len); diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h index bf01fdcfcd..3f00db5519 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h @@ -136,6 +136,12 @@ template<> struct TestValueFactory { template<> struct TestValueFactory { static char *create() { return new char('c'); } }; +template<> struct TestValueFactory { + static char16_t *create() { return new char16_t('c'); } +}; +template<> struct TestValueFactory { + static char32_t *create() { return new char32_t('c'); } +}; template<> struct TestValueFactory { static ulong *create() { return new ulong(ULONG_MAX); } };