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. 163
      src/Generator/Generators/CSharp/CSharpMarshal.cs

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

@ -82,59 +82,50 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType) switch (array.SizeType)
{ {
case ArrayType.ArraySize.Constant: case ArrayType.ArraySize.Constant:
if (Context.MarshalKind == MarshalKind.NativeField || if (Context.MarshalKind != MarshalKind.NativeField &&
Context.MarshalKind == MarshalKind.ReturnVariableArray) Context.MarshalKind != MarshalKind.ReturnVariableArray)
goto case ArrayType.ArraySize.Incomplete;
var supportBefore = Context.Before;
string value = Generator.GeneratedIdentifier("value");
var arrayType = array.Type.Desugar();
supportBefore.WriteLine($"{arrayType}[] {value} = null;");
supportBefore.WriteLine($"if ({Context.ReturnVarName} != null)");
supportBefore.WriteStartBraceIndent();
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($@"{value}[i] = new global::System.IntPtr({
Context.ReturnVarName}[i]);");
else
{ {
var supportBefore = Context.Before; var finalArrayType = arrayType.GetPointee() ?? arrayType;
string value = Generator.GeneratedIdentifier("value"); Class @class;
supportBefore.WriteLine("{0}[] {1} = null;", array.Type, value, array.Size); if ((finalArrayType.TryGetClass(out @class)) && @class.IsRefType)
supportBefore.WriteLine("if ({0} != null)", Context.ReturnVarName); {
supportBefore.WriteStartBraceIndent(); if (arrayType == finalArrayType)
supportBefore.WriteLine("{0} = new {1}[{2}];", value, array.Type, array.Size); supportBefore.WriteLineIndent(
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size); "{0}[i] = {1}.{2}(*(({1}.{3}*)&({4}[i * sizeof({1}.{3})])));",
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void)) value, array.Type, Helpers.CreateInstanceIdentifier,
supportBefore.WriteLineIndent("{0}[i] = new global::System.IntPtr({1}[i]);", Helpers.InternalStruct, Context.ReturnVarName);
value, Context.ReturnVarName); else
supportBefore.WriteLineIndent(
$@"{value}[i] = {finalArrayType}.{Helpers.CreateInstanceIdentifier}(({
CSharpTypePrinter.IntPtrType}) {Context.ReturnVarName}[i]);");
}
else else
{ {
var arrayType = array.Type.Desugar(); if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
var finalArrayType = arrayType.GetPointee() ?? arrayType; Context.Context.Options.MarshalCharAsManagedChar)
Class @class; supportBefore.WriteLineIndent($@"{value}[i] = global::System.Convert.ToChar({
if ((finalArrayType.TryGetClass(out @class)) && @class.IsRefType) Context.ReturnVarName}[i]);");
{
if (arrayType == finalArrayType)
supportBefore.WriteLineIndent(
"{0}[i] = {1}.{2}(*(({1}.{3}*)&({4}[i * sizeof({1}.{3})])));",
value, array.Type, Helpers.CreateInstanceIdentifier,
Helpers.InternalStruct, Context.ReturnVarName);
else
supportBefore.WriteLineIndent(
$@"{value}[i] = {finalArrayType}.{Helpers.CreateInstanceIdentifier}(({
CSharpTypePrinter.IntPtrType}) {Context.ReturnVarName}[i]);");
}
else else
{ supportBefore.WriteLineIndent($@"{value}[i] = {
if (arrayType.IsPrimitiveType(PrimitiveType.Char) && Context.ReturnVarName}[i];");
Context.Context.Options.MarshalCharAsManagedChar)
{
supportBefore.WriteLineIndent(
"{0}[i] = global::System.Convert.ToChar({1}[i]);",
value, Context.ReturnVarName);
}
else
{
supportBefore.WriteLineIndent("{0}[i] = {1}[i];",
value, Context.ReturnVarName);
}
}
} }
supportBefore.WriteCloseBraceIndent();
Context.Return.Write(value);
}
else
{
goto case ArrayType.ArraySize.Incomplete;
} }
supportBefore.WriteCloseBraceIndent();
Context.Return.Write(value);
break; break;
case ArrayType.ArraySize.Incomplete: case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string // const char* and const char[] are the same so we can use a string
@ -495,56 +486,46 @@ namespace CppSharp.Generators.CSharp
{ {
case ArrayType.ArraySize.Constant: case ArrayType.ArraySize.Constant:
if (string.IsNullOrEmpty(Context.ReturnVarName)) if (string.IsNullOrEmpty(Context.ReturnVarName))
{
goto case ArrayType.ArraySize.Incomplete; goto case ArrayType.ArraySize.Incomplete;
var supportBefore = Context.Before;
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 != {array.Size})");
ThrowArgumentOutOfRangeException();
}
supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)");
if (@class != null && @class.IsRefType)
{
if (finalArrayType == arrayType)
supportBefore.WriteLineIndent(
"*({1}.{2}*) &{0}[i * sizeof({1}.{2})] = *({1}.{2}*){3}[i].{4};",
Context.ReturnVarName, arrayType, Helpers.InternalStruct,
Context.ArgName, Helpers.InstanceIdentifier);
else
supportBefore.WriteLineIndent($@"{Context.ReturnVarName}[i] = ({
(Context.Context.TargetInfo.PointerWidth == 64 ? "long" : "int")}) {
Context.ArgName}[i].{Helpers.InstanceIdentifier};");
} }
else else
{ {
var supportBefore = Context.Before; if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
supportBefore.WriteLine("if ({0} != null)", Context.ArgName); Context.Context.Options.MarshalCharAsManagedChar)
supportBefore.WriteStartBraceIndent(); supportBefore.WriteLineIndent($@"{
Class @class; Context.ReturnVarName}[i] = global::System.Convert.ToSByte({
var arrayType = array.Type.Desugar(); Context.ArgName}[i]);");
var finalArrayType = arrayType.GetPointee() ?? arrayType;
if (finalArrayType.TryGetClass(out @class) && @class.IsRefType)
{
supportBefore.WriteLine("if (value.Length != {0})", array.Size);
ThrowArgumentOutOfRangeException();
}
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
if (@class != null && @class.IsRefType)
{
if (finalArrayType == arrayType)
supportBefore.WriteLineIndent(
"*({1}.{2}*) &{0}[i * sizeof({1}.{2})] = *({1}.{2}*){3}[i].{4};",
Context.ReturnVarName, arrayType, Helpers.InternalStruct,
Context.ArgName, Helpers.InstanceIdentifier);
else
supportBefore.WriteLineIndent($@"{Context.ReturnVarName}[i] = ({
(Context.Context.TargetInfo.PointerWidth == 64 ? "long" : "int")}) {
Context.ArgName}[i].{Helpers.InstanceIdentifier};");
}
else else
{ supportBefore.WriteLineIndent($@"{Context.ReturnVarName}[i] = {
if (arrayType.IsPrimitiveType(PrimitiveType.Char) && Context.ArgName}[i]{
Context.Context.Options.MarshalCharAsManagedChar) (arrayType.IsPointerToPrimitiveType(PrimitiveType.Void) ?
{ ".ToPointer()" : string.Empty)};");
supportBefore.WriteLineIndent(
"{0}[i] = global::System.Convert.ToSByte({1}[i]);",
Context.ReturnVarName, Context.ArgName);
}
else
{
supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};",
Context.ReturnVarName,
Context.ArgName,
arrayType.IsPointerToPrimitiveType(PrimitiveType.Void)
? ".ToPointer()"
: string.Empty);
}
}
supportBefore.WriteCloseBraceIndent();
} }
supportBefore.WriteCloseBraceIndent();
break; break;
case ArrayType.ArraySize.Incomplete: case ArrayType.ArraySize.Incomplete:
MarshalArray(array); MarshalArray(array);

Loading…
Cancel
Save