Eliminate duplicate overrides of Converter methods in example

The Out-only converters shouldn't need to implement loadFile(), as it
shouldn't be called - a converter is not used for input unless it says
it supports input. At the same time, provide the "ground state"
implementations for optionsHelp(), outputOptions() and probeFile() to
save the trivial implementations the need to duplicate one another.

In the process, make the handling of loadFile()'s outputConverter more
consistent among those that do implement it.  Always set
outputConverter if it's initially null (the caller does assert this).

Pick-to: 6.6 6.5
Task-number: QTBUG-111228
Change-Id: I856d12c791d1f8e0accdb7dd1412d493117b2302
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2023-09-04 19:21:58 +02:00
parent 8b8fc42f2c
commit 1022b0d111
12 changed files with 21 additions and 82 deletions

View File

@ -142,20 +142,6 @@ const char *CborDiagnosticDumper::optionsHelp() const
return diagnosticHelp;
}
bool CborDiagnosticDumper::probeFile(QIODevice *f) const
{
Q_UNUSED(f);
return false;
}
QVariant CborDiagnosticDumper::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
Q_UNREACHABLE();
Q_UNUSED(f);
Q_UNUSED(outputConverter);
return QVariant();
}
void CborDiagnosticDumper::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{

View File

@ -14,8 +14,6 @@ public:
Directions 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;
};

View File

@ -23,3 +23,20 @@ const QList<const Converter *> &Converter::allConverters()
{
return converters();
}
// Some virtual methods that Converter classes needn't override, when not relevant:
Converter::Options Converter::outputOptions() const { return {}; }
const char *Converter::optionsHelp() const { return nullptr; }
bool Converter::probeFile(QIODevice *) const { return false; }
// The virtual method they should override if they claim to support In:
QVariant Converter::loadFile(QIODevice *, const Converter *&outputConverter) const
{
Q_ASSERT(!directions().testFlag(Converter::Direction::In));
// For those that don't, this should never be called.
Q_UNIMPLEMENTED();
// But every implementation should at least do this:
if (!outputConverter)
outputConverter = this;
return QVariant();
}

View File

@ -29,10 +29,10 @@ public:
virtual QString name() const = 0;
virtual Directions 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 Options outputOptions() const;
virtual const char *optionsHelp() const;
virtual bool probeFile(QIODevice *f) const;
virtual QVariant loadFile(QIODevice *f, const Converter *&outputConverter) const;
virtual void saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const = 0;
};

View File

@ -59,25 +59,6 @@ Converter::Options DebugTextDumper::outputOptions() const
return SupportsArbitraryMapKeys;
}
const char *DebugTextDumper::optionsHelp() const
{
return nullptr;
}
bool DebugTextDumper::probeFile(QIODevice *f) const
{
Q_UNUSED(f);
return false;
}
QVariant DebugTextDumper::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
Q_UNREACHABLE();
Q_UNUSED(f);
Q_UNUSED(outputConverter);
return QVariant();
}
void DebugTextDumper::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{

View File

@ -13,9 +13,6 @@ public:
QString name() const override;
Directions 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;
};

View File

@ -33,11 +33,6 @@ Converter::Directions JsonConverter::directions() const
return Direction::InOut;
}
Converter::Options JsonConverter::outputOptions() const
{
return {};
}
const char *JsonConverter::optionsHelp() const
{
return jsonOptionHelp;

View File

@ -12,7 +12,6 @@ class JsonConverter : public Converter
public:
QString name() const override;
Directions 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;

View File

@ -26,25 +26,6 @@ Converter::Options NullConverter::outputOptions() const
return SupportsArbitraryMapKeys;
}
const char *NullConverter::optionsHelp() const
{
return nullptr;
}
bool NullConverter::probeFile(QIODevice *f) const
{
Q_UNUSED(f);
return false;
}
QVariant NullConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
{
Q_UNUSED(f);
Q_UNUSED(outputConverter);
outputConverter = this;
return QVariant();
}
void NullConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
{

View File

@ -13,9 +13,6 @@ public:
QString name() const override;
Directions 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;
};

View File

@ -54,16 +54,6 @@ Converter::Directions TextConverter::directions() const
return Direction::InOut;
}
Converter::Options TextConverter::outputOptions() const
{
return {};
}
const char *TextConverter::optionsHelp() const
{
return nullptr;
}
bool TextConverter::probeFile(QIODevice *f) const
{
if (QFile *file = qobject_cast<QFile *>(f))

View File

@ -12,8 +12,6 @@ class TextConverter : public Converter
public:
QString name() const override;
Directions 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,