aboutsummaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-07-28 00:50:00 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:05 -0300
commit0eb50de5faccf1a87f6f4686c8e214dcdb677aa5 (patch)
tree1fb77ab1438c808a31a858ca6e07b43edda8768c /generator
parent9e55726d0b7abf8daf41319ec34db6dceb834b30 (diff)
Improved CppGenerator::writeCppSelfDefinition() method and its use.
Now anyone wanting to get a cppSelf object must go through this method.
Diffstat (limited to 'generator')
-rw-r--r--generator/cppgenerator.cpp99
-rw-r--r--generator/cppgenerator.h3
2 files changed, 50 insertions, 52 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index 0d56aeedb..fbe58dcf8 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -1174,45 +1174,7 @@ void CppGenerator::writeMethodWrapper(QTextStream& s, const AbstractMetaFunction
if (rfunc->implementingClass() &&
(!rfunc->implementingClass()->isNamespace() && overloadData.hasInstanceFunction())) {
-
- s << INDENT;
- QString protectedClassWrapperName;
- if (avoidProtectedHack() && rfunc->ownerClass()->hasProtectedMembers()) {
- protectedClassWrapperName = wrapperName(rfunc->ownerClass());
- s << protectedClassWrapperName;
- } else {
- s << "::" << rfunc->ownerClass()->qualifiedCppName();
- }
- s << "* " CPP_SELF_VAR " = 0;" << endl;
-
- if (rfunc->isOperatorOverload() && rfunc->isBinaryOperator()) {
- QString checkFunc = cpythonCheckFunction(rfunc->ownerClass()->typeEntry());
- s << INDENT << "bool isReverse = " << checkFunc << "(arg) && !" << checkFunc << "(self);\n"
- << INDENT << "if (isReverse)\n";
- Indentation indent(INDENT);
- s << INDENT << "std::swap(self, arg);\n\n";
- }
-
- // Sets the C++ "self" (the "this" for the object) if it has one.
- QString cppSelfAttribution = CPP_SELF_VAR " = ";
- if (avoidProtectedHack() && !protectedClassWrapperName.isEmpty())
- cppSelfAttribution += QString("(%1*)").arg(protectedClassWrapperName);
- cppSelfAttribution += cpythonWrapperCPtr(rfunc->ownerClass(), "self");
-
- // Checks if the underlying C++ object is valid.
- if (overloadData.hasStaticFunction()) {
- s << INDENT << "if (self) {" << endl;
- {
- Indentation indent(INDENT);
- writeInvalidCppObjectCheck(s, "self");
- s << INDENT << cppSelfAttribution << ';' << endl;
- }
- s << INDENT << '}' << endl;
- } else {
- writeInvalidCppObjectCheck(s, "self");
- s << INDENT << cppSelfAttribution << ';' << endl;
- }
- s << endl;
+ writeCppSelfDefinition(s, rfunc, overloadData.hasStaticFunction());
}
bool hasReturnValue = overloadData.hasNonVoidReturnType();
@@ -1422,22 +1384,57 @@ void CppGenerator::writeArgumentsInitializer(QTextStream& s, OverloadData& overl
s << endl;
}
-void CppGenerator::writeCppSelfDefinition(QTextStream& s, const AbstractMetaFunction* func)
+void CppGenerator::writeCppSelfDefinition(QTextStream& s, const AbstractMetaClass* metaClass, bool hasStaticOverload)
{
- if (!func->ownerClass() || func->isStatic() || func->isConstructor())
- return;
+ QString className;
+ if (avoidProtectedHack() && metaClass->hasProtectedMembers())
+ className = wrapperName(metaClass);
+ else
+ className = QString("::%1").arg(metaClass->qualifiedCppName());
+ s << INDENT << className << "* " CPP_SELF_VAR " = 0;" << endl;
- s << INDENT;
- if (avoidProtectedHack()) {
- QString _wrapperName = wrapperName(func->ownerClass());
- bool hasProtectedMembers = func->ownerClass()->hasProtectedMembers();
- s << "::" << (hasProtectedMembers ? _wrapperName : func->ownerClass()->qualifiedCppName());
- s << "* " CPP_SELF_VAR " = ";
- s << (hasProtectedMembers ? QString("(%1*)").arg(_wrapperName) : "");
+ QString cppSelfAttribution = CPP_SELF_VAR" = ";
+ if (avoidProtectedHack() && metaClass->hasProtectedMembers())
+ cppSelfAttribution += QString("(%1*)").arg(className);
+ cppSelfAttribution += cpythonWrapperCPtr(metaClass, "self");
+
+ // Checks if the underlying C++ object is valid.
+ if (hasStaticOverload) {
+ s << INDENT << "if (self) {" << endl;
+ {
+ Indentation indent(INDENT);
+ writeInvalidCppObjectCheck(s, "self");
+ s << INDENT << cppSelfAttribution << ';' << endl;
+ }
+ s << INDENT << '}' << endl;
} else {
- s << func->ownerClass()->qualifiedCppName() << "* " CPP_SELF_VAR " = ";
+ writeInvalidCppObjectCheck(s, "self");
+ s << INDENT << cppSelfAttribution << ';' << endl;
+ }
+}
+
+void CppGenerator::writeCppSelfDefinition(QTextStream& s, const AbstractMetaFunction* func, bool hasStaticOverload)
+{
+ if (!func->ownerClass() || func->isConstructor())
+ return;
+
+ if (func->isOperatorOverload() && func->isBinaryOperator()) {
+ QString checkFunc = cpythonCheckFunction(func->ownerClass()->typeEntry());
+ s << INDENT << "bool isReverse = " << checkFunc << "(arg)" << endl;
+ {
+ Indentation indent1(INDENT);
+ Indentation indent2(INDENT);
+ Indentation indent3(INDENT);
+ Indentation indent4(INDENT);
+ s << INDENT << "&& !" << checkFunc << "(self);" << endl;
+ }
+ s << INDENT << "if (isReverse)" << endl;
+ Indentation indent(INDENT);
+ s << INDENT << "std::swap(self, arg);" << endl;
}
- s << cpythonWrapperCPtr(func->ownerClass(), "self") << ';' << endl;
+
+ writeCppSelfDefinition(s, func->ownerClass(), hasStaticOverload);
+
if (func->isUserAdded())
s << INDENT << "(void)" CPP_SELF_VAR "; // avoid warnings about unused variables" << endl;
}
diff --git a/generator/cppgenerator.h b/generator/cppgenerator.h
index e6add4e9d..1ce0c9d60 100644
--- a/generator/cppgenerator.h
+++ b/generator/cppgenerator.h
@@ -52,7 +52,8 @@ private:
void writeDestructorWrapper(QTextStream& s, const AbstractMetaClass* metaClass);
void writeMethodWrapper(QTextStream &s, const AbstractMetaFunctionList overloads);
void writeArgumentsInitializer(QTextStream& s, OverloadData& overloadData);
- void writeCppSelfDefinition(QTextStream& s, const AbstractMetaFunction* func);
+ void writeCppSelfDefinition(QTextStream& s, const AbstractMetaFunction* func, bool hasStaticOverload = false);
+ void writeCppSelfDefinition(QTextStream& s, const AbstractMetaClass* metaClass, bool hasStaticOverload = false);
void writeErrorSection(QTextStream& s, OverloadData& overloadData);
/**