ProjectExplorer: Introduce JsonWizardGeneratorTypedFactory

Limit code repetition by introducing convenient class template.

Change-Id: I90b45a305c5a6f28bf75a602c14ff055928cda48
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Jarek Kobus
2024-07-18 08:34:46 +02:00
parent 9d0c144892
commit 2a5c2765c4
4 changed files with 41 additions and 125 deletions

View File

@@ -260,51 +260,9 @@ bool JsonWizardFileGenerator::writeFile(const JsonWizard *wizard, Core::Generate
return true;
}
// Factory
class FileGeneratorFactory final : public JsonWizardGeneratorFactory
{
public:
FileGeneratorFactory()
{
setTypeIdsSuffix(QLatin1String("File"));
}
JsonWizardGenerator *create(Id typeId, const QVariant &data,
const QString &path, Id platform,
const QVariantMap &variables) final
{
Q_UNUSED(path)
Q_UNUSED(platform)
Q_UNUSED(variables)
QTC_ASSERT(canCreate(typeId), return nullptr);
auto gen = new JsonWizardFileGenerator;
QString errorMessage;
gen->setup(data, &errorMessage);
if (!errorMessage.isEmpty()) {
qWarning() << "FileGeneratorFactory setup error:" << errorMessage;
delete gen;
return nullptr;
}
return gen;
}
bool validateData(Id typeId, const QVariant &data, QString *errorMessage) final
{
QTC_ASSERT(canCreate(typeId), return false);
QScopedPointer<JsonWizardFileGenerator> gen(new JsonWizardFileGenerator);
return gen->setup(data, errorMessage);
}
};
void setupJsonWizardFileGenerator()
{
static FileGeneratorFactory theFileGeneratorFactory;
static JsonWizardGeneratorTypedFactory<JsonWizardFileGenerator> theFileGeneratorFactory("File");
}
} // ProjectExplorer

View File

@@ -69,4 +69,40 @@ private:
QList<Utils::Id> m_typeIds;
};
template <typename Generator>
class JsonWizardGeneratorTypedFactory : public JsonWizardGeneratorFactory
{
public:
JsonWizardGeneratorTypedFactory(const QString &suffix) { setTypeIdsSuffix(suffix); }
JsonWizardGenerator *create(Utils::Id typeId, const QVariant &data,
const QString &path, Utils::Id platform,
const QVariantMap &variables) final
{
Q_UNUSED(path)
Q_UNUSED(platform)
Q_UNUSED(variables)
QTC_ASSERT(canCreate(typeId), return nullptr);
auto gen = new Generator;
QString errorMessage;
gen->setup(data, &errorMessage);
if (!errorMessage.isEmpty()) {
qWarning() << "JsonWizardGeneratorTypedFactory for " << typeId << "setup error:"
<< errorMessage;
delete gen;
return nullptr;
}
return gen;
}
bool validateData(Utils::Id typeId, const QVariant &data, QString *errorMessage) final
{
QTC_ASSERT(canCreate(typeId), return false);
QScopedPointer<Generator> gen(new Generator);
return gen->setup(data, errorMessage);
}
};
} // namespace ProjectExplorer

View File

@@ -149,51 +149,10 @@ Core::GeneratedFiles JsonWizardScannerGenerator::scan(const Utils::FilePath &dir
return result;
}
// JsonWizardScannerGeneratorFactory
class JsonWizardScannerGeneratorFactory final : public JsonWizardGeneratorFactory
{
public:
JsonWizardScannerGeneratorFactory()
{
setTypeIdsSuffix(QLatin1String("Scanner"));
}
JsonWizardGenerator *create(Id typeId, const QVariant &data,
const QString &path, Id platform,
const QVariantMap &variables) final
{
Q_UNUSED(path)
Q_UNUSED(platform)
Q_UNUSED(variables)
QTC_ASSERT(canCreate(typeId), return nullptr);
auto gen = new JsonWizardScannerGenerator;
QString errorMessage;
gen->setup(data, &errorMessage);
if (!errorMessage.isEmpty()) {
qWarning() << "JsonWizardScannerGeneratorFactory setup error:" << errorMessage;
delete gen;
return nullptr;
}
return gen;
}
bool validateData(Id typeId, const QVariant &data, QString *errorMessage) final
{
QTC_ASSERT(canCreate(typeId), return false);
QScopedPointer<JsonWizardScannerGenerator> gen(new JsonWizardScannerGenerator);
return gen->setup(data, errorMessage);
}
};
void setupJsonWizardScannerGenerator()
{
static JsonWizardScannerGeneratorFactory theScannerGeneratorFactory;
static JsonWizardGeneratorTypedFactory<JsonWizardScannerGenerator>
theScannerGeneratorFactory("Scanner");
}
} // ProjectExplorer::Internal

View File

@@ -417,50 +417,13 @@ bool SquishFileGenerator::allDone(const JsonWizard *wizard,
return true;
}
class SquishGeneratorFactory final : public JsonWizardGeneratorFactory
{
public:
SquishGeneratorFactory()
{
setTypeIdsSuffix("SquishSuiteGenerator");
}
JsonWizardGenerator *create(Id typeId, const QVariant &data,
const QString &path, Id platform,
const QVariantMap &variables) final
{
Q_UNUSED(path)
Q_UNUSED(platform)
Q_UNUSED(variables)
QTC_ASSERT(canCreate(typeId), return nullptr);
auto generator = new SquishFileGenerator;
QString errorMessage;
generator->setup(data, &errorMessage);
if (!errorMessage.isEmpty()) {
qWarning() << "SquishSuiteGenerator setup error:" << errorMessage;
delete generator;
return nullptr;
}
return generator;
}
bool validateData(Id typeId, const QVariant &data, QString *errorMessage) final
{
QTC_ASSERT(canCreate(typeId), return false);
QScopedPointer<SquishFileGenerator> generator(new SquishFileGenerator);
return generator->setup(data, errorMessage);
}
};
void setupSquishWizardPages()
{
static SquishToolkitsPageFactory theSquishToolkitsPageFactory;
static SquishScriptLanguagePageFactory theSquishScriptLanguagePageFactory;
static SquishAUTPageFactory theSquishAUTPageFactory;
static SquishGeneratorFactory theSquishGeneratorFactory;
static JsonWizardGeneratorTypedFactory<SquishFileGenerator>
theSquishGeneratorFactory("SquishSuiteGenerator");
}
} // Squish::Internal