From d2c1f891cce0b7339ac86009e1fbb042d40dda0f Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 12 May 2020 10:22:21 +0200 Subject: shiboken: Prepare for extending GeneratorContext Remove the constructor GeneratorContext and add creation functions to the generators. Make the class creation function virtual so that ShibokenGenerator can override it. Change-Id: I7bc002555356be73ddab5a2095802747796acb7e Reviewed-by: Cristian Maureira-Fredes --- sources/shiboken2/generator/generator.cpp | 23 ++++++++++++++++++---- sources/shiboken2/generator/generator.h | 13 ++++++------ .../shiboken2/generator/shiboken2/cppgenerator.cpp | 4 ++-- .../generator/shiboken2/headergenerator.cpp | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) (limited to 'sources/shiboken2/generator') diff --git a/sources/shiboken2/generator/generator.cpp b/sources/shiboken2/generator/generator.cpp index e997ef687..c04661ab6 100644 --- a/sources/shiboken2/generator/generator.cpp +++ b/sources/shiboken2/generator/generator.cpp @@ -440,12 +440,28 @@ QString Generator::getFileNameBaseForSmartPointer(const AbstractMetaType *smartP return fileName; } +GeneratorContext Generator::contextForClass(const AbstractMetaClass *c) const +{ + GeneratorContext result; + result.m_metaClass = c; + return result; +} + +GeneratorContext Generator::contextForSmartPointer(const AbstractMetaClass *c, + const AbstractMetaType *t) const +{ + GeneratorContext result; + result.m_metaClass = c; + result.m_preciseClassType = t; + result.m_forSmartPointer = true; + return result; +} + bool Generator::generate() { const AbstractMetaClassList &classList = m_d->apiextractor->classes(); for (AbstractMetaClass *cls : classList) { - GeneratorContext context(cls); - if (!generateFileForContext(context)) + if (!generateFileForContext(contextForClass(cls))) return false; } @@ -459,8 +475,7 @@ bool Generator::generate() smartPointers))); return false; } - GeneratorContext context(smartPointerClass, type, true); - if (!generateFileForContext(context)) + if (!generateFileForContext(contextForSmartPointer(smartPointerClass, type))) return false; } return finishGeneration(); diff --git a/sources/shiboken2/generator/generator.h b/sources/shiboken2/generator/generator.h index 136f0a5f0..48412e7ea 100644 --- a/sources/shiboken2/generator/generator.h +++ b/sources/shiboken2/generator/generator.h @@ -146,15 +146,10 @@ private: * In the future the second case might be generalized for all template type instantiations. */ class GeneratorContext { + friend class ShibokenGenerator; + friend class Generator; public: GeneratorContext() = default; - GeneratorContext(const AbstractMetaClass *metaClass, - const AbstractMetaType *preciseType = nullptr, - bool forSmartPointer = false) - : m_metaClass(metaClass), - m_preciseClassType(preciseType), - m_forSmartPointer(forSmartPointer) {} - const AbstractMetaClass *metaClass() const { return m_metaClass; } bool forSmartPointer() const { return m_forSmartPointer; } @@ -294,6 +289,10 @@ protected: /// Returns an AbstractMetaEnum for a given AbstractMetaType that holds an EnumTypeEntry, or nullptr if not found. const AbstractMetaEnum *findAbstractMetaEnum(const AbstractMetaType *metaType) const; + virtual GeneratorContext contextForClass(const AbstractMetaClass *c) const; + GeneratorContext contextForSmartPointer(const AbstractMetaClass *c, + const AbstractMetaType *t) const; + /// Generates a file for given AbstractMetaClass or AbstractMetaType (smart pointer case). bool generateFileForContext(const GeneratorContext &context); diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 94b488ac0..1ef901ade 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -341,7 +341,7 @@ void CppGenerator::generateClass(QTextStream &s, const GeneratorContext &classCo s << Qt::endl << "// inner classes\n"; const AbstractMetaClassList &innerClasses = metaClass->innerClasses(); for (AbstractMetaClass *innerClass : innerClasses) { - GeneratorContext innerClassContext(innerClass); + GeneratorContext innerClassContext = contextForClass(innerClass); if (shouldGenerate(innerClass) && !innerClass->typeEntry()->isSmartPointer()) { QString headerfile = fileNameForContext(innerClassContext); headerfile.replace(QLatin1String(".cpp"), QLatin1String(".h")); @@ -5606,7 +5606,7 @@ bool CppGenerator::finishGeneration() // Initialize smart pointer types. const QVector &smartPtrs = instantiatedSmartPointers(); for (const AbstractMetaType *metaType : smartPtrs) { - GeneratorContext context(nullptr, metaType, true); + GeneratorContext context = contextForSmartPointer(nullptr, metaType); writeInitFunc(s_classInitDecl, s_classPythonDefines, INDENT, getInitFunctionName(context), metaType->typeEntry()->targetLangEnclosingEntry()); diff --git a/sources/shiboken2/generator/shiboken2/headergenerator.cpp b/sources/shiboken2/generator/shiboken2/headergenerator.cpp index b72a3f8bb..65bc8909e 100644 --- a/sources/shiboken2/generator/shiboken2/headergenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/headergenerator.cpp @@ -206,7 +206,7 @@ void HeaderGenerator::generateClass(QTextStream &s, const GeneratorContext &clas metaClass = metaClass->baseClass(); if (!metaClass || !avoidProtectedHack()) break; - classContext = GeneratorContext(metaClass); + classContext = contextForClass(metaClass); if (!classContext.forSmartPointer()) { wrapperName = HeaderGenerator::wrapperName(metaClass); } else { -- cgit v1.2.3