Browse Source

Added a test for fixed arrays which showed that setting actually worked incorrectly.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/75/head
Dimitar Dobrev 12 years ago
parent
commit
acbdf7d82a
  1. 57
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 1
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 10
      tests/CSharpTemp/CSharpTemp.Tests.cs

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

@ -115,23 +115,21 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType) switch (array.SizeType)
{ {
case ArrayType.ArraySize.Constant: case ArrayType.ArraySize.Constant:
StringBuilder arrayBuilder = new StringBuilder(); var supportBefore = Context.SupportBefore;
arrayBuilder.AppendFormat("{0}[] value = new {0}[{1}];", array.Type, array.Size); string value = Generator.GeneratedIdentifier("value");
arrayBuilder.AppendLine(); supportBefore.WriteLine("{0}[] {1} = new {0}[{2}];", array.Type, value, array.Size);
arrayBuilder.AppendFormat("fixed ({0}* v = value)", array.Type); string v = Generator.GeneratedIdentifier("v");
arrayBuilder.AppendLine(); supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, value);
arrayBuilder.AppendLine("{"); supportBefore.WriteStartBraceIndent();
arrayBuilder.AppendFormat(" {0}* to = v;", array.Type); string to = Generator.GeneratedIdentifier("to");
arrayBuilder.AppendLine(); supportBefore.WriteLine("{0}* {1} = {2};", array.Type, to, v);
arrayBuilder.AppendFormat(" {0}* from = {1}->{2};", string from = Generator.GeneratedIdentifier("from");
array.Type, Generator.GeneratedIdentifier("ptr"), Context.ArgName); supportBefore.WriteLine("{0}* {1} = {2}->{3};",
arrayBuilder.AppendLine(); array.Type, from, Generator.GeneratedIdentifier("ptr"), Context.ArgName);
arrayBuilder.AppendFormat(" for (int i = 0; i < {0}; i++)", array.Size); supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
arrayBuilder.AppendLine(); supportBefore.WriteLineIndent("*{0}++ = *{1}++;", to, from);
arrayBuilder.AppendLine(" *to++ = *from++;"); supportBefore.WriteCloseBraceIndent();
arrayBuilder.Append("}"); Context.Return.Write(value);
Context.SupportBefore.WriteLine(arrayBuilder.ToString());
Context.Return.Write("value");
break; break;
case ArrayType.ArraySize.Variable: case ArrayType.ArraySize.Variable:
Context.Return.Write("null"); Context.Return.Write("null");
@ -377,18 +375,23 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType) switch (array.SizeType)
{ {
case ArrayType.ArraySize.Constant: case ArrayType.ArraySize.Constant:
StringBuilder arrayBuilder = new StringBuilder(); var supportBefore = Context.SupportBefore;
arrayBuilder.AppendFormat("if (value.Length != {0})", array.Size); supportBefore.WriteLine("if ({0}.Length != {1})",
arrayBuilder.AppendLine(); Context.ArgName, array.Size);
arrayBuilder.AppendFormat( supportBefore.WriteLineIndent(
"throw new ArgumentException(\"The value must be an array of size {0}.\");", "throw new ArgumentException(\"The value must be an array of size {0}.\");",
array.Size); array.Size);
arrayBuilder.AppendLine(); string v = Generator.GeneratedIdentifier("v");
arrayBuilder.AppendFormat("fixed ({0}* v = value)", array.Type); supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, Context.ArgName);
arrayBuilder.AppendLine(); supportBefore.WriteLineIndent("for (int i = 0; i < {0}; i++)", array.Size);
arrayBuilder.Append(" *"); supportBefore.PushIndent();
Context.SupportBefore.Write(arrayBuilder.ToString()); supportBefore.WriteLineIndent("*({0}->{1} + i) = *({2} + i);",
Context.Return.Write("*v"); Generator.GeneratedIdentifier("ptr"), Context.ReturnVarName, v);
supportBefore.PopIndent();
supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, Context.ArgName);
supportBefore.PushIndent();
supportBefore.Write("*");
Context.Return.Write("*{0}", v);
break; break;
default: default:
Context.Return.Write("null"); Context.Return.Write("null");

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

@ -826,6 +826,7 @@ namespace CppSharp.Generators.CSharp
Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.ReturnVarName = field.OriginalName;
param.Visit(marshal); param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))

10
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -31,6 +31,16 @@ public class CSharpTempTests
BindingFlags.Instance | BindingFlags.NonPublic), Is.Not.Null); BindingFlags.Instance | BindingFlags.NonPublic), Is.Not.Null);
} }
[Test]
public void TestFixedArrays()
{
Qux qux = new Qux();
var array = new[] { 1, 2, 3 };
qux.array = array;
for (int i = 0; i < qux.array.Length; i++)
Assert.That(array[i], Is.EqualTo(qux.array[i]));
}
[Test] [Test]
public void TestMultipleInheritance() public void TestMultipleInheritance()
{ {

Loading…
Cancel
Save