Browse Source

Fix templated indexers returning objects

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1635/head
Dimitar Dobrev 4 years ago
parent
commit
4649052f2c
  1. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 17
      tests/CSharp/CSharp.Tests.cs
  3. 15
      tests/CSharp/CSharpTemplates.cpp
  4. 16
      tests/CSharp/CSharpTemplates.h

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

@ -278,7 +278,9 @@ namespace CppSharp.Generators.CSharp @@ -278,7 +278,9 @@ namespace CppSharp.Generators.CSharp
if (finalType.TryGetClass(out returnedClass) && returnedClass.IsDependent)
Context.Return.Write($"({returnType.Visit(typePrinter)}) (object) ");
if (returnType.IsAddress())
// these two aren't the same for members of templates
if (Context.Function?.OriginalReturnType.Type.Desugar().IsAddress() == true ||
returnType.IsAddress())
Context.Return.Write(HandleReturnedPointer(@class, qualifiedClass));
else
{

17
tests/CSharp/CSharp.Tests.cs

@ -1176,17 +1176,20 @@ public unsafe class CSharpTests @@ -1176,17 +1176,20 @@ public unsafe class CSharpTests
templateWithIndexer["test"] = 15;
Assert.That(templateWithIndexer["test"], Is.EqualTo(15));
}
using (var templateWithIndexer = new TemplateWithIndexer<T1>())
using (var templateWithIndexer = new TemplateWithIndexer<T2>())
{
using (var t1 = new T1(10))
using (var t2 = new T2())
{
templateWithIndexer[0] = t1;
Assert.That(templateWithIndexer[0].Field, Is.EqualTo(t1.Field));
templateWithIndexer[0] = t2;
var item = templateWithIndexer[0];
Assert.That(item.Field, Is.EqualTo(t2.Field));
item.Field = 5;
Assert.That(templateWithIndexer[0].Field, Is.EqualTo(5));
}
using (var t1 = new T1(15))
using (var t2 = new T2(15))
{
templateWithIndexer["test"] = t1;
Assert.That(templateWithIndexer["test"].Field, Is.EqualTo(t1.Field));
templateWithIndexer["test"] = t2;
Assert.That(templateWithIndexer["test"].Field, Is.EqualTo(t2.Field));
}
}
}

15
tests/CSharp/CSharpTemplates.cpp

@ -1,29 +1,26 @@ @@ -1,29 +1,26 @@
#include "CSharpTemplates.h"
T1::T1()
T2::T2() : field(0)
{
}
T1::T1(const T1& other) : field(other.field)
{
}
T1::T1(int f)
T2::T2(int f)
{
field = f;
}
T1::~T1()
T2::~T2()
{
}
int T1::getField() const
int T2::getField() const
{
return field;
}
T2::T2()
void T2::setField(int value)
{
field = value;
}
DerivedFromSpecializationOfUnsupportedTemplate::DerivedFromSpecializationOfUnsupportedTemplate()

16
tests/CSharp/CSharpTemplates.h

@ -12,20 +12,18 @@ class DLL_API QString @@ -12,20 +12,18 @@ class DLL_API QString
class DLL_API T1
{
public:
T1();
T1(const T1& other);
T1(int f);
~T1();
int getField() const;
private:
int field;
};
class DLL_API T2
{
public:
T2();
T2(int f);
virtual ~T2();
int getField() const;
void setField(int value);
private:
int field;
};
class DLL_API Ignored
@ -92,7 +90,7 @@ IndependentFields<T>::IndependentFields(float f) @@ -92,7 +90,7 @@ IndependentFields<T>::IndependentFields(float f)
}
template <typename T>
IndependentFields<T>::IndependentFields(const std::map<T, T> &v)
IndependentFields<T>::IndependentFields(const std::map<T, T> &v) : independent(1)
{
}

Loading…
Cancel
Save