From 6f1e53943d463978d2a3ae226fdeea78b06df34c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 23 Feb 2023 11:58:51 +0100 Subject: [PATCH] savegame ex.: revamp the way print() works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Basically, instead of re-creating QTextStreams all the time, create it once, in main(), and then pass it to print() alongside the int indentation. Also fix a hard-coded indentation value that should have been relative to the caller's indentation level. Pick-to: 6.5 6.2 Task-number: QTBUG-108857 Change-Id: I811447295c9c3fdef23f61aff31ebe82941eb3b4 Reviewed-by: Edward Welbourne Reviewed-by: MÃ¥rten Nordheim --- examples/corelib/serialization/savegame/character.cpp | 10 +++++----- examples/corelib/serialization/savegame/character.h | 4 +++- .../serialization/savegame/doc/src/savegame.qdoc | 4 +++- examples/corelib/serialization/savegame/game.cpp | 10 +++++----- examples/corelib/serialization/savegame/game.h | 4 +++- examples/corelib/serialization/savegame/level.cpp | 8 ++++---- examples/corelib/serialization/savegame/level.h | 4 +++- examples/corelib/serialization/savegame/main.cpp | 5 +++-- 8 files changed, 29 insertions(+), 20 deletions(-) diff --git a/examples/corelib/serialization/savegame/character.cpp b/examples/corelib/serialization/savegame/character.cpp index 910e7e9e7a..039aa1fa94 100644 --- a/examples/corelib/serialization/savegame/character.cpp +++ b/examples/corelib/serialization/savegame/character.cpp @@ -77,12 +77,12 @@ QJsonObject Character::toJson() const } //! [toJson] -void Character::print(int indentation) const +void Character::print(QTextStream &s, int indentation) const { const QString indent(indentation * 2, ' '); - QTextStream(stdout) << indent << "Name:\t" << mName << "\n"; - QTextStream(stdout) << indent << "Level:\t" << mLevel << "\n"; + const QString className = QMetaEnum::fromType().valueToKey(mClassType); - QString className = QMetaEnum::fromType().valueToKey(mClassType); - QTextStream(stdout) << indent << "Class:\t" << className << "\n"; + s << indent << "Name:\t" << mName << "\n" + << indent << "Level:\t" << mLevel << "\n" + << indent << "Class:\t" << className << "\n"; } diff --git a/examples/corelib/serialization/savegame/character.h b/examples/corelib/serialization/savegame/character.h index d91bc8519b..cffa5fa659 100644 --- a/examples/corelib/serialization/savegame/character.h +++ b/examples/corelib/serialization/savegame/character.h @@ -8,6 +8,8 @@ #include #include +QT_FORWARD_DECLARE_CLASS(QTextStream) + //! [0] class Character { @@ -34,7 +36,7 @@ public: static Character fromJson(const QJsonObject &json); QJsonObject toJson() const; - void print(int indentation = 0) const; + void print(QTextStream &s, int indentation = 0) const; private: QString mName; int mLevel = 0; diff --git a/examples/corelib/serialization/savegame/doc/src/savegame.qdoc b/examples/corelib/serialization/savegame/doc/src/savegame.qdoc index b1aa599e79..f78ca0ce3b 100644 --- a/examples/corelib/serialization/savegame/doc/src/savegame.qdoc +++ b/examples/corelib/serialization/savegame/doc/src/savegame.qdoc @@ -35,7 +35,9 @@ subclasses, for example), other patterns may be more suitable. See the \l{xml/dombookmarks} and \l{xml/streambookmarks} examples for XML, and the implementation of \l QListWidgetItem::read() and \l QListWidgetItem::write() - for idiomatic QDataStream serialization. + for idiomatic QDataStream serialization. The \c{print()} functions in this example + are good examples of QTextStream serialization, even though they, of course, lack + the deserialization side. \snippet serialization/savegame/character.h 0 diff --git a/examples/corelib/serialization/savegame/game.cpp b/examples/corelib/serialization/savegame/game.cpp index 3140178f88..85a2619b42 100644 --- a/examples/corelib/serialization/savegame/game.cpp +++ b/examples/corelib/serialization/savegame/game.cpp @@ -140,13 +140,13 @@ QJsonObject Game::toJson() const } //! [toJson] -void Game::print(int indentation) const +void Game::print(QTextStream &s, int indentation) const { const QString indent(indentation * 2, ' '); - QTextStream(stdout) << indent << "Player\n"; - mPlayer.print(indentation + 1); + s << indent << "Player\n"; + mPlayer.print(s, indentation + 1); - QTextStream(stdout) << indent << "Levels\n"; + s << indent << "Levels\n"; for (const Level &level : mLevels) - level.print(indentation + 1); + level.print(s, indentation + 1); } diff --git a/examples/corelib/serialization/savegame/game.h b/examples/corelib/serialization/savegame/game.h index 4c72ea426e..2d7630c7df 100644 --- a/examples/corelib/serialization/savegame/game.h +++ b/examples/corelib/serialization/savegame/game.h @@ -10,6 +10,8 @@ #include #include +QT_FORWARD_DECLARE_CLASS(QTextStream) + //! [0] class Game { @@ -28,7 +30,7 @@ public: void read(const QJsonObject &json); QJsonObject toJson() const; - void print(int indentation = 0) const; + void print(QTextStream &s, int indentation = 0) const; private: Character mPlayer; QList mLevels; diff --git a/examples/corelib/serialization/savegame/level.cpp b/examples/corelib/serialization/savegame/level.cpp index 5a6137715a..489a25e204 100644 --- a/examples/corelib/serialization/savegame/level.cpp +++ b/examples/corelib/serialization/savegame/level.cpp @@ -57,12 +57,12 @@ QJsonObject Level::toJson() const } //! [toJson] -void Level::print(int indentation) const +void Level::print(QTextStream &s, int indentation) const { const QString indent(indentation * 2, ' '); - QTextStream(stdout) << indent << "Name:\t" << mName << "\n"; - QTextStream(stdout) << indent << "NPCs:\n"; + s << indent << "Name:\t" << mName << "\n" + << indent << "NPCs:\n"; for (const Character &character : mNpcs) - character.print(2); + character.print(s, indentation + 1); } diff --git a/examples/corelib/serialization/savegame/level.h b/examples/corelib/serialization/savegame/level.h index ce39a611b4..ad8d0fd593 100644 --- a/examples/corelib/serialization/savegame/level.h +++ b/examples/corelib/serialization/savegame/level.h @@ -9,6 +9,8 @@ #include #include +QT_FORWARD_DECLARE_CLASS(QTextStream) + //! [0] class Level { @@ -24,7 +26,7 @@ public: static Level fromJson(const QJsonObject &json); QJsonObject toJson() const; - void print(int indentation = 0) const; + void print(QTextStream &s, int indentation = 0) const; private: QString mName; QList mNpcs; diff --git a/examples/corelib/serialization/savegame/main.cpp b/examples/corelib/serialization/savegame/main.cpp index 79f928cda9..3fc0f3af10 100644 --- a/examples/corelib/serialization/savegame/main.cpp +++ b/examples/corelib/serialization/savegame/main.cpp @@ -29,8 +29,9 @@ int main(int argc, char *argv[]) // Game is played; changes are made... //! [0] //! [1] - QTextStream(stdout) << "Game ended in the following state:\n"; - game.print(); + QTextStream s(stdout); + s << "Game ended in the following state:\n"; + game.print(s); if (!game.saveGame(json ? Game::Json : Game::Binary)) return 1;