Browse Source

Fixed return of structures by value in C# backend similarly to the CLI backend by using copy constructors if the class has any non-trivial ones, or just copying memory directly if the class only has trivial ones.

pull/45/merge
triton 12 years ago
parent
commit
fb481baf7d
  1. 34
      src/Generator/Generators/CSharp/CSharpMarshal.cs

34
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
using CppSharp.Types;
using Type = CppSharp.AST.Type;
@ -208,6 +209,39 @@ namespace CppSharp.Generators.CSharp @@ -208,6 +209,39 @@ namespace CppSharp.Generators.CSharp
instance = string.Format("new global::System.IntPtr(&{0})", instance);
}
if (@class.IsRefType)
{
var instanceName = Helpers.GeneratedIdentifier("instance");
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
instanceName, @class.Layout.Size);
if (@class.HasNonTrivialCopyConstructor)
{
// Find a valid copy constructor overload.
var copyCtorMethod = @class.Methods.FirstOrDefault(method =>
method.IsCopyConstructor);
if (copyCtorMethod == null)
throw new NotSupportedException("Expected a valid copy constructor");
// Call the copy constructor.
Context.SupportBefore.WriteLine("{0}.Internal.{1}({2}, new global::System.IntPtr(&{3}));",
@class.QualifiedName,
CSharpTextTemplate.GetFunctionNativeIdentifier(copyCtorMethod),
instanceName, instance);
}
else
{
Context.SupportBefore.WriteLine(
"CppSharp.Runtime.Helpers.memcpy({0}, new IntPtr(&{1}), new UIntPtr({2}));",
instanceName, instance, @class.Layout.Size);
}
instance = instanceName;
}
Context.Return.Write("new {0}({1})", QualifiedIdentifier(@class),
instance);

Loading…
Cancel
Save