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. 121
      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;

121
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,16 +555,20 @@ 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",
CSharpTypePrinterContextKind.Managed, out isRefClass);
var location = GetFieldLocation(field, "Instance", if (isRefClass && kind == PropertyMethodKind.Getter)
CSharpTypePrinterContextKind.Managed, out isRefClass); location = string.Format("new System.IntPtr({0})", location);
if (isRefClass && kind == PropertyMethodKind.Getter) return location;
location = string.Format("new System.IntPtr({0})", location); }
return location; throw new NotSupportedException();
} }
private void GeneratePropertySetter<T>(T decl, Class @class) private void GeneratePropertySetter<T>(T decl, Class @class)
@ -584,22 +589,31 @@ 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;
var variable = GetPropertyLocation(decl, PropertyMethodKind.Setter,
out isRefClass);
bool isRefClass; var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
var variable = GetPropertyLocation(decl, PropertyMethodKind.Setter, param.Visit(marshal);
out isRefClass);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
Write("{0} = {1}", variable, marshal.Context.Return); Write("{0} = {1}", variable, marshal.Context.Return);
if (isRefClass) if (isRefClass)
Write(".ToPointer()"); Write(".ToPointer()");
WriteLine(";"); WriteLine(";");
}
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }
@ -610,24 +624,35 @@ namespace CppSharp.Generators.CSharp
WriteLine("get"); WriteLine("get");
WriteStartBraceIndent(); WriteStartBraceIndent();
bool isRefClass; var @return = string.Empty;
var variable = GetPropertyLocation(decl, PropertyMethodKind.Getter,
out isRefClass);
var ctx = new CSharpMarshalContext(Driver) if (decl is Function)
{ {
ArgName = decl.Name, var function = decl as Function;
ReturnVarName = variable, GenerateInternalFunctionCall(function, @class);
ReturnType = decl.Type @return = "ret";
}; }
else
{
bool isRefClass;
@return = GetPropertyLocation(decl, PropertyMethodKind.Getter,
out isRefClass);
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx); var ctx = new CSharpMarshalContext(Driver)
decl.Visit(marshal); {
ArgName = decl.Name,
ReturnVarName = @return,
ReturnType = decl.QualifiedType
};
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
Write(marshal.Context.SupportBefore); decl.Visit(marshal);
WriteLine("return {0};", marshal.Context.Return); if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
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