Skip to content

Commit

Permalink
[QTextStream] Add operator bool() to QTextStream class
Browse files Browse the repository at this point in the history
- Add operator bool() that checks whether the associated stream has
  no error status. This operator makes it possible to use streams
  and functions that return references to streams as loop conditions:
    `while (stream >> text)`.
- Add testcase for testing the new operator.
- Update existing testcases that use or can use the status of stream
  operations.

[ChangeLog][QtCore][QTextStream] Added implicit conversion to bool,
returning `status() == Ok`.

Task-number: QTBUG-52189
Change-Id: I21cbe02ec194078304bcfe606a6f2f572dd09c84
Reviewed-by: Marc Mutz <[email protected]>
  • Loading branch information
qt-tatiana authored and marcmutz committed Dec 27, 2024
1 parent 6e8274a commit cfd3c84
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 173 deletions.
7 changes: 7 additions & 0 deletions src/corelib/serialization/qtextstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,13 @@ QTextStream &QTextStream::operator<<(const void *ptr)
return *this;
}

/*!
\fn QTextStream::operator bool() const
\since 6.10
Returns whether this stream has no errors (status() == \l{OK}).
*/

namespace Qt {

/*!
Expand Down
2 changes: 2 additions & 0 deletions src/corelib/serialization/qtextstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class Q_CORE_EXPORT QTextStream QT_TEXT_STREAM_FINAL : public QIODeviceBase
QTextStream &operator<<(const char *c);
QTextStream &operator<<(const void *ptr);

explicit operator bool() const noexcept { return status() == Ok; }

private:
Q_DISABLE_COPY(QTextStream)
friend class QDebugStateSaverPrivate;
Expand Down
Loading

0 comments on commit cfd3c84

Please sign in to comment.