From 5a21a7facd0866057aca7cef892dbb4398481cad Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sat, 30 Oct 2021 20:19:05 +0300 Subject: [PATCH] Generate valid C# for copy ctors with extra args Signed-off-by: Dimitar Dobrev --- src/Generator/Generators/CSharp/CSharpSources.cs | 6 +++++- tests/Common/Common.cpp | 5 +++++ tests/Common/Common.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 79ba9ccd..9c88a4e1 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -2501,7 +2501,11 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty // Allocate memory for a new native object and call the ctor. WriteLine($"var ret = Marshal.AllocHGlobal(sizeof({@internal}));"); var printed = TypePrinter.PrintNative(@class); - WriteLine($"{printed}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new {TypePrinter.IntPtrType}(&native));", + string defaultValue = string.Empty; + if (copyCtorMethod.Parameters.Count > 1) + defaultValue = $", {ExpressionPrinter.VisitParameter(copyCtorMethod.Parameters.Last())}"; + WriteLine($@"{printed}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new { + TypePrinter.IntPtrType}(&native){defaultValue});", printed, GetFunctionNativeIdentifier(copyCtorMethod)); WriteLine("return ret.ToPointer();"); } diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 48a72887..04d63cdd 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -465,6 +465,11 @@ ClassA::ClassA(int value) Value = value; } +ClassA::ClassA(const ClassA& other, bool param) +{ + Value = other.Value; +} + ClassB::ClassB(const ClassA& x) { Value = x.Value; diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 3f9c9c0c..1063b741 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -713,6 +713,7 @@ class DLL_API ClassA { public: ClassA(int value); + ClassA(const ClassA& other, bool param = true); int Value; }; class DLL_API ClassB