Browse Source

Fixed the generated C# for template specializations of pointers.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1166/head
Dimitar Dobrev 8 years ago
parent
commit
cd32a449c8
  1. 5
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 10
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  3. 2
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 15
      tests/CSharp/CSharp.Tests.cs
  5. 5
      tests/CSharp/CSharpTemplates.h

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

@ -512,10 +512,7 @@ namespace CppSharp.Generators.CSharp @@ -512,10 +512,7 @@ namespace CppSharp.Generators.CSharp
if (realPointer != pointer)
Context.Before.Write($"({CSharpTypePrinter.IntPtrType}) ");
Context.Before.WriteLine($"(object) {Context.Parameter.Name};");
Context.Before.Write($"var {refParamPtr} = ");
if (realPointer == pointer)
Context.Before.Write("&");
Context.Before.WriteLine($"{castParam};");
Context.Before.WriteLine($"var {refParamPtr} = &{castParam};");
Context.Return.Write(refParamPtr);
return true;
}

10
src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

@ -109,9 +109,13 @@ namespace CppSharp.Generators.CSharp @@ -109,9 +109,13 @@ namespace CppSharp.Generators.CSharp
{
gen.WriteLine("if ({0})", string.Join(" && ",
Enumerable.Range(0, @class.TemplateParameters.Count).Select(
i => string.Format("__{0}.IsAssignableFrom(typeof({1}))",
@class.TemplateParameters[i].Name,
specialization.Arguments[i].Type.Type.Desugar()))));
i =>
{
CppSharp.AST.Type type = specialization.Arguments[i].Type.Type.Desugar();
return type.IsPointerToPrimitiveType() ?
$"__{@class.TemplateParameters[i].Name}.FullName == \"System.IntPtr\"" :
$"__{@class.TemplateParameters[i].Name}.IsAssignableFrom(typeof({type}))";
})));
}
private static void ThrowException(CSharpSources gen, Class @class)

2
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -226,7 +226,7 @@ namespace CppSharp.Generators.CSharp @@ -226,7 +226,7 @@ namespace CppSharp.Generators.CSharp
// * Any enum type.
// * Any pointer type.
// * Any user-defined struct type that contains fields of unmanaged types only.
var finalPointee = pointer.GetFinalPointee();
var finalPointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
if (finalPointee.IsPrimitiveType())
{
// Skip one indirection if passed by reference

15
tests/CSharp/CSharp.Tests.cs

@ -859,11 +859,24 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -859,11 +859,24 @@ public unsafe class CSharpTests : GeneratorTestFixture
{
dependentValueFields.DependentValue = 10;
other.DependentValue = 15;
Assert.That((dependentValueFields + other).DependentValue, Is.EqualTo(25));
Assert.That(dependentValueFields.DependentValue, Is.EqualTo(10));
Assert.That((other).DependentValue, Is.EqualTo(15));
Assert.That((dependentValueFields + other).DependentValue, Is.EqualTo(0));
}
}
}
[Test]
public void TestTemplateSpecializationWithPointer()
{
using (var dependentValueFields = new DependentValueFields<IntPtr>())
{
int i = 10;
dependentValueFields.DependentValue = (IntPtr) (&i);
Assert.That(*(int*) dependentValueFields.DependentValue, Is.EqualTo(10));
}
}
[Test]
public void TestReturnTemplateWithRenamedTypeArg()
{

5
tests/CSharp/CSharpTemplates.h

@ -216,9 +216,7 @@ DependentValueFields<T> DependentValueFields<T>::returnValue() @@ -216,9 +216,7 @@ DependentValueFields<T> DependentValueFields<T>::returnValue()
template <typename T>
DependentValueFields<T> DependentValueFields<T>::operator+(const DependentValueFields& other)
{
DependentValueFields<T> sum;
sum.field = field + other.field;
return sum;
return DependentValueFields<T>();
}
class DLL_API DerivedFromSpecializationOfUnsupportedTemplate : public DependentValueFields<int>
@ -720,6 +718,7 @@ template class DLL_API IndependentFields<const T1>; @@ -720,6 +718,7 @@ template class DLL_API IndependentFields<const T1>;
template class DLL_API IndependentFields<std::string>;
template class DLL_API Base<int>;
template class DLL_API DependentValueFields<int>;
template class DLL_API DependentValueFields<int*>;
template class DLL_API DependentValueFields<float>;
template class DLL_API DependentPointerFields<float>;
template class DLL_API VirtualTemplate<int>;

Loading…
Cancel
Save