Skip to content

Commit aa079c3

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

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public enum CSharpMarshalKind
1414
NativeField,
1515
GenericDelegate,
1616
DefaultExpression,
17-
VTableReturnValue
17+
VTableReturnValue,
18+
Variable
1819
}
1920

2021
public class CSharpMarshalContext : MarshalContext
@@ -591,7 +592,8 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
591592
typePrinter.PopContext();
592593
}
593594
if (marshalAsString && (Context.Kind == CSharpMarshalKind.NativeField ||
594-
Context.Kind == CSharpMarshalKind.VTableReturnValue))
595+
Context.Kind == CSharpMarshalKind.VTableReturnValue ||
596+
Context.Kind == CSharpMarshalKind.Variable))
595597
Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
596598
else
597599
Context.Return.Write(Context.Parameter.Name);

src/Generator/Generators/CSharp/CSharpSources.cs

+44-9
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,49 @@ 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.Kind = CSharpMarshalKind.Variable;
980+
ctx.ReturnType = new QualifiedType(var.Type);
981+
982+
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
983+
decl.CSharpMarshalToNative(marshal);
984+
985+
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
986+
Write(marshal.Context.SupportBefore);
987+
988+
if (ctx.HasCodeBlock)
989+
PushIndent();
990+
991+
WriteLine($"*{ptr} = {marshal.Context.Return};", marshal.Context.Return);
992+
993+
if (ctx.HasCodeBlock)
994+
WriteCloseBraceIndent();
962995
}
996+
WriteCloseBraceIndent();
963997

964998
PopBlock(NewLineKind.BeforeNextBlock);
965999
}
@@ -1371,7 +1405,8 @@ private void GenerateVariable(Class @class, Variable variable)
13711405

13721406
GeneratePropertyGetter(variable.QualifiedType, variable, @class);
13731407

1374-
if (!variable.QualifiedType.Qualifiers.IsConst)
1408+
if (!variable.QualifiedType.Qualifiers.IsConst &&
1409+
!(variable.Type.Desugar() is ArrayType))
13751410
GeneratePropertySetter(variable, @class);
13761411

13771412
WriteCloseBraceIndent();

tests/Common/Common.Tests.cs

+8
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

+2
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)