From 795a54ff9688697c033fc5d522f4c50c707d5924 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 9 Mar 2017 13:54:56 +0100 Subject: QDataStream: add operator<< and >> for std::nullptr_t std::nullptr_t is nullary: it accepts only one value, nullptr. So we don't need to read or write anything. This commit simply adds the two operators that allow generic code to operate on std::nullptr_t if required. This commit also adds the actual use to QMetaType::load/save, even though there's no change in behavior. [ChangeLog][QtCore][QDataStream] Added operator<< and operator>> overloads that take std::nullptr_t, to facilitate generic code. Change-Id: Iae839f6a131a4f0784bffffd14aa37e7f62d2740 Reviewed-by: Marc Mutz Reviewed-by: Lars Knoll --- src/corelib/io/qdatastream.cpp | 20 ++++++++++++++++++++ src/corelib/io/qdatastream.h | 2 ++ src/corelib/kernel/qmetatype.cpp | 2 ++ 3 files changed, 24 insertions(+) (limited to 'src/corelib') diff --git a/src/corelib/io/qdatastream.cpp b/src/corelib/io/qdatastream.cpp index 2369fe4726..9a42fb4a37 100644 --- a/src/corelib/io/qdatastream.cpp +++ b/src/corelib/io/qdatastream.cpp @@ -770,6 +770,17 @@ int QDataStream::readBlock(char *data, int len) return readResult; } +/*! + \fn QDataStream &QDataStream::operator>>(std::nullptr &ptr) + \since 5.9 + \overload + + Simulates reading a \c{std::nullptr_t} from the stream into \a ptr and + returns a reference to the stream. This function does not actually read + anything from the stream, as \c{std::nullptr_t} values are stored as 0 + bytes. +*/ + /*! \fn QDataStream &QDataStream::operator>>(quint8 &i) \overload @@ -1084,6 +1095,15 @@ int QDataStream::readRawData(char *s, int len) QDataStream write functions *****************************************************************************/ +/*! + \fn QDataStream &QDataStream::operator<<(std::nullptr ptr) + \since 5.9 + \overload + + Simulates writing a \c{std::nullptr_t}, \a ptr, to the stream and returns a + reference to the stream. This function does not actually write anything to + the stream, as \c{std::nullptr_t} values are stored as 0 bytes. +*/ /*! \fn QDataStream &QDataStream::operator<<(quint8 i) diff --git a/src/corelib/io/qdatastream.h b/src/corelib/io/qdatastream.h index db1bbfbd63..575607e147 100644 --- a/src/corelib/io/qdatastream.h +++ b/src/corelib/io/qdatastream.h @@ -152,6 +152,7 @@ public: QDataStream &operator>>(quint32 &i); QDataStream &operator>>(qint64 &i); QDataStream &operator>>(quint64 &i); + QDataStream &operator>>(std::nullptr_t &ptr) { ptr = nullptr; return *this; } QDataStream &operator>>(bool &i); QDataStream &operator>>(qfloat16 &f); @@ -167,6 +168,7 @@ public: QDataStream &operator<<(quint32 i); QDataStream &operator<<(qint64 i); QDataStream &operator<<(quint64 i); + QDataStream &operator<<(std::nullptr_t) { return *this; } QDataStream &operator<<(bool i); QDataStream &operator<<(qfloat16 f); QDataStream &operator<<(float f); diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index fc8d7dcfea..b75f2ad9dc 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -1350,6 +1350,7 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data) case QMetaType::QJsonDocument: return false; case QMetaType::Nullptr: + stream << *static_cast(data); return true; case QMetaType::Long: stream << qlonglong(*static_cast(data)); @@ -1573,6 +1574,7 @@ bool QMetaType::load(QDataStream &stream, int type, void *data) case QMetaType::QJsonDocument: return false; case QMetaType::Nullptr: + stream >> *static_cast(data); return true; case QMetaType::Long: { qlonglong l; -- cgit v1.2.3