From 3a74428f94946d485829a02daab10466e9124e8b Mon Sep 17 00:00:00 2001 From: triton Date: Thu, 19 Dec 2013 17:42:25 +0000 Subject: [PATCH] Fixed delegate generation regression and added some tests. --- src/Generator/Generators/CLI/CLIHeadersTemplate.cs | 7 +++++-- tests/Basic/Basic.Tests.cs | 8 ++++++++ tests/Basic/Basic.h | 14 +++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index e9893f0b..964e2d91 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -615,12 +615,15 @@ namespace CppSharp.Generators.CLI return false; FunctionType function; - if (typedef.Type.IsPointerTo(out function)) + if (typedef.Type.IsPointerTo(out function)) { PushBlock(CLIBlockKind.Typedef, typedef); GenerateDeclarationCommon(typedef); - WriteLine("{0};", + var insideClass = typedef.Namespace is Class; + + WriteLine("{0}{1};", + !insideClass ? "public " : "", string.Format(TypePrinter.VisitDelegate(function), SafeIdentifier(typedef.Name))); PopBlock(NewLineKind.BeforeNextBlock); diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index bb683f3c..5b203a20 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -151,5 +151,13 @@ public class BasicTests Assert.AreEqual(300, new Bar2.Nested()); Assert.AreEqual(500, new Bar2()); } + + [Test] + public void TestDelegates() + { + var delegates = new TestDelegates(); + var doubleSum = delegates.A(2) + delegates.B(2); + Assert.AreEqual(8, doubleSum); + } } \ No newline at end of file diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index de37d3cc..21603797 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -120,7 +120,6 @@ public: virtual int pureFunction(int i) = 0; virtual int pureFunction1() = 0; virtual int pureFunction2() = 0; - typedef void (*QTextStreamFunction)(int &); }; class DLL_API ImplementsAbstractFoo : public AbstractFoo @@ -194,3 +193,16 @@ class DLL_API basic DLL_API int test(basic& s); +// Tests delegates +typedef int (*DelegateInGlobalNamespace)(int); + +struct DLL_API TestDelegates +{ + typedef int (*DelegateInClass)(int); + + TestDelegates() : A(Double), B(Double) {} + static int Double(int N) { return N * 2; } + + DelegateInClass A; + DelegateInGlobalNamespace B; +};