aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Lima <hugo.lima@openbossa.org>2009-11-27 19:50:48 -0200
committerHugo Lima <hugo.lima@openbossa.org>2009-11-30 13:56:28 -0200
commit0986fe76a6caedcd77d0334efe2c09e38ff2a087 (patch)
tree133dbba6ded11b80bdb6b06b9a57124df7123ebf
parent78bf7bca1fe92052ae7b64827ae81bbe25bd8c3d (diff)
Add the generator flag "enable-parent-ctor-heuristic".
When enabled, this heuristic will check every constructor for an argument named "parent", if the argument is a pointer, then it'll be the parent of this object.
-rw-r--r--cppgenerator.cpp21
-rw-r--r--shibokengenerator.cpp15
-rw-r--r--shibokengenerator.h8
-rw-r--r--tests/samplebinding/CMakeLists.txt2
-rw-r--r--tests/samplebinding/typesystem_sample.xml5
5 files changed, 40 insertions, 11 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index 5847b5567..1460d4b77 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -2337,17 +2337,32 @@ void CppGenerator::writeParentChildManagement(QTextStream& s, const AbstractMeta
{
const int numArgs = func->arguments().count();
const AbstractMetaClass* cppClass = func->ownerClass();
+ bool ctorHeuristicEnabled = func->isConstructor() && useCtorHeuristic();
+
+ // -1 = return value
+ // 0 = self
+ // 1..n = func. args.
for (int i = -1; i <= numArgs; ++i) {
QString parentVariable;
QString childVariable;
ArgumentOwner argOwner = func->argumentOwner(cppClass, i);
bool usePyArgs = getMinMaxArguments(func).second > 1 || func->isConstructor();
- if (argOwner.action != ArgumentOwner::Invalid) {
+ ArgumentOwner::Action action = argOwner.action;
+ int childIndex = argOwner.index; // for argOwner.index, -1 is self... wtf?
+ if (ctorHeuristicEnabled && i > 0 && numArgs) {
+ AbstractMetaArgument* arg = func->arguments().at(i-1);
+ if (arg->argumentName() == "parent" && (arg->type()->isObject() || arg->type()->isQObject())) {
+ action = ArgumentOwner::Add;
+ childIndex = -1;
+ }
+ }
+
+ if (action != ArgumentOwner::Invalid) {
if (!usePyArgs && i > 1)
- ReportHandler::warning("");
+ ReportHandler::warning("Argument index for parent tag out of bounds: "+func->signature());
- if (argOwner.index == -1)
+ if (childIndex == -1)
childVariable = "self";
else
childVariable = usePyArgs ? "pyargs["+QString::number(argOwner.index-1)+"]" : "arg";
diff --git a/shibokengenerator.cpp b/shibokengenerator.cpp
index 5b6734cbf..e4d207c8a 100644
--- a/shibokengenerator.cpp
+++ b/shibokengenerator.cpp
@@ -29,7 +29,7 @@
#include <limits>
#define NULL_VALUE "NULL"
-#define COMMENT_LINE_WIDTH 77
+#define PARENT_CTOR_HEURISTIC "enable-parent-ctor-heuristic"
static Indentor INDENT;
//static void dumpFunction(AbstractMetaFunctionList lst);
@@ -1180,8 +1180,21 @@ QPair< int, int > ShibokenGenerator::getMinMaxArguments(const AbstractMetaFuncti
return qMakePair(minArgs, maxArgs);
}
+QMap<QString, QString> ShibokenGenerator::options() const
+{
+ QMap<QString, QString> opts(Generator::options());
+ opts.insert(PARENT_CTOR_HEURISTIC, "Enable heuristics to detect parent relationship on constructors.");
+ return opts;
+}
+
bool ShibokenGenerator::doSetup(const QMap<QString, QString>& args)
{
+ m_useCtorHeuristic = args.contains(PARENT_CTOR_HEURISTIC);
return true;
}
+bool ShibokenGenerator::useCtorHeuristic() const
+{
+ return m_useCtorHeuristic;
+}
+
diff --git a/shibokengenerator.h b/shibokengenerator.h
index af0b80a95..d78175144 100644
--- a/shibokengenerator.h
+++ b/shibokengenerator.h
@@ -253,9 +253,13 @@ public:
/// Returns the name of the macro used to export symbols
QString getApiExportMacro() const;
- bool doSetup(const QMap<QString, QString>& args);
+ QMap< QString, QString > options() const;
+ /// Returns true if the user enabled the so called "parent constructor heuristic".
+ bool useCtorHeuristic() const;
protected:
+ bool doSetup(const QMap<QString, QString>& args);
+
bool m_native_jump_table;
static QHash<QString, QString> m_pythonPrimitiveTypeName;
static QHash<QString, QString> m_pythonOperators;
@@ -281,6 +285,8 @@ protected:
AbstractMetaFunctionList filterFunctions(const AbstractMetaClass* metaClass);
AbstractMetaFunctionList queryGlobalOperators(const AbstractMetaClass* metaClass);
+private:
+ bool m_useCtorHeuristic;
};
diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt
index 4529056ed..a9d2118fd 100644
--- a/tests/samplebinding/CMakeLists.txt
+++ b/tests/samplebinding/CMakeLists.txt
@@ -59,7 +59,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/virtualmethods_wrapper.cpp
find_program(GENERATOR generatorrunner REQUIRED)
add_custom_command(OUTPUT ${sample_SRC}
-COMMAND ${GENERATOR} --generatorSet=${shiboken_BINARY_DIR}/shiboken_generator
+COMMAND ${GENERATOR} --generatorSet=${shiboken_BINARY_DIR}/shiboken_generator --enable-parent-ctor-heuristic
${CMAKE_CURRENT_SOURCE_DIR}/global.h
--include-paths=${libsample_SOURCE_DIR}
--typesystem-paths=${CMAKE_CURRENT_SOURCE_DIR}
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index 1b0228d64..ab4696d14 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -64,11 +64,6 @@
<modify-function signature="event(Event*)">
<modify-argument index="1" invalidate-after-use="yes"/>
</modify-function>
- <modify-function signature="ObjectType(ObjectType*)">
- <modify-argument index="1">
- <parent index="this" action="add"/>
- </modify-argument>
- </modify-function>
<modify-function signature="create()">
<modify-argument index="return">
<define-ownership owner="target"/>