Skip to content

Commit c9103e6

Browse files
committed
Added setters to non-const static fields (variables) in the C# end.
Fixes #545.
1 parent f12597f commit c9103e6

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,6 @@ private void GeneratePropertySetter<T>(T decl,
841841
Class @class, bool isAbstract = false, Property property = null)
842842
where T : Declaration, ITypedDecl
843843
{
844-
if (!(decl is Function || decl is Field) )
845-
{
846-
return;
847-
}
848-
849844
PushBlock(CSharpBlockKind.Method);
850845
Write("set");
851846

@@ -905,9 +900,8 @@ private void GeneratePropertySetter<T>(T decl,
905900
GenerateInternalFunctionCall(function, new List<Parameter> { param });
906901
}
907902
}
908-
WriteCloseBraceIndent();
909903
}
910-
else
904+
else if (decl is Field)
911905
{
912906
var field = decl as Field;
913907
if (WrapSetterArrayOfPointers(decl.Name, field.Type))
@@ -957,9 +951,48 @@ private void GeneratePropertySetter<T>(T decl,
957951

958952
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
959953
WriteCloseBraceIndent();
954+
}
955+
else if (decl is Variable)
956+
{
957+
NewLine();
958+
WriteStartBraceIndent();
959+
var var = decl as Variable;
960960

961-
WriteCloseBraceIndent();
961+
TypePrinter.PushContext(CSharpTypePrinterContextKind.Native);
962+
963+
var location = $@"CppSharp.SymbolResolver.ResolveSymbol(""{
964+
GetLibraryOf(decl)}"", ""{var.Mangled}"")";
965+
966+
string ptr = Generator.GeneratedIdentifier("ptr");
967+
var arrayType = decl.Type as ArrayType;
968+
var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType;
969+
if (isRefTypeArray)
970+
WriteLine($@"var {ptr} = {
971+
(arrayType.Type.IsPrimitiveType(PrimitiveType.Char) &&
972+
arrayType.QualifiedType.Qualifiers.IsConst ?
973+
string.Empty : "(byte*)")}{location};");
974+
else
975+
WriteLine($"var {ptr} = ({var.Type}*){location};");
976+
977+
TypePrinter.PopContext();
978+
979+
ctx.ReturnType = new QualifiedType(var.Type);
980+
981+
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
982+
decl.CSharpMarshalToNative(marshal);
983+
984+
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
985+
Write(marshal.Context.SupportBefore);
986+
987+
if (ctx.HasCodeBlock)
988+
PushIndent();
989+
990+
WriteLine($"*{ptr} = {marshal.Context.Return};", marshal.Context.Return);
991+
992+
if (ctx.HasCodeBlock)
993+
WriteCloseBraceIndent();
962994
}
995+
WriteCloseBraceIndent();
963996

964997
PopBlock(NewLineKind.BeforeNextBlock);
965998
}
@@ -1371,7 +1404,8 @@ private void GenerateVariable(Class @class, Variable variable)
13711404

13721405
GeneratePropertyGetter(variable.QualifiedType, variable, @class);
13731406

1374-
if (!variable.QualifiedType.Qualifiers.IsConst)
1407+
if (!variable.QualifiedType.Qualifiers.IsConst &&
1408+
!(variable.Type.Desugar() is ArrayType))
13751409
GeneratePropertySetter(variable, @class);
13761410

13771411
WriteCloseBraceIndent();

tests/Common/Common.Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,14 @@ public void TestFixedCharArray()
648648
}
649649
}
650650

651+
[Test]
652+
public void TestStaticFields()
653+
{
654+
Assert.That(Foo.readWrite, Is.EqualTo(15));
655+
Foo.readWrite = 25;
656+
Assert.That(Foo.readWrite, Is.EqualTo(25));
657+
}
658+
651659
[Test, Ignore("We need symbols for std::string to invoke and auto-compilation of exported templates is not added yet.")]
652660
public void TestStdString()
653661
{

tests/Common/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class DLL_API Foo
4949
void* ptr;
5050
static const int unsafe;
5151
static const char charArray[];
52+
static int readWrite;
5253

5354
const char* GetANSI();
5455

@@ -68,6 +69,7 @@ class DLL_API Foo
6869

6970
// HACK: do not move these to the cpp - C++/CLI is buggy and cannot link static fields initialised in the cpp
7071
const int Foo::unsafe = 10;
72+
int Foo::readWrite = 15;
7173
const char Foo::charArray[] = "abc";
7274

7375
struct DLL_API Bar

0 commit comments

Comments
 (0)