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>
This commit is contained in:
Giuseppe D'Angelo 2024-02-16 13:58:04 +01:00
parent a43d86fe1c
commit 3823e310e3
3 changed files with 19 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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.")