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
var internalFunction = GetFunctionNativeIdentifier(function); var internalFunction = GetFunctionNativeIdentifier(function);
var paramMarshal = GenerateFunctionParamMarshal( var paramMarshal = GenerateFunctionParamMarshal(
function.Parameters[0], 0, function); function.Parameters[0], 0, function);
string call = $@"{@internal}.{internalFunction}({
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{paramMarshal.Name})";
if (type.IsPrimitiveType()) if (type.IsPrimitiveType())
{ {
WriteLine($@"*{@internal}.{internalFunction}({ WriteLine($"*{call} = {marshal.Context.Return};");
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{
paramMarshal.Name}) = {marshal.Context.Return};");
} }
else else
{ {
var typeInternal = TypePrinter.PrintNative(type); Class @class;
WriteLine($@"*({typeInternal}*) {@internal}.{internalFunction}({ if (type.TryGetClass(out @class) && @class.HasNonTrivialCopyConstructor)
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{ {
paramMarshal.Name}) = {marshal.Context.Return};"); 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
[Test] [Test]
public void TestImplicitConversionToString() public void TestImplicitConversionToString()
{ {
using (Foo foo = new Foo()) using (Foo foo = new Foo("name"))
{ {
string name = foo; 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)
{ {
A = 10; A = 10;
P = 50; P = 50;
if (name)
{
_name = name;
}
} }
Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2) Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
@ -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() int Foo::method()
{ {
return 1; return 1;
@ -106,7 +119,7 @@ int Foo::operator --()
Foo::operator const char*() const Foo::operator const char*() const
{ {
return "test"; return _name.data();
} }
const Foo& Bar::operator[](int i) const const Foo& Bar::operator[](int i) const
@ -220,6 +233,10 @@ Bar::Bar(Items item)
{ {
} }
Bar::~Bar()
{
}
int Bar::method() int Bar::method()
{ {
return 2; return 2;
@ -268,10 +285,18 @@ ForceCreationOfInterface::ForceCreationOfInterface()
{ {
} }
ForceCreationOfInterface::~ForceCreationOfInterface()
{
}
Baz::Baz(Bar::Items item) Baz::Baz(Bar::Items item)
{ {
} }
Baz::~Baz()
{
}
int Baz::takesQux(const Qux& qux) int Baz::takesQux(const Qux& qux)
{ {
return qux.farAwayFunc(); return qux.farAwayFunc();
@ -909,6 +934,10 @@ TestOverrideFromSecondaryBase::TestOverrideFromSecondaryBase()
{ {
} }
TestOverrideFromSecondaryBase::~TestOverrideFromSecondaryBase()
{
}
void TestOverrideFromSecondaryBase::VirtualMember() void TestOverrideFromSecondaryBase::VirtualMember()
{ {
} }
@ -975,10 +1004,18 @@ InheritanceBuffer::InheritanceBuffer()
{ {
} }
InheritanceBuffer::~InheritanceBuffer()
{
}
InheritsProtectedVirtualFromSecondaryBase::InheritsProtectedVirtualFromSecondaryBase() InheritsProtectedVirtualFromSecondaryBase::InheritsProtectedVirtualFromSecondaryBase()
{ {
} }
InheritsProtectedVirtualFromSecondaryBase::~InheritsProtectedVirtualFromSecondaryBase()
{
}
void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual() void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual()
{ {
} }

9
tests/CSharp/CSharp.h

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

9
tests/Common/Common.cpp

@ -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() int TestProperties::getFieldValue()
{ {
return Field; return Field;

1
tests/Common/Common.h

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

Loading…
Cancel
Save