Browse Source

Reduced the indentation at the marshalling of arrays in C#.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/987/head
Dimitar Dobrev 9 years ago
parent
commit
fd9c9520c8
  1. 71
      src/Generator/Generators/CSharp/CSharpMarshal.cs

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

@ -82,22 +82,23 @@ namespace CppSharp.Generators.CSharp @@ -82,22 +82,23 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType)
{
case ArrayType.ArraySize.Constant:
if (Context.MarshalKind == MarshalKind.NativeField ||
Context.MarshalKind == MarshalKind.ReturnVariableArray)
{
if (Context.MarshalKind != MarshalKind.NativeField &&
Context.MarshalKind != MarshalKind.ReturnVariableArray)
goto case ArrayType.ArraySize.Incomplete;
var supportBefore = Context.Before;
string value = Generator.GeneratedIdentifier("value");
supportBefore.WriteLine("{0}[] {1} = null;", array.Type, value, array.Size);
supportBefore.WriteLine("if ({0} != null)", Context.ReturnVarName);
var arrayType = array.Type.Desugar();
supportBefore.WriteLine($"{arrayType}[] {value} = null;");
supportBefore.WriteLine($"if ({Context.ReturnVarName} != null)");
supportBefore.WriteStartBraceIndent();
supportBefore.WriteLine("{0} = new {1}[{2}];", value, array.Type, array.Size);
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLine($"{value} = new {arrayType}[{array.Size}];");
supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)");
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void))
supportBefore.WriteLineIndent("{0}[i] = new global::System.IntPtr({1}[i]);",
value, Context.ReturnVarName);
supportBefore.WriteLineIndent($@"{value}[i] = new global::System.IntPtr({
Context.ReturnVarName}[i]);");
else
{
var arrayType = array.Type.Desugar();
var finalArrayType = arrayType.GetPointee() ?? arrayType;
Class @class;
if ((finalArrayType.TryGetClass(out @class)) && @class.IsRefType)
@ -116,25 +117,15 @@ namespace CppSharp.Generators.CSharp @@ -116,25 +117,15 @@ namespace CppSharp.Generators.CSharp
{
if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
Context.Context.Options.MarshalCharAsManagedChar)
{
supportBefore.WriteLineIndent(
"{0}[i] = global::System.Convert.ToChar({1}[i]);",
value, Context.ReturnVarName);
}
supportBefore.WriteLineIndent($@"{value}[i] = global::System.Convert.ToChar({
Context.ReturnVarName}[i]);");
else
{
supportBefore.WriteLineIndent("{0}[i] = {1}[i];",
value, Context.ReturnVarName);
}
supportBefore.WriteLineIndent($@"{value}[i] = {
Context.ReturnVarName}[i];");
}
}
supportBefore.WriteCloseBraceIndent();
Context.Return.Write(value);
}
else
{
goto case ArrayType.ArraySize.Incomplete;
}
break;
case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string
@ -495,23 +486,20 @@ namespace CppSharp.Generators.CSharp @@ -495,23 +486,20 @@ namespace CppSharp.Generators.CSharp
{
case ArrayType.ArraySize.Constant:
if (string.IsNullOrEmpty(Context.ReturnVarName))
{
goto case ArrayType.ArraySize.Incomplete;
}
else
{
var supportBefore = Context.Before;
supportBefore.WriteLine("if ({0} != null)", Context.ArgName);
supportBefore.WriteLine($"if ({Context.ArgName} != null)");
supportBefore.WriteStartBraceIndent();
Class @class;
var arrayType = array.Type.Desugar();
var finalArrayType = arrayType.GetPointee() ?? arrayType;
if (finalArrayType.TryGetClass(out @class) && @class.IsRefType)
{
supportBefore.WriteLine("if (value.Length != {0})", array.Size);
supportBefore.WriteLine($"if (value.Length != {array.Size})");
ThrowArgumentOutOfRangeException();
}
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)");
if (@class != null && @class.IsRefType)
{
if (finalArrayType == arrayType)
@ -528,23 +516,16 @@ namespace CppSharp.Generators.CSharp @@ -528,23 +516,16 @@ namespace CppSharp.Generators.CSharp
{
if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
Context.Context.Options.MarshalCharAsManagedChar)
{
supportBefore.WriteLineIndent(
"{0}[i] = global::System.Convert.ToSByte({1}[i]);",
Context.ReturnVarName, Context.ArgName);
}
supportBefore.WriteLineIndent($@"{
Context.ReturnVarName}[i] = global::System.Convert.ToSByte({
Context.ArgName}[i]);");
else
{
supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};",
Context.ReturnVarName,
Context.ArgName,
arrayType.IsPointerToPrimitiveType(PrimitiveType.Void)
? ".ToPointer()"
: string.Empty);
}
supportBefore.WriteLineIndent($@"{Context.ReturnVarName}[i] = {
Context.ArgName}[i]{
(arrayType.IsPointerToPrimitiveType(PrimitiveType.Void) ?
".ToPointer()" : string.Empty)};");
}
supportBefore.WriteCloseBraceIndent();
}
break;
case ArrayType.ArraySize.Incomplete:
MarshalArray(array);

Loading…
Cancel
Save