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

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

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

Loading…
Cancel
Save