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). Task-number: QTBUG-111228 Change-Id: I30f4bf3c46d39e04d0ac4e3e9ba431945ebb9193 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io> (cherry picked from commit d57a78657e09e38dc4137e3c6c80c1a42817cc8c) Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit d46d1db4c64800c5990957007c9fd0ff2e5a120f) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
942ac893ce
commit
24dfc96fa8
@ -5,16 +5,19 @@
|
|||||||
#define CONVERTER_H
|
#define CONVERTER_H
|
||||||
|
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
|
#include <QList>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
class Converter
|
class Converter
|
||||||
{
|
{
|
||||||
|
static QList<const Converter *> &converters();
|
||||||
protected:
|
protected:
|
||||||
Converter();
|
Converter();
|
||||||
static bool isNull(const Converter *converter); // in nullconverter.cpp
|
static bool isNull(const Converter *converter); // in nullconverter.cpp
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static const QList<const Converter *> &allConverters();
|
||||||
|
|
||||||
enum class Direction { In = 1, Out = 2, InOut = In | Out };
|
enum class Direction { In = 1, Out = 2, InOut = In | Out };
|
||||||
Q_DECLARE_FLAGS(Directions, Direction)
|
Q_DECLARE_FLAGS(Directions, Direction)
|
||||||
|
@ -14,18 +14,25 @@
|
|||||||
|
|
||||||
using namespace Qt::StringLiterals;
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static QList<const Converter *> *availableConverters;
|
|
||||||
|
|
||||||
Converter::Converter()
|
Converter::Converter()
|
||||||
{
|
{
|
||||||
if (!availableConverters)
|
converters().append(this);
|
||||||
availableConverters = new QList<const Converter *>;
|
|
||||||
availableConverters->append(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::~Converter()
|
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,
|
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",
|
qFatal("Could not open \"%s\" for %s: %s",
|
||||||
qPrintable(stream->fileName()), dirn, qPrintable(stream->errorString()));
|
qPrintable(stream->fileName()), dirn, qPrintable(stream->errorString()));
|
||||||
} else if (format == "auto"_L1) {
|
} 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))
|
if (conv->directions().testFlag(direction) && conv->probeFile(stream))
|
||||||
return conv;
|
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:
|
// Input format, however, we must know before we can call that:
|
||||||
qFatal("Could not determine input format. Specify it with the -I option.");
|
qFatal("Could not determine input format. Specify it with the -I option.");
|
||||||
} else {
|
} else {
|
||||||
for (const Converter *conv : std::as_const(*availableConverters)) {
|
for (const Converter *conv : Converter::allConverters()) {
|
||||||
if (conv->name() == format) {
|
if (conv->name() == format) {
|
||||||
if (!conv->directions().testFlag(direction)) {
|
if (!conv->directions().testFlag(direction)) {
|
||||||
qWarning("File format \"%s\" cannot be used for %s",
|
qWarning("File format \"%s\" cannot be used for %s",
|
||||||
@ -77,7 +84,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
QStringList inputFormats;
|
QStringList inputFormats;
|
||||||
QStringList outputFormats;
|
QStringList outputFormats;
|
||||||
for (const Converter *conv : std::as_const(*availableConverters)) {
|
for (const Converter *conv : Converter::allConverters()) {
|
||||||
auto direction = conv->directions();
|
auto direction = conv->directions();
|
||||||
QString name = conv->name();
|
QString name = conv->name();
|
||||||
if (direction.testFlag(Converter::Direction::In))
|
if (direction.testFlag(Converter::Direction::In))
|
||||||
@ -130,7 +137,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (parser.isSet(formatOptionsOption)) {
|
if (parser.isSet(formatOptionsOption)) {
|
||||||
QString format = parser.value(formatOptionsOption);
|
QString format = parser.value(formatOptionsOption);
|
||||||
for (const Converter *conv : std::as_const(*availableConverters)) {
|
for (const Converter *conv : Converter::allConverters()) {
|
||||||
if (conv->name() == format) {
|
if (conv->name() == format) {
|
||||||
const char *help = conv->optionsHelp();
|
const char *help = conv->optionsHelp();
|
||||||
if (help) {
|
if (help) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user