Browse Source

Marshal non primitive fixed arrays. (#1311)

Marshal non primitive fixed arrays.
pull/1316/head
Ali Alamiri 5 years ago committed by GitHub
parent
commit
3547203538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 20
      tests/Common/Common.Tests.cs
  3. 9
      tests/Common/Common.cpp
  4. 16
      tests/Common/Common.h

23
src/Generator/Generators/CLI/CLIMarshal.cs

@ -52,6 +52,8 @@ namespace CppSharp.Generators.CLI @@ -52,6 +52,8 @@ namespace CppSharp.Generators.CLI
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void))
Context.Before.WriteLineIndent("{0}[i] = new ::System::IntPtr({1}[i]);",
value, Context.ReturnVarName);
else if (!array.Type.IsPrimitiveType())
Context.Before.WriteLineIndent($"{value}[i] = gcnew {array.Type.ToString().TrimEnd('^')}(&{Context.ReturnVarName}[i]);");
else
Context.Before.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName);
Context.Before.UnindentAndWriteCloseBrace();
@ -436,13 +438,28 @@ namespace CppSharp.Generators.CLI @@ -436,13 +438,28 @@ namespace CppSharp.Generators.CLI
}
else
{
bool isPointerToPrimitive = array.Type.IsPointerToPrimitiveType(PrimitiveType.Void);
bool isPrimitive = array.Type.IsPrimitiveType();
var supportBefore = Context.Before;
supportBefore.WriteLine("if ({0} != nullptr)", Context.ArgName);
supportBefore.WriteOpenBraceAndIndent();
string nativeVal = string.Empty;
if (isPointerToPrimitive)
{
nativeVal = ".ToPointer()";
}
else if (!isPrimitive)
{
nativeVal = "->NativePtr";
}
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};",
Context.ReturnVarName, Context.ArgName,
array.Type.IsPointerToPrimitiveType(PrimitiveType.Void) ? ".ToPointer()" : string.Empty);
supportBefore.WriteLineIndent("{0}[i] = {1}{2}[i]{3};",
Context.ReturnVarName,
isPointerToPrimitive || isPrimitive ? string.Empty : "*",
Context.ArgName,
nativeVal);
supportBefore.UnindentAndWriteCloseBrace();
}
break;

20
tests/Common/Common.Tests.cs

@ -981,4 +981,24 @@ This is a very long string. This is a very long string. This is a very long stri @@ -981,4 +981,24 @@ This is a very long string. This is a very long string. This is a very long stri
Assert.That(ret, Is.EqualTo(10));
}
}
[Test]
public void TestNonPrimitiveFixedArrays()
{
using (var nonPrimitiveFixedArray = new TestFixedNonPrimitiveArrays())
{
nonPrimitiveFixedArray.NonPrimitiveTypeArray = new NonPrimitiveType[]
{
new NonPrimitiveType{ foo = 1 },
new NonPrimitiveType{ foo = 2 },
new NonPrimitiveType{ foo = 3 }
};
Assert.AreEqual(3, nonPrimitiveFixedArray.NonPrimitiveTypeArray.Length);
Assert.AreEqual(1, nonPrimitiveFixedArray.NonPrimitiveTypeArray[0].Foo);
Assert.AreEqual(2, nonPrimitiveFixedArray.NonPrimitiveTypeArray[1].Foo);
Assert.AreEqual(3, nonPrimitiveFixedArray.NonPrimitiveTypeArray[2].Foo);
}
}
}

9
tests/Common/Common.cpp

@ -1181,3 +1181,12 @@ BaseCovariant::~BaseCovariant() @@ -1181,3 +1181,12 @@ BaseCovariant::~BaseCovariant()
DerivedCovariant::~DerivedCovariant()
{
}
int NonPrimitiveType::GetFoo()
{
return foo;
}
TestFixedNonPrimitiveArrays::TestFixedNonPrimitiveArrays()
{
}

16
tests/Common/Common.h

@ -773,6 +773,22 @@ TestArraysPointers::TestArraysPointers(MyEnum *values, int count) @@ -773,6 +773,22 @@ TestArraysPointers::TestArraysPointers(MyEnum *values, int count)
if (values && count) Value = values[0];
}
class DLL_API NonPrimitiveType
{
public:
int GetFoo();
int foo;
};
class DLL_API TestFixedNonPrimitiveArrays
{
public:
TestFixedNonPrimitiveArrays();
NonPrimitiveType NonPrimitiveTypeArray[3];
};
struct DLL_API TestGetterSetterToProperties
{
int getWidth();

Loading…
Cancel
Save