Browse Source

Merge pull request #75 from ddobrev/master

Fixed arrays
pull/76/merge
João Matos 12 years ago
parent
commit
3ff2383a08
  1. 42
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 10
      tests/CSharpTemp/CSharpTemp.Tests.cs
  4. 1
      tests/CSharpTemp/CSharpTemp.h

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

@ -115,7 +115,21 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType) switch (array.SizeType)
{ {
case ArrayType.ArraySize.Constant: case ArrayType.ArraySize.Constant:
Context.Return.Write("null"); var supportBefore = Context.SupportBefore;
string value = Generator.GeneratedIdentifier("value");
supportBefore.WriteLine("{0}[] {1} = new {0}[{2}];", array.Type, value, array.Size);
string v = Generator.GeneratedIdentifier("v");
supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, value);
supportBefore.WriteStartBraceIndent();
string to = Generator.GeneratedIdentifier("to");
supportBefore.WriteLine("{0}* {1} = {2};", array.Type, to, v);
string from = Generator.GeneratedIdentifier("from");
supportBefore.WriteLine("{0}* {1} = {2}->{3};",
array.Type, from, Generator.GeneratedIdentifier("ptr"), Context.ArgName);
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLineIndent("*{0}++ = *{1}++;", to, from);
supportBefore.WriteCloseBraceIndent();
Context.Return.Write(value);
break; break;
case ArrayType.ArraySize.Variable: case ArrayType.ArraySize.Variable:
Context.Return.Write("null"); Context.Return.Write("null");
@ -358,7 +372,31 @@ namespace CppSharp.Generators.CSharp
if (!VisitType(array, quals)) if (!VisitType(array, quals))
return false; return false;
Context.Return.Write("null"); switch (array.SizeType)
{
case ArrayType.ArraySize.Constant:
var supportBefore = Context.SupportBefore;
supportBefore.WriteLine("if ({0}.Length != {1})",
Context.ArgName, array.Size);
supportBefore.WriteLineIndent(
"throw new ArgumentException(\"The value must be an array of size {0}.\");",
array.Size);
string v = Generator.GeneratedIdentifier("v");
supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, Context.ArgName);
supportBefore.WriteLineIndent("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.PushIndent();
supportBefore.WriteLineIndent("*({0}->{1} + i) = *({2} + i);",
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;
default:
Context.Return.Write("null");
break;
}
return true; return true;
} }

4
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))
@ -1806,7 +1807,8 @@ namespace CppSharp.Generators.CSharp
Class retClass = null; Class retClass = null;
hiddenParam.Type.Desugar().IsTagDecl(out retClass); hiddenParam.Type.Desugar().IsTagDecl(out retClass);
WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"), QualifiedIdentifier(retClass)); WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"),
QualifiedIdentifier(retClass.OriginalClass ?? retClass));
} }
var names = new List<string>(); var names = new List<string>();

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()
{ {

1
tests/CSharpTemp/CSharpTemp.h

@ -22,6 +22,7 @@ class DLL_API Qux
{ {
public: public:
int farAwayFunc() const; int farAwayFunc() const;
int array[3];
}; };
class DLL_API Bar : public Qux class DLL_API Bar : public Qux

Loading…
Cancel
Save