2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2013-08-19 14:29:29 +00:00
|
|
|
|
|
|
|
#ifndef LEVEL_H
|
|
|
|
#define LEVEL_H
|
|
|
|
|
2023-02-07 12:52:50 +00:00
|
|
|
#include "character.h"
|
|
|
|
|
2013-08-19 14:29:29 +00:00
|
|
|
#include <QJsonObject>
|
2020-06-22 08:12:38 +00:00
|
|
|
#include <QList>
|
2013-08-19 14:29:29 +00:00
|
|
|
|
2023-02-23 10:58:51 +00:00
|
|
|
QT_FORWARD_DECLARE_CLASS(QTextStream)
|
|
|
|
|
2013-08-19 14:29:29 +00:00
|
|
|
//! [0]
|
|
|
|
class Level
|
|
|
|
{
|
|
|
|
public:
|
2017-08-22 07:52:55 +00:00
|
|
|
Level() = default;
|
2023-02-08 13:03:03 +00:00
|
|
|
explicit Level(const QString &name);
|
2013-08-19 14:29:29 +00:00
|
|
|
|
2017-08-22 07:52:55 +00:00
|
|
|
QString name() const;
|
|
|
|
|
2020-06-22 08:12:38 +00:00
|
|
|
QList<Character> npcs() const;
|
|
|
|
void setNpcs(const QList<Character> &npcs);
|
2013-08-19 14:29:29 +00:00
|
|
|
|
savegame ex.: revamp the way we (de)serialize JSON
JSON, unlike, say, QDataStream, allows building up objects independent
of some central object, and combining them into a QJsonDocument
later. This suggests returning QJsonObjects from a toJson() const
method instead of having the caller supply a QJsonObject. Doing it
this way enables transparent move semantics to kick in, too.
For deserialization, use a fromJson() named constructor for value-like
classes (where identity doesn't matter, only equality). Keep using
read(), too, and add a note to explain when to use which form.
Also, avoid the triple lookup from
if (json.contains("key") && json["key"].isSoughtType())
mFoo = json["key"].toSoughtType();
by using C++17 if-with-initializer and showing the trick with
Undefined never being of isSoughtType():
if (const QJsonValue v = json["key"]; v.isSoughtType())
mFoo = v.toSoughtType();
Adjust the discussion to match the new code, up the copyright years
and rename some qdoc snippet markers from nondescript [0]/[1] to
[toJson]/[fromJson].
Task-number: QTBUG-108857
Pick-to: 6.5 6.4 6.2
Change-Id: Icaa14acc7464fef00a59534679d710252e921383
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2023-02-07 12:52:50 +00:00
|
|
|
static Level fromJson(const QJsonObject &json);
|
|
|
|
QJsonObject toJson() const;
|
2017-08-22 07:52:55 +00:00
|
|
|
|
2023-02-23 10:58:51 +00:00
|
|
|
void print(QTextStream &s, int indentation = 0) const;
|
2013-08-19 14:29:29 +00:00
|
|
|
private:
|
2017-08-22 07:52:55 +00:00
|
|
|
QString mName;
|
2020-06-22 08:12:38 +00:00
|
|
|
QList<Character> mNpcs;
|
2013-08-19 14:29:29 +00:00
|
|
|
};
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
#endif // LEVEL_H
|