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 @@ -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;

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