Browse Source

Added support for function-backed properties to the C# backends.

pull/1/head
triton 12 years ago
parent
commit
1be1972907
  1. 7
      src/Bridge/Property.cs
  2. 75
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

7
src/Bridge/Property.cs

@ -6,7 +6,7 @@ namespace CppSharp @@ -6,7 +6,7 @@ namespace CppSharp
/// <summary>
/// Represents a C++ property.
/// </summary>
public class Property : Declaration
public class Property : Declaration, ITypedDecl
{
public Property()
{
@ -15,10 +15,11 @@ namespace CppSharp @@ -15,10 +15,11 @@ namespace CppSharp
public Type Type
{
get;
set;
get { return QualifiedType.Type; }
}
public QualifiedType QualifiedType { get; set; }
public Method GetMethod
{
get;

75
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -261,6 +261,7 @@ namespace CppSharp.Generators.CSharp @@ -261,6 +261,7 @@ namespace CppSharp.Generators.CSharp
GenerateClassFields(@class);
GenerateClassMethods(@class);
GenerateClassVariables(@class);
GenerateClassProperties(@class);
}
WriteCloseBraceIndent();
@ -554,7 +555,8 @@ namespace CppSharp.Generators.CSharp @@ -554,7 +555,8 @@ namespace CppSharp.Generators.CSharp
return string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")",
Path.GetFileNameWithoutExtension(library.FileName), symbol);
}
else if (decl is Field)
{
var field = decl as Field;
var location = GetFieldLocation(field, "Instance",
@ -566,6 +568,9 @@ namespace CppSharp.Generators.CSharp @@ -566,6 +568,9 @@ namespace CppSharp.Generators.CSharp
return location;
}
throw new NotSupportedException();
}
private void GeneratePropertySetter<T>(T decl, Class @class)
where T : Declaration, ITypedDecl
{
@ -584,13 +589,21 @@ namespace CppSharp.Generators.CSharp @@ -584,13 +589,21 @@ namespace CppSharp.Generators.CSharp
ArgName = param.Name,
};
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
param.Visit(marshal);
if (decl is Function)
{
var function = decl as Function;
var parameters = new List<Parameter> { param };
GenerateInternalFunctionCall(function, @class, parameters);
}
else
{
bool isRefClass;
var variable = GetPropertyLocation(decl, PropertyMethodKind.Setter,
out isRefClass);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
@ -600,6 +613,7 @@ namespace CppSharp.Generators.CSharp @@ -600,6 +613,7 @@ namespace CppSharp.Generators.CSharp
Write(".ToPointer()");
WriteLine(";");
}
WriteCloseBraceIndent();
}
@ -610,15 +624,25 @@ namespace CppSharp.Generators.CSharp @@ -610,15 +624,25 @@ namespace CppSharp.Generators.CSharp
WriteLine("get");
WriteStartBraceIndent();
var @return = string.Empty;
if (decl is Function)
{
var function = decl as Function;
GenerateInternalFunctionCall(function, @class);
@return = "ret";
}
else
{
bool isRefClass;
var variable = GetPropertyLocation(decl, PropertyMethodKind.Getter,
@return = GetPropertyLocation(decl, PropertyMethodKind.Getter,
out isRefClass);
var ctx = new CSharpMarshalContext(Driver)
{
ArgName = decl.Name,
ReturnVarName = variable,
ReturnType = decl.Type
ReturnVarName = @return,
ReturnType = decl.QualifiedType
};
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
@ -628,6 +652,7 @@ namespace CppSharp.Generators.CSharp @@ -628,6 +652,7 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.SupportBefore);
WriteLine("return {0};", marshal.Context.Return);
}
WriteCloseBraceIndent();
}
@ -679,6 +704,30 @@ namespace CppSharp.Generators.CSharp @@ -679,6 +704,30 @@ namespace CppSharp.Generators.CSharp
}
}
private void GenerateClassProperties(Class @class)
{
foreach (var prop in @class.Properties)
{
if (prop.Ignore) continue;
NewLineIfNeeded();
WriteLine("public {0} {1}", prop.Type, prop.Name);
WriteStartBraceIndent();
GeneratePropertyGetter(prop.GetMethod, @class);
NeedNewLine();
if (prop.SetMethod != null)
{
NewLineIfNeeded();
GeneratePropertySetter(prop.SetMethod, @class);
}
WriteCloseBraceIndent();
NeedNewLine();
}
}
private void GenerateVariable(Class @class, Type type, Variable variable)
{
WriteLine("public static {0} {1}", type, variable.Name);
@ -975,14 +1024,18 @@ namespace CppSharp.Generators.CSharp @@ -975,14 +1024,18 @@ namespace CppSharp.Generators.CSharp
}
}
public void GenerateInternalFunctionCall(Function function, Class @class)
public void GenerateInternalFunctionCall(Function function, Class @class,
List<Parameter> parameters = null)
{
if (parameters == null)
parameters = function.Parameters;
var functionName = string.Format("Internal.{0}",
GetFunctionNativeIdentifier(function, @class));
GenerateFunctionCall(functionName, function, @class);
GenerateFunctionCall(functionName, parameters, function, @class);
}
public void GenerateFunctionCall(string functionName,
public void GenerateFunctionCall(string functionName, List<Parameter> parameters,
Function function, Class @class = null)
{
var retType = function.ReturnType;
@ -1006,7 +1059,7 @@ namespace CppSharp.Generators.CSharp @@ -1006,7 +1059,7 @@ namespace CppSharp.Generators.CSharp
needsReturn = false;
}
var @params = GenerateFunctionParamsMarshal(function.Parameters, function);
var @params = GenerateFunctionParamsMarshal(parameters, function);
var names = (from param in @params
where !param.Param.Ignore

Loading…
Cancel
Save