Break out the list of available converters to a static method

Mostly to pave the way for moving the class code to a file of its own,
but this incidentally saves the need to std::as_const() every use.
Moving the underlying object to a local static of a private method
also saves the need for heap allocation (which was leaked).

Pick-to: 6.6 6.5
Task-number: QTBUG-111228
Change-Id: I30f4bf3c46d39e04d0ac4e3e9ba431945ebb9193
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
This commit is contained in:
Edward Welbourne 2023-10-31 14:58:10 +01:00
parent 0b1670134b
commit d57a78657e
2 changed files with 20 additions and 10 deletions

View File

@ -5,16 +5,19 @@
#define CONVERTER_H
#include <QIODevice>
#include <QList>
#include <QStringList>
#include <QVariant>
class Converter
{
static QList<const Converter *> &converters();
protected:
Converter();
static bool isNull(const Converter *converter); // in nullconverter.cpp
public:
static const QList<const Converter *> &allConverters();
enum class Direction { In = 1, Out = 2, InOut = In | Out };
Q_DECLARE_FLAGS(Directions, Direction)

View File

@ -14,18 +14,25 @@
using namespace Qt::StringLiterals;
static QList<const Converter *> *availableConverters;
Converter::Converter()
{
if (!availableConverters)
availableConverters = new QList<const Converter *>;
availableConverters->append(this);
converters().append(this);
}
Converter::~Converter()
{
availableConverters->removeAll(this);
converters().removeAll(this);
}
QList<const Converter *> &Converter::converters()
{
Q_CONSTINIT static QList<const Converter *> store;
return store;
}
const QList<const Converter *> &Converter::allConverters()
{
return converters();
}
static const Converter *prepareConverter(QString format, Converter::Direction direction,
@ -46,7 +53,7 @@ static const Converter *prepareConverter(QString format, Converter::Direction di
qFatal("Could not open \"%s\" for %s: %s",
qPrintable(stream->fileName()), dirn, qPrintable(stream->errorString()));
} else if (format == "auto"_L1) {
for (const Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : Converter::allConverters()) {
if (conv->directions().testFlag(direction) && conv->probeFile(stream))
return conv;
}
@ -56,7 +63,7 @@ static const Converter *prepareConverter(QString format, Converter::Direction di
// Input format, however, we must know before we can call that:
qFatal("Could not determine input format. Specify it with the -I option.");
} else {
for (const Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : Converter::allConverters()) {
if (conv->name() == format) {
if (!conv->directions().testFlag(direction)) {
qWarning("File format \"%s\" cannot be used for %s",
@ -77,7 +84,7 @@ int main(int argc, char *argv[])
QStringList inputFormats;
QStringList outputFormats;
for (const Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : Converter::allConverters()) {
auto direction = conv->directions();
QString name = conv->name();
if (direction.testFlag(Converter::Direction::In))
@ -130,7 +137,7 @@ int main(int argc, char *argv[])
if (parser.isSet(formatOptionsOption)) {
QString format = parser.value(formatOptionsOption);
for (const Converter *conv : std::as_const(*availableConverters)) {
for (const Converter *conv : Converter::allConverters()) {
if (conv->name() == format) {
const char *help = conv->optionsHelp();
if (help) {