summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2024-02-16 13:58:04 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2024-02-20 16:27:20 +0100
commit3823e310e39426043dc7f0529a6fba33fe4d49f0 (patch)
tree25b0648cb3f043c966966882b13439b0dccc24fb
parenta43d86fe1c0bc9d352f67c134a9ee5f754aea5e6 (diff)
QDataStream: reimplement and constrain operator<<(bool)
Instead of offering it as a plain overload, make it a template and constrain the argument to be precisely bool. This removes the danger of accidentally streaming things that are convertible to bool, such as pointers, by, indeed, converting them to bool. This allows us to remove the deleted overloads for pointers to objects and pointers to members. The existing operator<<(bool) is exported, hence I moved it into removed_api.cpp. Since the implementation required private QDataStream APIs, I've just "inlined" the implementation that simply routed through the operator<<(qint8) overload. Change-Id: I3c0a9811bf5c9e734e28514b37bcaaddb09ada25 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/compat/removed_api.cpp5
-rw-r--r--src/corelib/serialization/qdatastream.cpp11
-rw-r--r--src/corelib/serialization/qdatastream.h14
3 files changed, 19 insertions, 11 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp
index 6aa0d1cee9..0553d32807 100644
--- a/src/corelib/compat/removed_api.cpp
+++ b/src/corelib/compat/removed_api.cpp
@@ -919,6 +919,11 @@ QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode mode)
#include "qdatastream.h" // inlined API
+QDataStream &QDataStream::operator<<(bool i)
+{
+ return (*this << qint8(i));
+}
+
#include "qdir.h" // inlined API
bool QDir::operator==(const QDir &dir) const
diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp
index 29fe997801..14bbae53f4 100644
--- a/src/corelib/serialization/qdatastream.cpp
+++ b/src/corelib/serialization/qdatastream.cpp
@@ -1267,18 +1267,13 @@ QDataStream &QDataStream::operator<<(qint64 i)
*/
/*!
+ \fn QDataStream &QDataStream::operator<<(bool i)
+ \overload
+
Writes a boolean value, \a i, to the stream. Returns a reference
to the stream.
*/
-QDataStream &QDataStream::operator<<(bool i)
-{
- CHECK_STREAM_WRITE_PRECOND(*this)
- if (!dev->putChar(qint8(i)))
- q_status = WriteFailed;
- return *this;
-}
-
/*!
\overload
diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h
index 616f6330d9..5aa5cc28cd 100644
--- a/src/corelib/serialization/qdatastream.h
+++ b/src/corelib/serialization/qdatastream.h
@@ -167,7 +167,18 @@ public:
QDataStream &operator<<(qint64 i);
QDataStream &operator<<(quint64 i);
QDataStream &operator<<(std::nullptr_t) { return *this; }
+#if QT_CORE_REMOVED_SINCE(6, 8) || defined(Q_QDOC)
QDataStream &operator<<(bool i);
+#endif
+#if !defined(Q_QDOC)
+ // Disable implicit conversions to bool (e.g. for pointers)
+ template <typename T,
+ std::enable_if_t<std::is_same_v<std::remove_cv_t<T>, bool>, bool> = true>
+ QDataStream &operator<<(T i)
+ {
+ return (*this << qint8(i));
+ }
+#endif
#if QT_CORE_REMOVED_SINCE(6, 3)
QDataStream &operator<<(qfloat16 f);
#endif
@@ -176,9 +187,6 @@ public:
QDataStream &operator<<(const char *str);
QDataStream &operator<<(char16_t c);
QDataStream &operator<<(char32_t c);
- QDataStream &operator<<(const volatile void *) = delete;
- template <typename T, typename C>
- QDataStream &operator<<(T C::*) = delete;
#if QT_DEPRECATED_SINCE(6, 11)
QT_DEPRECATED_VERSION_X_6_11("Use an overload that takes qint64 length.")