diff options
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 50 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.h | 4 |
2 files changed, 32 insertions, 22 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 8143fd54f..4c6ed1711 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -692,28 +692,8 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon writeSetattroFunction(s, attroCheck, classContext); } - const auto f = boolCast(metaClass); - if (!f.isNull()) { - ErrorCode errorCode(-1); - s << "static int " << cpythonBaseName(metaClass) << "___nb_bool(PyObject *self)\n" - << "{\n" << indent; - writeCppSelfDefinition(s, classContext); - - const bool allowThread = f->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"; - - if (allowThread) - s << END_ALLOW_THREADS << "\nreturn result;\n"; - s << outdent << "}\n\n"; - } + if (const auto f = boolCast(metaClass) ; !f.isNull()) + writeNbBoolFunction(classContext, f, s); if (supportsNumberProtocol(metaClass) && !metaClass->typeEntry()->isSmartPointer()) { const QList<AbstractMetaFunctionCList> opOverloads = filterGroupedOperatorFunctions( @@ -6133,6 +6113,32 @@ PyErr_Format(PyExc_AttributeError, << "return tmp;\n" << outdent << "}\n\n"; } +void CppGenerator::writeNbBoolFunction(const GeneratorContext &context, + const AbstractMetaFunctionCPtr &f, + TextStream &s) const +{ + ErrorCode errorCode(-1); + + s << "static int " << cpythonBaseName(context.metaClass()) << "___nb_bool(PyObject *self)\n" + << "{\n" << indent; + writeCppSelfDefinition(s, context); + + const bool allowThread = f->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"; + + if (allowThread) + s << END_ALLOW_THREADS << "\nreturn result;\n"; + s << outdent << "}\n\n"; +} + // Write declaration and invocation of the init function for the module init // function. void CppGenerator::writeInitFunc(TextStream &declStr, TextStream &callStr, diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index b5de61eab..e226feec9 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -157,6 +157,10 @@ private: const GeneratorContext &context) const; QString qObjectGetAttroFunction() const; + void writeNbBoolFunction(const GeneratorContext &context, + const AbstractMetaFunctionCPtr &f, + TextStream &s) const; + /** * Writes Python to C++ conversions for arguments on Python wrappers. * If implicit conversions, and thus new object allocation, are needed, |