Browse Source

Make indexers use non-trivial copy ctors if any

Fixes #1229.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
bug-pass-const-char-star-no-copy
Dimitar Dobrev 6 years ago
parent
commit
fac861ad8d
  1. 22
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 4
      tests/CSharp/CSharp.Tests.cs
  3. 39
      tests/CSharp/CSharp.cpp
  4. 9
      tests/CSharp/CSharp.h
  5. 9
      tests/Common/Common.cpp
  6. 1
      tests/Common/Common.h

22
src/Generator/Generators/CSharp/CSharpSources.cs

@ -1045,18 +1045,26 @@ namespace CppSharp.Generators.CSharp @@ -1045,18 +1045,26 @@ namespace CppSharp.Generators.CSharp
var internalFunction = GetFunctionNativeIdentifier(function);
var paramMarshal = GenerateFunctionParamMarshal(
function.Parameters[0], 0, function);
string call = $@"{@internal}.{internalFunction}({
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{paramMarshal.Name})";
if (type.IsPrimitiveType())
{
WriteLine($@"*{@internal}.{internalFunction}({
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{
paramMarshal.Name}) = {marshal.Context.Return};");
WriteLine($"*{call} = {marshal.Context.Return};");
}
else
{
var typeInternal = TypePrinter.PrintNative(type);
WriteLine($@"*({typeInternal}*) {@internal}.{internalFunction}({
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{
paramMarshal.Name}) = {marshal.Context.Return};");
Class @class;
if (type.TryGetClass(out @class) && @class.HasNonTrivialCopyConstructor)
{
Method cctor = @class.Methods.First(c => c.IsCopyConstructor);
WriteLine($@"{@class.Visit(TypePrinter)}.{Helpers.InternalStruct}.{
GetFunctionNativeIdentifier(cctor)}({call}, {
ctx.Parameter.Name}.{Helpers.InstanceIdentifier});");
}
else
{
WriteLine($"*({TypePrinter.PrintNative(type)}*) {call} = {marshal.Context.Return};");
}
}
}

4
tests/CSharp/CSharp.Tests.cs

@ -1279,10 +1279,10 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -1279,10 +1279,10 @@ public unsafe class CSharpTests : GeneratorTestFixture
[Test]
public void TestImplicitConversionToString()
{
using (Foo foo = new Foo())
using (Foo foo = new Foo("name"))
{
string name = foo;
Assert.That(name, Is.EqualTo("test"));
Assert.That(name, Is.EqualTo("name"));
}
}

39
tests/CSharp/CSharp.cpp

@ -10,6 +10,10 @@ Foo::Foo(const char* name) : publicFieldMappedToEnum(TestFlag::Flag2) @@ -10,6 +10,10 @@ Foo::Foo(const char* name) : publicFieldMappedToEnum(TestFlag::Flag2)
{
A = 10;
P = 50;
if (name)
{
_name = name;
}
}
Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
@ -26,6 +30,15 @@ Foo::Foo(wchar_t ch) @@ -26,6 +30,15 @@ Foo::Foo(wchar_t ch)
{
}
Foo::Foo(const Foo& other) : A(other.A), P(other.P),
templateInAnotherUnit(other.templateInAnotherUnit), _name(other._name)
{
}
Foo::~Foo()
{
}
int Foo::method()
{
return 1;
@ -106,7 +119,7 @@ int Foo::operator --() @@ -106,7 +119,7 @@ int Foo::operator --()
Foo::operator const char*() const
{
return "test";
return _name.data();
}
const Foo& Bar::operator[](int i) const
@ -220,6 +233,10 @@ Bar::Bar(Items item) @@ -220,6 +233,10 @@ Bar::Bar(Items item)
{
}
Bar::~Bar()
{
}
int Bar::method()
{
return 2;
@ -268,10 +285,18 @@ ForceCreationOfInterface::ForceCreationOfInterface() @@ -268,10 +285,18 @@ ForceCreationOfInterface::ForceCreationOfInterface()
{
}
ForceCreationOfInterface::~ForceCreationOfInterface()
{
}
Baz::Baz(Bar::Items item)
{
}
Baz::~Baz()
{
}
int Baz::takesQux(const Qux& qux)
{
return qux.farAwayFunc();
@ -909,6 +934,10 @@ TestOverrideFromSecondaryBase::TestOverrideFromSecondaryBase() @@ -909,6 +934,10 @@ TestOverrideFromSecondaryBase::TestOverrideFromSecondaryBase()
{
}
TestOverrideFromSecondaryBase::~TestOverrideFromSecondaryBase()
{
}
void TestOverrideFromSecondaryBase::VirtualMember()
{
}
@ -975,10 +1004,18 @@ InheritanceBuffer::InheritanceBuffer() @@ -975,10 +1004,18 @@ InheritanceBuffer::InheritanceBuffer()
{
}
InheritanceBuffer::~InheritanceBuffer()
{
}
InheritsProtectedVirtualFromSecondaryBase::InheritsProtectedVirtualFromSecondaryBase()
{
}
InheritsProtectedVirtualFromSecondaryBase::~InheritsProtectedVirtualFromSecondaryBase()
{
}
void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual()
{
}

9
tests/CSharp/CSharp.h

@ -16,6 +16,8 @@ public: @@ -16,6 +16,8 @@ public:
Foo(int a, int p = 0);
Foo(char16_t ch);
Foo(wchar_t ch);
Foo(const Foo& other);
~Foo();
int method();
int operator[](int i) const;
int operator[](unsigned int i);
@ -46,6 +48,7 @@ public: @@ -46,6 +48,7 @@ public:
protected:
int P;
TemplateInAnotherUnit<int> templateInAnotherUnit;
std::string _name;
};
class DLL_API Quux
@ -94,6 +97,7 @@ public: @@ -94,6 +97,7 @@ public:
Bar();
Bar(Qux qux);
Bar(Items item);
~Bar();
int method();
const Foo& operator[](int i) const;
Foo& operator[](int i);
@ -122,6 +126,7 @@ class DLL_API ForceCreationOfInterface : public Foo, public Bar @@ -122,6 +126,7 @@ class DLL_API ForceCreationOfInterface : public Foo, public Bar
{
public:
ForceCreationOfInterface();
~ForceCreationOfInterface();
};
class DLL_API Baz : public Foo, public Bar
@ -137,6 +142,7 @@ public: @@ -137,6 +142,7 @@ public:
Baz();
Baz(Bar::Items item);
~Baz();
int P;
@ -689,6 +695,7 @@ class DLL_API TestOverrideFromSecondaryBase : public Foo, public SecondaryBase @@ -689,6 +695,7 @@ class DLL_API TestOverrideFromSecondaryBase : public Foo, public SecondaryBase
{
public:
TestOverrideFromSecondaryBase();
~TestOverrideFromSecondaryBase();
void VirtualMember();
void setProperty(int value);
};
@ -729,12 +736,14 @@ class DLL_API InheritanceBuffer : public Foo, public HasProtectedVirtual @@ -729,12 +736,14 @@ class DLL_API InheritanceBuffer : public Foo, public HasProtectedVirtual
{
public:
InheritanceBuffer();
~InheritanceBuffer();
};
class DLL_API InheritsProtectedVirtualFromSecondaryBase : public InheritanceBuffer
{
public:
InheritsProtectedVirtualFromSecondaryBase();
~InheritsProtectedVirtualFromSecondaryBase();
protected:
void protectedVirtual();
};

9
tests/Common/Common.cpp

@ -527,6 +527,15 @@ TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0), @@ -527,6 +527,15 @@ TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0),
{
}
TestProperties::TestProperties(const TestProperties& other) : Field(other.Field),
FieldValue(other.FieldValue),
_refToPrimitiveInSetter(other._refToPrimitiveInSetter),
_getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName),
_setterReturnsBoolean(other._setterReturnsBoolean),
_virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean)
{
}
int TestProperties::getFieldValue()
{
return Field;

1
tests/Common/Common.h

@ -585,6 +585,7 @@ public: @@ -585,6 +585,7 @@ public:
};
TestProperties();
TestProperties(const TestProperties& other);
int Field;
int getFieldValue();

Loading…
Cancel
Save