diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 5287b28a..ffaee542 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -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};"); + } } } diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index a923a430..76218ad6 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -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")); } } diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 4bcf2ce9..d36dd8d5 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -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) { } +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 --() 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) { } +Bar::~Bar() +{ +} + int Bar::method() { return 2; @@ -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() { } +TestOverrideFromSecondaryBase::~TestOverrideFromSecondaryBase() +{ +} + void TestOverrideFromSecondaryBase::VirtualMember() { } @@ -975,10 +1004,18 @@ InheritanceBuffer::InheritanceBuffer() { } +InheritanceBuffer::~InheritanceBuffer() +{ +} + InheritsProtectedVirtualFromSecondaryBase::InheritsProtectedVirtualFromSecondaryBase() { } +InheritsProtectedVirtualFromSecondaryBase::~InheritsProtectedVirtualFromSecondaryBase() +{ +} + void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual() { } diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 0a049172..f12f91e3 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -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: protected: int P; TemplateInAnotherUnit templateInAnotherUnit; + std::string _name; }; class DLL_API Quux @@ -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 { public: ForceCreationOfInterface(); + ~ForceCreationOfInterface(); }; class DLL_API Baz : public Foo, public Bar @@ -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 { public: TestOverrideFromSecondaryBase(); + ~TestOverrideFromSecondaryBase(); void VirtualMember(); void setProperty(int value); }; @@ -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(); }; diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 44b4af16..72c50a4f 100644 --- a/tests/Common/Common.cpp +++ b/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() { return Field; diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 4dc42120..0f297948 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -585,6 +585,7 @@ public: }; TestProperties(); + TestProperties(const TestProperties& other); int Field; int getFieldValue();