Skip to content

Commit

Permalink
Added setters to non-const static fields (variables) in the C# end.
Browse files Browse the repository at this point in the history
Fixes #545.
  • Loading branch information
ddobrev committed Jan 16, 2017
1 parent f12597f commit c9103e6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -841,11 +841,6 @@ private void GeneratePropertySetter<T>(T decl,
Class @class, bool isAbstract = false, Property property = null)
where T : Declaration, ITypedDecl
{
if (!(decl is Function || decl is Field) )
{
return;
}

PushBlock(CSharpBlockKind.Method);
Write("set");

Expand Down Expand Up @@ -905,9 +900,8 @@ private void GeneratePropertySetter<T>(T decl,
GenerateInternalFunctionCall(function, new List<Parameter> { param });
}
}
WriteCloseBraceIndent();
}
else
else if (decl is Field)
{
var field = decl as Field;
if (WrapSetterArrayOfPointers(decl.Name, field.Type))
Expand Down Expand Up @@ -957,9 +951,48 @@ private void GeneratePropertySetter<T>(T decl,

if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
WriteCloseBraceIndent();
}
else if (decl is Variable)
{
NewLine();
WriteStartBraceIndent();
var var = decl as Variable;

WriteCloseBraceIndent();
TypePrinter.PushContext(CSharpTypePrinterContextKind.Native);

var location = $@"CppSharp.SymbolResolver.ResolveSymbol(""{
GetLibraryOf(decl)}"", ""{var.Mangled}"")";

string ptr = Generator.GeneratedIdentifier("ptr");
var arrayType = decl.Type as ArrayType;
var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType;
if (isRefTypeArray)
WriteLine($@"var {ptr} = {
(arrayType.Type.IsPrimitiveType(PrimitiveType.Char) &&
arrayType.QualifiedType.Qualifiers.IsConst ?
string.Empty : "(byte*)")}{location};");
else
WriteLine($"var {ptr} = ({var.Type}*){location};");

TypePrinter.PopContext();

ctx.ReturnType = new QualifiedType(var.Type);

var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
decl.CSharpMarshalToNative(marshal);

if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);

if (ctx.HasCodeBlock)
PushIndent();

WriteLine($"*{ptr} = {marshal.Context.Return};", marshal.Context.Return);

if (ctx.HasCodeBlock)
WriteCloseBraceIndent();
}
WriteCloseBraceIndent();

PopBlock(NewLineKind.BeforeNextBlock);
}
Expand Down Expand Up @@ -1371,7 +1404,8 @@ private void GenerateVariable(Class @class, Variable variable)

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

if (!variable.QualifiedType.Qualifiers.IsConst)
if (!variable.QualifiedType.Qualifiers.IsConst &&
!(variable.Type.Desugar() is ArrayType))
GeneratePropertySetter(variable, @class);

WriteCloseBraceIndent();
Expand Down
8 changes: 8 additions & 0 deletions tests/Common/Common.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ public void TestFixedCharArray()
}
}

[Test]
public void TestStaticFields()
{
Assert.That(Foo.readWrite, Is.EqualTo(15));
Foo.readWrite = 25;
Assert.That(Foo.readWrite, Is.EqualTo(25));
}

[Test, Ignore("We need symbols for std::string to invoke and auto-compilation of exported templates is not added yet.")]
public void TestStdString()
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DLL_API Foo
void* ptr;
static const int unsafe;
static const char charArray[];
static int readWrite;

const char* GetANSI();

Expand All @@ -68,6 +69,7 @@ class DLL_API Foo

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

struct DLL_API Bar
Expand Down

0 comments on commit c9103e6

Please sign in to comment.