diff options
author | Jake Petroules <jake.petroules@qt.io> | 2017-06-28 18:23:28 -0700 |
---|---|---|
committer | Jake Petroules <jake.petroules@qt.io> | 2017-09-01 18:25:16 +0000 |
commit | 7eb98994dc9d95cf8d3d531804f84e2de090b4c0 (patch) | |
tree | cbd5a2e608afb0bd87e4242cd4f85b2dd6e3ddff /src/lib/corelib/generators | |
parent | 710dfb0cd2f6e4e24d7af38cdd962dd218d997bc (diff) |
Generator data: use a template method to reduce pattern duplication
Change-Id: I7613d221fba6d343726418837ce5f3013b9a8d96
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'src/lib/corelib/generators')
-rw-r--r-- | src/lib/corelib/generators/generatordata.cpp | 59 | ||||
-rw-r--r-- | src/lib/corelib/generators/generatordata.h | 30 |
2 files changed, 36 insertions, 53 deletions
diff --git a/src/lib/corelib/generators/generatordata.cpp b/src/lib/corelib/generators/generatordata.cpp index 1ff186580..37e4246fe 100644 --- a/src/lib/corelib/generators/generatordata.cpp +++ b/src/lib/corelib/generators/generatordata.cpp @@ -47,65 +47,30 @@ namespace qbs { QString GeneratableProductData::name() const { - QString name; - QMapIterator<QString, ProductData> it(data); - while (it.hasNext()) { - it.next(); - QString oldName = name; - name = it.value().name(); - if (!oldName.isEmpty() && oldName != name) - throw ErrorInfo(QLatin1String("Products with different names per-configuration " - "are not compatible with this generator. Consider " - "using the targetName property instead.")); - } - return name; + return uniqueValue<QString>(&ProductData::name, + QLatin1String("Products with different names per configuration are not " + "compatible with this generator. " + "Consider using the targetName property instead.")); } CodeLocation GeneratableProductData::location() const { - CodeLocation location; - QMapIterator<QString, ProductData> it(data); - while (it.hasNext()) { - it.next(); - CodeLocation oldLocation = location; - location = it.value().location(); - if (oldLocation.isValid() && oldLocation != location) - throw ErrorInfo(QLatin1String("Products with different code locations " - "per-configuration are not compatible with this " - "generator.")); - } - return location; + return uniqueValue<CodeLocation>(&ProductData::location, + QLatin1String("GeneratableProductData::location: internal bug; this should not happen.")); } QStringList GeneratableProductData::dependencies() const { - QStringList list; - QMapIterator<QString, ProductData> it(data); - while (it.hasNext()) { - it.next(); - QStringList oldList = list; - list = it.value().dependencies(); - if (!oldList.isEmpty() && oldList != list) - throw ErrorInfo(QLatin1String("Products with different dependency lists " - "per-configuration are not compatible with this " - "generator.")); - } - return list; + return uniqueValue<QStringList>(&ProductData::dependencies, + QLatin1String("Products with different dependency lists per configuration are not " + "compatible with this generator.")); } QString GeneratableProjectData::name() const { - QString name; - QMapIterator<QString, ProjectData> it(data); - while (it.hasNext()) { - it.next(); - QString oldName = name; - name = it.value().name(); - if (!oldName.isEmpty() && oldName != name) - throw ErrorInfo(QLatin1String("Projects with different names per-configuration " - "are not compatible with this generator.")); - } - return name; + return uniqueValue<QString>(&ProjectData::name, + QLatin1String("Projects with different names per configuration are not " + "compatible with this generator.")); } CodeLocation GeneratableProjectData::location() const diff --git a/src/lib/corelib/generators/generatordata.h b/src/lib/corelib/generators/generatordata.h index 47f4acb93..d8cf91c34 100644 --- a/src/lib/corelib/generators/generatordata.h +++ b/src/lib/corelib/generators/generatordata.h @@ -44,22 +44,41 @@ #include <QtCore/qmap.h> #include <api/project.h> #include <api/projectdata.h> +#include <tools/error.h> #include <tools/installoptions.h> +#include <tools/set.h> +#include <functional> namespace qbs { typedef QMap<QString, Project> GeneratableProjectMap; -typedef QMap<QString, ProjectData> GeneratableProjectDataMap; -typedef QMap<QString, ProductData> GeneratableProductDataMap; -struct QBS_EXPORT GeneratableProductData { - GeneratableProductDataMap data; +template <typename U> struct IMultiplexableContainer { + QMap<QString, U> data; + + template <typename T> T uniqueValue(const std::function<T(const U &data)> &func, + const QString &errorMessage) const + { + auto it = data.begin(), end = data.end(); + auto value = func(*it++); + for (; it != end; ++it) { + if (value != func(*it)) + throw ErrorInfo(errorMessage); + } + return value; + } + +protected: + IMultiplexableContainer() { } +}; + +struct QBS_EXPORT GeneratableProductData : public IMultiplexableContainer<ProductData> { QString name() const; CodeLocation location() const; QStringList dependencies() const; }; -struct QBS_EXPORT GeneratableProjectData { +struct QBS_EXPORT GeneratableProjectData : public IMultiplexableContainer<ProjectData> { struct Id { private: friend struct GeneratableProjectData; @@ -70,7 +89,6 @@ struct QBS_EXPORT GeneratableProjectData { bool operator<(const Id &id) const { return value < id.value; } }; - GeneratableProjectDataMap data; QList<GeneratableProjectData> subProjects; QList<GeneratableProductData> products; QString name() const; |