Skip to content

Commit fac861a

Browse files
committed
Make indexers use non-trivial copy ctors if any
Fixes #1229. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 49ab022 commit fac861a

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,18 +1045,26 @@ private void GenerateIndexerSetter(Function function)
10451045
var internalFunction = GetFunctionNativeIdentifier(function);
10461046
var paramMarshal = GenerateFunctionParamMarshal(
10471047
function.Parameters[0], 0, function);
1048+
string call = $@"{@internal}.{internalFunction}({
1049+
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{paramMarshal.Name})";
10481050
if (type.IsPrimitiveType())
10491051
{
1050-
WriteLine($@"*{@internal}.{internalFunction}({
1051-
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{
1052-
paramMarshal.Name}) = {marshal.Context.Return};");
1052+
WriteLine($"*{call} = {marshal.Context.Return};");
10531053
}
10541054
else
10551055
{
1056-
var typeInternal = TypePrinter.PrintNative(type);
1057-
WriteLine($@"*({typeInternal}*) {@internal}.{internalFunction}({
1058-
GetInstanceParam(function)}, {paramMarshal.Context.ArgumentPrefix}{
1059-
paramMarshal.Name}) = {marshal.Context.Return};");
1056+
Class @class;
1057+
if (type.TryGetClass(out @class) && @class.HasNonTrivialCopyConstructor)
1058+
{
1059+
Method cctor = @class.Methods.First(c => c.IsCopyConstructor);
1060+
WriteLine($@"{@class.Visit(TypePrinter)}.{Helpers.InternalStruct}.{
1061+
GetFunctionNativeIdentifier(cctor)}({call}, {
1062+
ctx.Parameter.Name}.{Helpers.InstanceIdentifier});");
1063+
}
1064+
else
1065+
{
1066+
WriteLine($"*({TypePrinter.PrintNative(type)}*) {call} = {marshal.Context.Return};");
1067+
}
10601068
}
10611069
}
10621070

tests/CSharp/CSharp.Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,10 +1279,10 @@ public void TestConstCharStarRef()
12791279
[Test]
12801280
public void TestImplicitConversionToString()
12811281
{
1282-
using (Foo foo = new Foo())
1282+
using (Foo foo = new Foo("name"))
12831283
{
12841284
string name = foo;
1285-
Assert.That(name, Is.EqualTo("test"));
1285+
Assert.That(name, Is.EqualTo("name"));
12861286
}
12871287
}
12881288

tests/CSharp/CSharp.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Foo::Foo(const char* name) : publicFieldMappedToEnum(TestFlag::Flag2)
1010
{
1111
A = 10;
1212
P = 50;
13+
if (name)
14+
{
15+
_name = name;
16+
}
1317
}
1418

1519
Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
@@ -26,6 +30,15 @@ Foo::Foo(wchar_t ch)
2630
{
2731
}
2832

33+
Foo::Foo(const Foo& other) : A(other.A), P(other.P),
34+
templateInAnotherUnit(other.templateInAnotherUnit), _name(other._name)
35+
{
36+
}
37+
38+
Foo::~Foo()
39+
{
40+
}
41+
2942
int Foo::method()
3043
{
3144
return 1;
@@ -106,7 +119,7 @@ int Foo::operator --()
106119

107120
Foo::operator const char*() const
108121
{
109-
return "test";
122+
return _name.data();
110123
}
111124

112125
const Foo& Bar::operator[](int i) const
@@ -220,6 +233,10 @@ Bar::Bar(Items item)
220233
{
221234
}
222235

236+
Bar::~Bar()
237+
{
238+
}
239+
223240
int Bar::method()
224241
{
225242
return 2;
@@ -268,10 +285,18 @@ ForceCreationOfInterface::ForceCreationOfInterface()
268285
{
269286
}
270287

288+
ForceCreationOfInterface::~ForceCreationOfInterface()
289+
{
290+
}
291+
271292
Baz::Baz(Bar::Items item)
272293
{
273294
}
274295

296+
Baz::~Baz()
297+
{
298+
}
299+
275300
int Baz::takesQux(const Qux& qux)
276301
{
277302
return qux.farAwayFunc();
@@ -909,6 +934,10 @@ TestOverrideFromSecondaryBase::TestOverrideFromSecondaryBase()
909934
{
910935
}
911936

937+
TestOverrideFromSecondaryBase::~TestOverrideFromSecondaryBase()
938+
{
939+
}
940+
912941
void TestOverrideFromSecondaryBase::VirtualMember()
913942
{
914943
}
@@ -975,10 +1004,18 @@ InheritanceBuffer::InheritanceBuffer()
9751004
{
9761005
}
9771006

1007+
InheritanceBuffer::~InheritanceBuffer()
1008+
{
1009+
}
1010+
9781011
InheritsProtectedVirtualFromSecondaryBase::InheritsProtectedVirtualFromSecondaryBase()
9791012
{
9801013
}
9811014

1015+
InheritsProtectedVirtualFromSecondaryBase::~InheritsProtectedVirtualFromSecondaryBase()
1016+
{
1017+
}
1018+
9821019
void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual()
9831020
{
9841021
}

tests/CSharp/CSharp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class DLL_API Foo
1616
Foo(int a, int p = 0);
1717
Foo(char16_t ch);
1818
Foo(wchar_t ch);
19+
Foo(const Foo& other);
20+
~Foo();
1921
int method();
2022
int operator[](int i) const;
2123
int operator[](unsigned int i);
@@ -46,6 +48,7 @@ class DLL_API Foo
4648
protected:
4749
int P;
4850
TemplateInAnotherUnit<int> templateInAnotherUnit;
51+
std::string _name;
4952
};
5053

5154
class DLL_API Quux
@@ -94,6 +97,7 @@ class DLL_API Bar : public Qux
9497
Bar();
9598
Bar(Qux qux);
9699
Bar(Items item);
100+
~Bar();
97101
int method();
98102
const Foo& operator[](int i) const;
99103
Foo& operator[](int i);
@@ -122,6 +126,7 @@ class DLL_API ForceCreationOfInterface : public Foo, public Bar
122126
{
123127
public:
124128
ForceCreationOfInterface();
129+
~ForceCreationOfInterface();
125130
};
126131

127132
class DLL_API Baz : public Foo, public Bar
@@ -137,6 +142,7 @@ class DLL_API Baz : public Foo, public Bar
137142

138143
Baz();
139144
Baz(Bar::Items item);
145+
~Baz();
140146

141147
int P;
142148

@@ -689,6 +695,7 @@ class DLL_API TestOverrideFromSecondaryBase : public Foo, public SecondaryBase
689695
{
690696
public:
691697
TestOverrideFromSecondaryBase();
698+
~TestOverrideFromSecondaryBase();
692699
void VirtualMember();
693700
void setProperty(int value);
694701
};
@@ -729,12 +736,14 @@ class DLL_API InheritanceBuffer : public Foo, public HasProtectedVirtual
729736
{
730737
public:
731738
InheritanceBuffer();
739+
~InheritanceBuffer();
732740
};
733741

734742
class DLL_API InheritsProtectedVirtualFromSecondaryBase : public InheritanceBuffer
735743
{
736744
public:
737745
InheritsProtectedVirtualFromSecondaryBase();
746+
~InheritsProtectedVirtualFromSecondaryBase();
738747
protected:
739748
void protectedVirtual();
740749
};

tests/Common/Common.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0),
527527
{
528528
}
529529

530+
TestProperties::TestProperties(const TestProperties& other) : Field(other.Field),
531+
FieldValue(other.FieldValue),
532+
_refToPrimitiveInSetter(other._refToPrimitiveInSetter),
533+
_getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName),
534+
_setterReturnsBoolean(other._setterReturnsBoolean),
535+
_virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean)
536+
{
537+
}
538+
530539
int TestProperties::getFieldValue()
531540
{
532541
return Field;

tests/Common/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ struct DLL_API TestProperties
585585
};
586586

587587
TestProperties();
588+
TestProperties(const TestProperties& other);
588589
int Field;
589590

590591
int getFieldValue();

0 commit comments

Comments
 (0)