Browse Source

Marshalled pointers to primitive types as such instead of using IntPtr.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/63/head
Dimitar Dobrev 12 years ago
parent
commit
6edc1ac19b
  1. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 5
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 19
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 6
      tests/CSharpTemp/CSharpTemp.Tests.cs

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

@ -382,7 +382,7 @@ namespace CppSharp.Generators.CSharp @@ -382,7 +382,7 @@ namespace CppSharp.Generators.CSharp
type.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst)
{
Context.Return.Write(this.MarshalStringToUnmanaged(
Context.Return.Write(MarshalStringToUnmanaged(
Helpers.SafeIdentifier(Context.Parameter.Name)));
CSharpContext.Cleanup.WriteLine("Marshal.FreeHGlobal({0});",
Helpers.SafeIdentifier(Context.ArgName));
@ -430,7 +430,7 @@ namespace CppSharp.Generators.CSharp @@ -430,7 +430,7 @@ namespace CppSharp.Generators.CSharp
Context.SupportBefore.WriteLine("{0} _{1};", typeName,
Helpers.SafeIdentifier(param.Name));
Context.Return.Write("new global::System.IntPtr(&_{0})", param.Name);
Context.Return.Write("&_{0}", param.Name);
}
else

5
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -794,8 +794,7 @@ namespace CppSharp.Generators.CSharp @@ -794,8 +794,7 @@ namespace CppSharp.Generators.CSharp
PrimitiveType primitiveType;
if (type.IsPrimitiveType(out primitiveType))
{
WriteLine("*({0}*) this[{1}] = *({0}*) value;",
type.ToString(), function.Parameters[0].Name);
WriteLine("*this[{0}] = *value;", function.Parameters[0].Name);
}
else
{
@ -2002,7 +2001,7 @@ namespace CppSharp.Generators.CSharp @@ -2002,7 +2001,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("[UnmanagedFunctionPointerAttribute(CallingConvention.{0})]",
Helpers.ToCSharpCallConv(functionType.CallingConvention));
TypePrinter.PushContext(CSharpTypePrinterContextKind.Native);
WriteLine("{0} {1};",
WriteLine("{0} unsafe {1};",
typedef.Access == AccessSpecifier.Public ? "public" : "protected",
string.Format(TypePrinter.VisitDelegate(functionType).Type,
SafeIdentifier(typedef.Name)));

19
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using CppSharp.AST;
using CppSharp.Types;
using Type = CppSharp.AST.Type;
@ -10,7 +10,8 @@ namespace CppSharp.Generators.CSharp @@ -10,7 +10,8 @@ namespace CppSharp.Generators.CSharp
{
Native,
Managed,
ManagedPointer
ManagedPointer,
GenericDelegate
}
public class CSharpTypePrinterContext : TypePrinterContext
@ -115,10 +116,14 @@ namespace CppSharp.Generators.CSharp @@ -115,10 +116,14 @@ namespace CppSharp.Generators.CSharp
{
var arguments = function.Parameters;
var returnType = function.ReturnType;
var args = string.Empty;
var args = string.Empty;
PushContext(CSharpTypePrinterContextKind.GenericDelegate);
if (arguments.Count > 0)
args = VisitParameters(function.Parameters, hasNames: false).Type;
args = VisitParameters(function.Parameters, hasNames: false).Type;
PopContext();
if (ContextKind != CSharpTypePrinterContextKind.Managed)
return "global::System.IntPtr";
@ -179,7 +184,9 @@ namespace CppSharp.Generators.CSharp @@ -179,7 +184,9 @@ namespace CppSharp.Generators.CSharp
(Context.Parameter.IsOut || Context.Parameter.IsInOut))
return VisitPrimitiveType(primitive, quals);
return "global::System.IntPtr";
if (ContextKind == CSharpTypePrinterContextKind.GenericDelegate)
return "global::System.IntPtr";
return VisitPrimitiveType(primitive, quals) + "*";
}
Class @class;

6
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -13,11 +13,11 @@ public class CSharpTempTests @@ -13,11 +13,11 @@ public class CSharpTempTests
var foo = new Foo();
// TODO: Most of the ugliness below will disappear when pointers to simple types are represented by C#-pointers or ref modifiers instead of the nasty IntPtr
var value = *(int*) foo[0];
var value = *foo[0];
Assert.That(value, Is.EqualTo(50));
int x = 250;
foo[0] = new IntPtr(&x);
value = *(int*) foo[0];
foo[0] = &x;
value = *foo[0];
Assert.That(value, Is.EqualTo(x));
Assert.That(foo[(uint) 0], Is.EqualTo(15));

Loading…
Cancel
Save