Skip to content

Commit 8b044a4

Browse files
committed
initial support for ownership handling
1 parent 6e7828b commit 8b044a4

7 files changed

Lines changed: 53 additions & 14 deletions

File tree

generator/generator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class Generator : public QObject
8888
ConvertReferenceToPtr = 0x00800000,
8989
UseIndexedName = 0x01000000,
9090
ProtectedEnumAsInts = 0x02000000,
91+
AddOwnershipTemplates = 0x04000000,
9192

9293
ForceValueType = ExcludeReference | ExcludeConst
9394
};

generator/shellgenerator.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,29 @@ void ShellGenerator::writeTypeInfo(QTextStream &s, const AbstractMetaType *type,
124124
}
125125

126126

127-
void ShellGenerator::writeFunctionArguments(QTextStream &s, const AbstractMetaClass* owner,
128-
const AbstractMetaArgumentList &arguments,
129-
Option option,
130-
int numArguments)
127+
void ShellGenerator::writeFunctionArguments(QTextStream &s,
128+
const AbstractMetaFunction *meta_function,
129+
Option option,
130+
int numArguments)
131131
{
132+
const AbstractMetaClass* owner = meta_function->ownerClass();
133+
const AbstractMetaArgumentList &arguments = meta_function->arguments();
134+
132135
if (numArguments < 0) numArguments = arguments.size();
133136

134137
for (int i=0; i<numArguments; ++i) {
135138
if (i != 0)
136139
s << ", ";
137140
AbstractMetaArgument *arg = arguments.at(i);
141+
TypeSystem::Ownership ownership = TypeSystem::InvalidOwnership;
142+
if (option & AddOwnershipTemplates) {
143+
ownership = writeOwnershipTemplate(s, meta_function, i+1);
144+
}
138145
writeTypeInfo(s, arg->type(), option);
146+
if (ownership != TypeSystem::InvalidOwnership) {
147+
s << "> ";
148+
}
149+
139150
if (!(option & SkipName)) {
140151
if (option & UseIndexedName) {
141152
s << " " << arg->indexedName();
@@ -194,8 +205,15 @@ void ShellGenerator::writeFunctionSignature(QTextStream &s,
194205

195206
if ((option & SkipReturnType) == 0) {
196207
if (function_type) {
197-
writeTypeInfo(s, function_type, option);
198-
s << " ";
208+
TypeSystem::Ownership ownership = TypeSystem::InvalidOwnership;
209+
if (option & AddOwnershipTemplates) {
210+
ownership = writeOwnershipTemplate(s, meta_function, 0);
211+
}
212+
writeTypeInfo(s, function_type, option);
213+
s << " ";
214+
if (ownership != TypeSystem::InvalidOwnership) {
215+
s << "> ";
216+
}
199217
} else if (!meta_function->isConstructor()) {
200218
s << "void ";
201219
}
@@ -245,7 +263,7 @@ void ShellGenerator::writeFunctionSignature(QTextStream &s,
245263
}
246264
}
247265

248-
writeFunctionArguments(s, meta_function->ownerClass(), meta_function->arguments(), Option(option & Option(~ConvertReferenceToPtr)), numArguments);
266+
writeFunctionArguments(s, meta_function, Option(option & Option(~ConvertReferenceToPtr)), numArguments);
249267

250268
// The extra arguments...
251269
for (int i=0; i<extra_arguments.size(); ++i) {
@@ -409,3 +427,18 @@ bool ShellGenerator::isBuiltIn(const QString& name) {
409427
}
410428
return builtIn.contains(name);
411429
}
430+
431+
TypeSystem::Ownership ShellGenerator::writeOwnershipTemplate(QTextStream & s, const AbstractMetaFunction * meta_function, int argumentIndex)
432+
{
433+
TypeSystem::Ownership ownership = meta_function->ownership(meta_function->ownerClass(), TypeSystem::TargetLangCode, argumentIndex);
434+
if (ownership == TypeSystem::CppOwnership) {
435+
s << "PythonQtPassOwnershipToCPP<";
436+
}
437+
else if (ownership == TypeSystem::TargetLangOwnership) {
438+
s << "PythonQtPassOwnershipToPython<";
439+
}
440+
else if (ownership == TypeSystem::TargetLangThisOwnership) {
441+
s << "PythonQtNewOwnerOfThis<";
442+
}
443+
return ownership;
444+
}

generator/shellgenerator.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ class ShellGenerator : public Generator
6666
const QString &classname_prefix = QString(),
6767
const QStringList &extra_arguments = QStringList(),
6868
int numArguments = -1);
69-
void writeFunctionArguments(QTextStream &s, const AbstractMetaClass* owner, const AbstractMetaArgumentList &arguments,
69+
70+
TypeSystem::Ownership writeOwnershipTemplate(QTextStream & s, const AbstractMetaFunction * meta_function, int argumentIndex);
71+
72+
void writeFunctionArguments(QTextStream &s, const AbstractMetaFunction *meta_function,
7073
Option option = NoOption,
7174
int numArguments = -1);
7275

generator/shellheadergenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ void ShellHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *meta_c
311311

312312
s << meta_class->qualifiedCppName() << "* ";
313313
writeFunctionSignature(s, fun, 0, "new_",
314-
Option(IncludeDefaultExpression | OriginalName | ShowStatic));
314+
Option(IncludeDefaultExpression | OriginalName | ShowStatic | AddOwnershipTemplates));
315315
s << ";" << endl;
316316
if (fun->arguments().size()==1 && meta_class->qualifiedCppName() == fun->arguments().at(0)->type()->typeEntry()->qualifiedCppName()) {
317317
copyConstructorSeen = true;
@@ -345,7 +345,7 @@ void ShellHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *meta_c
345345

346346
s << " ";
347347
writeFunctionSignature(s, function, 0, QString(),
348-
Option(ConvertReferenceToPtr | FirstArgIsWrappedObject | IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces | ProtectedEnumAsInts));
348+
Option(AddOwnershipTemplates | ConvertReferenceToPtr | FirstArgIsWrappedObject | IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces | ProtectedEnumAsInts));
349349
s << ";" << endl;
350350
}
351351
}

generator/shellimplgenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
223223

224224
s << meta_class->qualifiedCppName() << "* ";
225225
s << "PythonQtWrapper_" << meta_class->name() << "::";
226-
writeFunctionSignature(s, ctor, 0, "new_", Option(OriginalName | ShowStatic));
226+
writeFunctionSignature(s, ctor, 0, "new_", Option(AddOwnershipTemplates | OriginalName | ShowStatic));
227227
s << endl;
228228
s << "{ " << endl;
229229
s << "return new " << (meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName()) << "(";
@@ -247,7 +247,7 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
247247
continue;
248248
}
249249
writeFunctionSignature(s, fun, meta_class, QString(),
250-
Option(ConvertReferenceToPtr | FirstArgIsWrappedObject | OriginalName | ShowStatic | UnderscoreSpaces | ProtectedEnumAsInts),
250+
Option(AddOwnershipTemplates | ConvertReferenceToPtr | FirstArgIsWrappedObject | OriginalName | ShowStatic | UnderscoreSpaces | ProtectedEnumAsInts),
251251
"PythonQtWrapper_");
252252
s << endl << "{" << endl;
253253
s << " ";

generator/typesystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,10 @@ bool Handler::startElement(const QString &, const QString &n,
960960

961961
static QHash<QString, TypeSystem::Ownership> ownershipNames;
962962
if (ownershipNames.isEmpty()) {
963-
ownershipNames["java"] = TypeSystem::TargetLangOwnership;
963+
ownershipNames["python"] = TypeSystem::TargetLangOwnership;
964964
ownershipNames["c++"] = TypeSystem::CppOwnership;
965965
ownershipNames["default"] = TypeSystem::DefaultOwnership;
966+
ownershipNames["new-owner-of-this"] = TypeSystem::TargetLangThisOwnership;
966967
}
967968

968969
QString ownershipAttribute = attributes["owner"].toLower();

generator/typesystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ namespace TypeSystem {
127127
InvalidOwnership,
128128
DefaultOwnership,
129129
TargetLangOwnership,
130-
CppOwnership
130+
CppOwnership,
131+
TargetLangThisOwnership
131132
};
132133
};
133134

0 commit comments

Comments
 (0)