aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-04-04 11:32:03 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-04-04 15:14:45 +0200
commit4065148deccb1ea4f3859290b2fd1dc091ab0529 (patch)
treef39e3cede0e5c1fac6f6f62867deb94d35bc5191
parent04544e58cdf32049579b8081cac44e8b3ad17171 (diff)
shiboken6: Extend the bool cast function
Introduce a struct for the bool cast function that has a bool indicating whether the function returns true (operator bool(), std::optional::has_value()), or false (QSharedPointer::isNull()). Extract a helper to write the actual expression, hereby fixing the hitherto hard-coded function name. Pick-to: 6.2 Task-number: PYSIDE-454 Change-Id: I57b428febc30a9db7d3638f1c555943d2894ef58 Reviewed-by: Christian Tismer <tismer@stackless.com> Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp44
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.h15
2 files changed, 41 insertions, 18 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index a8a16d29a..31a978db5 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -286,7 +286,8 @@ QList<AbstractMetaFunctionCList>
return result;
}
-AbstractMetaFunctionCPtr CppGenerator::boolCast(const AbstractMetaClass *metaClass) const
+CppGenerator::BoolCastFunctionOptional
+ CppGenerator::boolCast(const AbstractMetaClass *metaClass) const
{
const auto *te = metaClass->typeEntry();
if (te->isSmartPointer()) {
@@ -296,7 +297,7 @@ AbstractMetaFunctionCPtr CppGenerator::boolCast(const AbstractMetaClass *metaCla
const auto func = metaClass->findFunction(nullCheckMethod);
if (func.isNull())
throw Exception(msgMethodNotFound(metaClass, nullCheckMethod));
- return func;
+ return BoolCastFunction{func, true};
}
}
@@ -305,7 +306,7 @@ AbstractMetaFunctionCPtr CppGenerator::boolCast(const AbstractMetaClass *metaCla
? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
const auto func = metaClass->findOperatorBool();
if (!func.isNull())
- return func;
+ return BoolCastFunction{func, false};
}
mode = te->isNullMode();
@@ -313,9 +314,9 @@ AbstractMetaFunctionCPtr CppGenerator::boolCast(const AbstractMetaClass *metaCla
? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
const auto func = metaClass->findQtIsNullMethod();
if (!func.isNull())
- return func;
+ return BoolCastFunction{func, true};
}
- return {};
+ return std::nullopt;
}
std::optional<AbstractMetaType>
@@ -673,8 +674,8 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
if ((attroCheck & AttroCheckFlag::SetattroMask) != 0)
writeSetattroFunction(s, attroCheck, classContext);
- if (const auto f = boolCast(metaClass) ; !f.isNull())
- writeNbBoolFunction(classContext, f, s);
+ if (const auto f = boolCast(metaClass) ; f.has_value())
+ writeNbBoolFunction(classContext, f.value(), s);
if (supportsNumberProtocol(metaClass)) {
const QList<AbstractMetaFunctionCList> opOverloads = filterGroupedOperatorFunctions(
@@ -870,11 +871,12 @@ void CppGenerator::generateSmartPointerClass(TextStream &s, const GeneratorConte
writePyMethodDefs(s, className, methodsDefinitions, true /* ___copy__ */);
// Write tp_s/getattro function
+ const auto boolCastOpt = boolCast(metaClass);
writeSmartPointerGetattroFunction(s, classContext);
writeSmartPointerSetattroFunction(s, classContext);
- if (const auto f = boolCast(metaClass) ; !f.isNull())
- writeNbBoolFunction(classContext, f, s);
+ if (boolCastOpt.has_value())
+ writeNbBoolFunction(classContext, boolCastOpt.value(), s);
writeSmartPointerRichCompareFunction(s, classContext);
@@ -6305,24 +6307,36 @@ PyErr_Format(PyExc_AttributeError,
<< "return tmp;\n" << outdent << "}\n\n";
}
+void CppGenerator::writeNbBoolExpression(TextStream &s, const BoolCastFunction &f,
+ bool invert)
+{
+ if (f.function->isOperatorBool()) {
+ if (invert)
+ s << '!';
+ s << '*' << CPP_SELF_VAR;
+ return;
+ }
+ if (invert != f.invert)
+ s << '!';
+ s << CPP_SELF_VAR << "->" << f.function->name() << "()";
+}
+
void CppGenerator::writeNbBoolFunction(const GeneratorContext &context,
- const AbstractMetaFunctionCPtr &f,
+ const BoolCastFunction &f,
TextStream &s) const
{
s << "static int " << cpythonBaseName(context.metaClass()) << "___nb_bool(PyObject *self)\n"
<< "{\n" << indent;
writeCppSelfDefinition(s, context, ErrorReturn::MinusOne);
- const bool allowThread = f->allowThread();
+ const bool allowThread = f.function->allowThread();
if (allowThread)
s << "int result;\n" << BEGIN_ALLOW_THREADS << "\nresult = ";
else
s << "return ";
- if (f->isOperatorBool())
- s << '*' << CPP_SELF_VAR << " ? 1 : 0;\n";
- else
- s << CPP_SELF_VAR << "->isNull() ? 0 : 1;\n";
+ writeNbBoolExpression(s, f);
+ s << " ? 1 : 0;\n";
if (allowThread)
s << END_ALLOW_THREADS << "\nreturn result;\n";
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h
index a45374dbb..b3a611f32 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.h
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.h
@@ -64,6 +64,13 @@ protected:
bool finishGeneration() override;
private:
+ struct BoolCastFunction
+ {
+ AbstractMetaFunctionCPtr function;
+ bool invert = false; // Function is isNull() (invert result).
+ };
+ using BoolCastFunctionOptional = std::optional<BoolCastFunction>;
+
void generateSmartPointerClass(TextStream &s, const GeneratorContext &classContext);
void generateIncludes(TextStream &s, const GeneratorContext &classContext,
QList<Include> includes = {},
@@ -182,8 +189,10 @@ private:
QString qObjectGetAttroFunction() const;
void writeNbBoolFunction(const GeneratorContext &context,
- const AbstractMetaFunctionCPtr &f,
+ const BoolCastFunction &f,
TextStream &s) const;
+ static void writeNbBoolExpression(TextStream &s, const BoolCastFunction &f,
+ bool invert = false);
/**
* Writes Python to C++ conversions for arguments on Python wrappers.
@@ -492,9 +501,9 @@ private:
QString writeReprFunction(TextStream &s, const GeneratorContext &context,
uint indirections) const;
- AbstractMetaFunctionCPtr boolCast(const AbstractMetaClass *metaClass) const;
+ BoolCastFunctionOptional boolCast(const AbstractMetaClass *metaClass) const;
bool hasBoolCast(const AbstractMetaClass *metaClass) const
- { return !boolCast(metaClass).isNull(); }
+ { return boolCast(metaClass).has_value(); }
std::optional<AbstractMetaType>
findSmartPointerInstantiation(const SmartPointerTypeEntry *pointer,