diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-14 11:14:05 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-15 13:16:03 +0200 |
commit | 2eae75d55a693d04f8a6d2cd09394f7bb9dcb400 (patch) | |
tree | 323b71f024f2e816c6230a7cbdacd4dff17bd604 /sources/shiboken6/ApiExtractor/abstractmetatype.cpp | |
parent | 87bce5e6bb10ed04a124c628e4794bfdb24d448b (diff) |
shiboken6: Move AbstractMetaType factory functions into AbstractMetaType
They do not really belong to the generator.
Task-number: PYSIDE-1660
Change-Id: I550643bb00cbb1af937f45de5a4820c883259802
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken6/ApiExtractor/abstractmetatype.cpp')
-rw-r--r-- | sources/shiboken6/ApiExtractor/abstractmetatype.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetatype.cpp b/sources/shiboken6/ApiExtractor/abstractmetatype.cpp index 3401a61c9..f099bba08 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetatype.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetatype.cpp @@ -27,6 +27,9 @@ ****************************************************************************/ #include "abstractmetatype.h" +#include "abstractmetabuilder.h" +#include "abstractmetalang.h" +#include "messages.h" #include "typedatabase.h" #include "typesystem.h" #include "parser/codemodel.h" @@ -35,6 +38,7 @@ # include <QtCore/QDebug> #endif +#include <QtCore/QHash> #include <QtCore/QSharedData> #include <QtCore/QSharedPointer> #include <QtCore/QStack> @@ -797,6 +801,55 @@ bool AbstractMetaType::valueTypeWithCopyConstructorOnlyPassed() const && isValueTypeWithCopyConstructorOnly(); } +using AbstractMetaTypeCache = QHash<QString, AbstractMetaType>; + +Q_GLOBAL_STATIC(AbstractMetaTypeCache, metaTypeFromStringCache) + +std::optional<AbstractMetaType> +AbstractMetaType::fromString(QString typeSignature, QString *errorMessage) +{ + typeSignature = typeSignature.trimmed(); + if (typeSignature.startsWith(QLatin1String("::"))) + typeSignature.remove(0, 2); + + auto &cache = *metaTypeFromStringCache(); + auto it = cache.find(typeSignature); + if (it == cache.end()) { + auto metaType = + AbstractMetaBuilder::translateType(typeSignature, nullptr, {}, errorMessage); + if (Q_UNLIKELY(!metaType.has_value())) { + if (errorMessage) + errorMessage->prepend(msgCannotBuildMetaType(typeSignature)); + return {}; + } + it = cache.insert(typeSignature, metaType.value()); + } + return it.value(); +} + +AbstractMetaType AbstractMetaType::fromTypeEntry(const TypeEntry *typeEntry) +{ + QString typeName = typeEntry->qualifiedCppName(); + if (typeName.startsWith(QLatin1String("::"))) + typeName.remove(0, 2); + auto &cache = *metaTypeFromStringCache(); + auto it = cache.find(typeName); + if (it != cache.end()) + return it.value(); + AbstractMetaType metaType(typeEntry); + metaType.clearIndirections(); + metaType.setReferenceType(NoReference); + metaType.setConstant(false); + metaType.decideUsagePattern(); + cache.insert(typeName, metaType); + return metaType; +} + +AbstractMetaType AbstractMetaType::fromAbstractMetaClass(const AbstractMetaClass *metaClass) +{ + return fromTypeEntry(metaClass->typeEntry()); +} + #ifndef QT_NO_DEBUG_STREAM void AbstractMetaType::formatDebug(QDebug &debug) const { |