Skip to content

Commit e31dc22

Browse files
Build Agentddobrev
authored andcommitted
CreateInstance factory overload to pass flag informing wrapper if it should own the native pointer passed to it.
1 parent dfa2436 commit e31dc22

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Generator/Generators/CLI/CLIHeaders.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ public void GenerateClassConstructors(Class @class, string nativeType)
380380
WriteLine("static {0}^ {1}(::System::IntPtr native);",
381381
@class.Name, Helpers.CreateInstanceIdentifier);
382382

383+
WriteLine($"static {@class.Name}^ {Helpers.CreateInstanceIdentifier}(::System::IntPtr native, bool {Helpers.OwnsNativeInstanceIdentifier});");
384+
383385
foreach (var ctor in @class.Constructors)
384386
{
385387
if (ASTUtils.CheckIgnoreMethod(ctor) || FunctionIgnored(ctor))

src/Generator/Generators/CLI/CLISources.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,21 @@ private void GenerateClassConstructor(Class @class, bool withOwnNativeInstancePa
670670

671671
UnindentAndWriteCloseBrace();
672672

673-
if (!withOwnNativeInstanceParam)
674-
{
675-
NewLine();
676-
WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier);
673+
string createInstanceParams = withOwnNativeInstanceParam ? $"::System::IntPtr native, bool {Helpers.OwnsNativeInstanceIdentifier}" : "::System::IntPtr native";
674+
string createInstanceParamsValues = withOwnNativeInstanceParam ? $"({nativeType}) native.ToPointer(), {Helpers.OwnsNativeInstanceIdentifier}" : $"({nativeType}) native.ToPointer()";
677675

678-
WriteOpenBraceAndIndent();
676+
NewLine();
677+
WriteLine($"{qualifiedIdentifier}^ {qualifiedIdentifier}::{Helpers.CreateInstanceIdentifier}({createInstanceParams})");
679678

680-
WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType);
679+
WriteOpenBraceAndIndent();
681680

682-
UnindentAndWriteCloseBrace();
683-
NewLine();
681+
WriteLine($"return gcnew ::{qualifiedIdentifier}({createInstanceParamsValues});");
682+
683+
UnindentAndWriteCloseBrace();
684+
NewLine();
684685

686+
if (!withOwnNativeInstanceParam)
687+
{
685688
GenerateClassConstructor(@class, true);
686689
}
687690
}

0 commit comments

Comments
 (0)