Serialization converter example: const-ify the Converter type

Its methods act on their parameters without changing the converter, so
can all be const. Its instances thus have no non-const members to
exercise, so can always be const.

Pick-to: 6.6 6.5
Task-number: QTBUG-111228
Change-Id: Ifcdb2f2159c2cfcd7998dd118aa327a32d299ccf
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Edward Welbourne 2023-09-01 13:34:26 +02:00
parent eaebb5c4d2
commit 77fdd21cbf
16 changed files with 143 additions and 129 deletions

View File

@ -120,33 +120,33 @@ static QCborValue convertFromVariant(const QVariant &v, TrimFloatingPoint fpTrim
}
//! [1]
QString CborDiagnosticDumper::name()
QString CborDiagnosticDumper::name() const
{
return "cbor-dump"_L1;
}
Converter::Direction CborDiagnosticDumper::directions()
Converter::Direction CborDiagnosticDumper::directions() const
{
return Out;
}
Converter::Options CborDiagnosticDumper::outputOptions()
Converter::Options CborDiagnosticDumper::outputOptions() const
{
return SupportsArbitraryMapKeys;
}
const char *CborDiagnosticDumper::optionsHelp()
const char *CborDiagnosticDumper::optionsHelp() const
{
return diagnosticHelp;
}
bool CborDiagnosticDumper::probeFile(QIODevice *f)
bool CborDiagnosticDumper::probeFile(QIODevice *f) const
{
Q_UNUSED(f);
return false;
}
QVariant CborDiagnosticDumper::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant CborDiagnosticDumper::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
Q_UNREACHABLE();
Q_UNUSED(f);
@ -155,7 +155,7 @@ QVariant CborDiagnosticDumper::loadFile(QIODevice *f, Converter *&outputConverte
}
void CborDiagnosticDumper::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options)
const QStringList &options) const
{
QCborValue::DiagnosticNotationOptions opts = QCborValue::LineWrapped;
for (const QString &s : options) {
@ -192,27 +192,27 @@ CborConverter::CborConverter()
qRegisterMetaType<QCborTag>();
}
QString CborConverter::name()
QString CborConverter::name() const
{
return "cbor";
}
Converter::Direction CborConverter::directions()
Converter::Direction CborConverter::directions() const
{
return InOut;
}
Converter::Options CborConverter::outputOptions()
Converter::Options CborConverter::outputOptions() const
{
return SupportsArbitraryMapKeys;
}
const char *CborConverter::optionsHelp()
const char *CborConverter::optionsHelp() const
{
return cborOptionHelp;
}
bool CborConverter::probeFile(QIODevice *f)
bool CborConverter::probeFile(QIODevice *f) const
{
if (QFile *file = qobject_cast<QFile *>(f)) {
if (file->fileName().endsWith(".cbor"_L1))
@ -222,7 +222,7 @@ bool CborConverter::probeFile(QIODevice *f)
}
//! [2]
QVariant CborConverter::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant CborConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
const char *ptr = nullptr;
if (auto file = qobject_cast<QFile *>(f))
@ -258,7 +258,7 @@ QVariant CborConverter::loadFile(QIODevice *f, Converter *&outputConverter)
}
//! [2]
//! [3]
void CborConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
void CborConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) const
{
//! [3]
bool useSignature = true;

View File

@ -10,13 +10,14 @@ class CborDiagnosticDumper : public Converter
{
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
class CborConverter : public Converter
@ -26,13 +27,14 @@ public:
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // CBORCONVERTER_H

View File

@ -39,13 +39,14 @@ public:
virtual ~Converter() = 0;
virtual QString name() = 0;
virtual Direction directions() = 0;
virtual Options outputOptions() = 0;
virtual const char *optionsHelp() = 0;
virtual bool probeFile(QIODevice *f) = 0;
virtual QVariant loadFile(QIODevice *f, Converter *&outputConverter) = 0;
virtual void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) = 0;
virtual QString name() const = 0;
virtual Direction directions() const = 0;
virtual Options outputOptions() const = 0;
virtual const char *optionsHelp() const = 0;
virtual bool probeFile(QIODevice *f) const = 0;
virtual QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const = 0;
virtual void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const = 0;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Converter::Options)

View File

@ -48,32 +48,32 @@ DataStreamConverter::DataStreamConverter()
qRegisterMetaType<VariantOrderedMap>();
}
QString DataStreamConverter::name()
QString DataStreamConverter::name() const
{
return "datastream"_L1;
}
Converter::Direction DataStreamConverter::directions()
Converter::Direction DataStreamConverter::directions() const
{
return InOut;
}
Converter::Options DataStreamConverter::outputOptions()
Converter::Options DataStreamConverter::outputOptions() const
{
return SupportsArbitraryMapKeys;
}
const char *DataStreamConverter::optionsHelp()
const char *DataStreamConverter::optionsHelp() const
{
return dataStreamOptionHelp;
}
bool DataStreamConverter::probeFile(QIODevice *f)
bool DataStreamConverter::probeFile(QIODevice *f) const
{
return f->isReadable() && f->peek(sizeof(signature) - 1) == signature;
}
QVariant DataStreamConverter::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant DataStreamConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
if (!outputConverter)
outputConverter = &debugTextDumper;
@ -97,7 +97,7 @@ QVariant DataStreamConverter::loadFile(QIODevice *f, Converter *&outputConverter
}
void DataStreamConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options)
const QStringList &options) const
{
QDataStream::Version version = QDataStream::Qt_6_0;
auto order = QDataStream::ByteOrder(QSysInfo::ByteOrder);

View File

@ -13,13 +13,14 @@ public:
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // DATASTREAMCONVERTER_H

View File

@ -43,33 +43,33 @@ static QString dumpVariant(const QVariant &v, const QString &indent = "\n"_L1)
return result;
}
QString DebugTextDumper::name()
QString DebugTextDumper::name() const
{
return "debugtext-dump"_L1;
}
Converter::Direction DebugTextDumper::directions()
Converter::Direction DebugTextDumper::directions() const
{
return Out;
}
Converter::Options DebugTextDumper::outputOptions()
Converter::Options DebugTextDumper::outputOptions() const
{
return SupportsArbitraryMapKeys;
}
const char *DebugTextDumper::optionsHelp()
const char *DebugTextDumper::optionsHelp() const
{
return nullptr;
}
bool DebugTextDumper::probeFile(QIODevice *f)
bool DebugTextDumper::probeFile(QIODevice *f) const
{
Q_UNUSED(f);
return false;
}
QVariant DebugTextDumper::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant DebugTextDumper::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
Q_UNREACHABLE();
Q_UNUSED(f);
@ -77,7 +77,8 @@ QVariant DebugTextDumper::loadFile(QIODevice *f, Converter *&outputConverter)
return QVariant();
}
void DebugTextDumper::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
void DebugTextDumper::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{
Q_UNUSED(options);
QString s = dumpVariant(contents);

View File

@ -10,13 +10,14 @@ class DebugTextDumper : public Converter
{
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // DEBUGTEXTDUMPER_H

View File

@ -25,27 +25,27 @@ static QJsonDocument convertFromVariant(const QVariant &v)
return doc;
}
QString JsonConverter::name()
QString JsonConverter::name() const
{
return "json"_L1;
}
Converter::Direction JsonConverter::directions()
Converter::Direction JsonConverter::directions() const
{
return InOut;
}
Converter::Options JsonConverter::outputOptions()
Converter::Options JsonConverter::outputOptions() const
{
return {};
}
const char *JsonConverter::optionsHelp()
const char *JsonConverter::optionsHelp() const
{
return jsonOptionHelp;
}
bool JsonConverter::probeFile(QIODevice *f)
bool JsonConverter::probeFile(QIODevice *f) const
{
if (QFile *file = qobject_cast<QFile *>(f)) {
if (file->fileName().endsWith(".json"_L1))
@ -59,7 +59,7 @@ bool JsonConverter::probeFile(QIODevice *f)
return false;
}
QVariant JsonConverter::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant JsonConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
if (!outputConverter)
outputConverter = this;
@ -84,7 +84,8 @@ QVariant JsonConverter::loadFile(QIODevice *f, Converter *&outputConverter)
return doc.toVariant();
}
void JsonConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
void JsonConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
for (const QString &s : options) {

View File

@ -10,13 +10,14 @@ class JsonConverter : public Converter
{
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // JSONCONVERTER_H

View File

@ -13,12 +13,12 @@
using namespace Qt::StringLiterals;
static QList<Converter *> *availableConverters;
static QList<const Converter *> *availableConverters;
Converter::Converter()
{
if (!availableConverters)
availableConverters = new QList<Converter *>;
availableConverters = new QList<const Converter *>;
availableConverters->append(this);
}
@ -33,7 +33,7 @@ int main(int argc, char *argv[])
QStringList inputFormats;
QStringList outputFormats;
for (Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : std::as_const(*availableConverters)) {
auto direction = conv->directions();
QString name = conv->name();
if (direction & Converter::In)
@ -86,7 +86,7 @@ int main(int argc, char *argv[])
if (parser.isSet(formatOptionsOption)) {
QString format = parser.value(formatOptionsOption);
for (Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : std::as_const(*availableConverters)) {
if (conv->name() == format) {
const char *help = conv->optionsHelp();
if (help) {
@ -103,10 +103,10 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
Converter *inconv = nullptr;
const Converter *inconv = nullptr;
QString format = parser.value(inputFormatOption);
if (format != "auto"_L1) {
for (Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : std::as_const(*availableConverters)) {
if (conv->name() == format) {
inconv = conv;
break;
@ -119,10 +119,10 @@ int main(int argc, char *argv[])
}
}
Converter *outconv = nullptr;
const Converter *outconv = nullptr;
format = parser.value(outputFormatOption);
if (format != "auto"_L1) {
for (Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : std::as_const(*availableConverters)) {
if (conv->name() == format) {
outconv = conv;
break;
@ -161,7 +161,7 @@ int main(int argc, char *argv[])
if (!inconv) {
// probe the input to find a file format
for (Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : std::as_const(*availableConverters)) {
if (conv->directions() & Converter::In && conv->probeFile(&input)) {
inconv = conv;
break;
@ -176,7 +176,7 @@ int main(int argc, char *argv[])
if (!outconv) {
// probe the output to find a file format
for (Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : std::as_const(*availableConverters)) {
if (conv->directions() & Converter::Out && conv->probeFile(&output)) {
outconv = conv;
break;

View File

@ -8,33 +8,33 @@ using namespace Qt::StringLiterals;
static NullConverter nullConverter;
Converter *Converter::null = &nullConverter;
QString NullConverter::name()
QString NullConverter::name() const
{
return "null"_L1;
}
Converter::Direction NullConverter::directions()
Converter::Direction NullConverter::directions() const
{
return Out;
}
Converter::Options NullConverter::outputOptions()
Converter::Options NullConverter::outputOptions() const
{
return SupportsArbitraryMapKeys;
}
const char *NullConverter::optionsHelp()
const char *NullConverter::optionsHelp() const
{
return nullptr;
}
bool NullConverter::probeFile(QIODevice *f)
bool NullConverter::probeFile(QIODevice *f) const
{
Q_UNUSED(f);
return false;
}
QVariant NullConverter::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant NullConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
Q_UNUSED(f);
Q_UNUSED(outputConverter);
@ -42,7 +42,8 @@ QVariant NullConverter::loadFile(QIODevice *f, Converter *&outputConverter)
return QVariant();
}
void NullConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
void NullConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{
if (!options.isEmpty()) {
fprintf(stderr, "Unknown option '%s' to null output. This format has no options.\n",

View File

@ -10,13 +10,14 @@ class NullConverter : public Converter
{
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // NULLCONVERTER_H

View File

@ -44,34 +44,34 @@ static void dumpVariant(QTextStream &out, const QVariant &v)
}
}
QString TextConverter::name()
QString TextConverter::name() const
{
return "text"_L1;
}
Converter::Direction TextConverter::directions()
Converter::Direction TextConverter::directions() const
{
return InOut;
}
Converter::Options TextConverter::outputOptions()
Converter::Options TextConverter::outputOptions() const
{
return {};
}
const char *TextConverter::optionsHelp()
const char *TextConverter::optionsHelp() const
{
return nullptr;
}
bool TextConverter::probeFile(QIODevice *f)
bool TextConverter::probeFile(QIODevice *f) const
{
if (QFile *file = qobject_cast<QFile *>(f))
return file->fileName().endsWith(".txt"_L1);
return false;
}
QVariant TextConverter::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant TextConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
if (!outputConverter)
outputConverter = this;
@ -94,7 +94,8 @@ QVariant TextConverter::loadFile(QIODevice *f, Converter *&outputConverter)
return list;
}
void TextConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
void TextConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{
if (!options.isEmpty()) {
fprintf(stderr, "Unknown option '%s' to text output. This format has no options.\n",

View File

@ -10,13 +10,14 @@ class TextConverter : public Converter
{
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // TEXTCONVERTER_H

View File

@ -396,27 +396,27 @@ static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
}
}
QString XmlConverter::name()
QString XmlConverter::name() const
{
return "xml"_L1;
}
Converter::Direction XmlConverter::directions()
Converter::Direction XmlConverter::directions() const
{
return InOut;
}
Converter::Options XmlConverter::outputOptions()
Converter::Options XmlConverter::outputOptions() const
{
return SupportsArbitraryMapKeys;
}
const char *XmlConverter::optionsHelp()
const char *XmlConverter::optionsHelp() const
{
return xmlOptionHelp;
}
bool XmlConverter::probeFile(QIODevice *f)
bool XmlConverter::probeFile(QIODevice *f) const
{
if (QFile *file = qobject_cast<QFile *>(f)) {
if (file->fileName().endsWith(".xml"_L1))
@ -426,7 +426,7 @@ bool XmlConverter::probeFile(QIODevice *f)
return f->isReadable() && f->peek(5) == "<?xml";
}
QVariant XmlConverter::loadFile(QIODevice *f, Converter *&outputConverter)
QVariant XmlConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
if (!outputConverter)
outputConverter = this;
@ -442,7 +442,8 @@ QVariant XmlConverter::loadFile(QIODevice *f, Converter *&outputConverter)
return v;
}
void XmlConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
void XmlConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{
bool compact = false;
for (const QString &s : options) {

View File

@ -10,13 +10,14 @@ class XmlConverter : public Converter
{
// Converter interface
public:
QString name() override;
Direction directions() override;
Options outputOptions() override;
const char *optionsHelp() override;
bool probeFile(QIODevice *f) override;
QVariant loadFile(QIODevice *f, Converter *&outputConverter) override;
void saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) override;
QString name() const override;
Direction directions() const override;
Options outputOptions() const override;
const char *optionsHelp() const override;
bool probeFile(QIODevice *f) const override;
QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const override;
void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const override;
};
#endif // XMLCONVERTER_H