Browse Source

CLIHeaderTemplate an CLISourcesTemplate now take care of properties with only a getter or s setter, and properties using fields that for now always have a getter and a setter.

pull/16/head
marcos henrich 12 years ago
parent
commit
61986172e7
  1. 26
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 146
      src/Generator/Generators/CLI/CLISourcesTemplate.cs

26
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -475,39 +475,33 @@ namespace CppSharp.Generators.CLI
public void GenerateClassProperties(Class @class) public void GenerateClassProperties(Class @class)
{ {
PushIndent();
foreach (var field in @class.Fields)
{
if (ASTUtils.CheckIgnoreField(@class, field))
continue;
GenerateDeclarationCommon(field);
GenerateProperty(field);
}
PopIndent();
PushIndent(); PushIndent();
foreach (var prop in @class.Properties) foreach (var prop in @class.Properties)
{ {
if (prop.Ignore) continue; if (prop.Ignore) continue;
GenerateDeclarationCommon(prop); GenerateDeclarationCommon(prop);
GenerateProperty(prop); var isGetter = prop.GetMethod != null || prop.Field != null;
var isSetter = prop.SetMethod != null || prop.Field != null;
GenerateProperty(prop, isGetter, isSetter);
} }
PopIndent(); PopIndent();
} }
public void GenerateProperty<T>(T decl) public void GenerateProperty<T>(T decl, bool isGetter = true, bool isSetter = true)
where T : Declaration, ITypedDecl where T : Declaration, ITypedDecl
{ {
if (!(isGetter || isSetter))
return;
PushBlock(CLIBlockKind.Property, decl); PushBlock(CLIBlockKind.Property, decl);
var type = decl.Type.Visit(TypePrinter, decl.QualifiedType.Qualifiers); var type = decl.Type.Visit(TypePrinter, decl.QualifiedType.Qualifiers);
WriteLine("property {0} {1}", type, decl.Name); WriteLine("property {0} {1}", type, decl.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
WriteLine("{0} get();", type); if(isGetter) WriteLine("{0} get();", type);
WriteLine("void set({0});", type); if(isSetter) WriteLine("void set({0});", type);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(); PopBlock();
@ -518,8 +512,6 @@ namespace CppSharp.Generators.CLI
if (method.Ignore) return; if (method.Ignore) return;
if (method.Access != AccessSpecifier.Public) if (method.Access != AccessSpecifier.Public)
return;
PushBlock(CLIBlockKind.Method, method); PushBlock(CLIBlockKind.Method, method);
GenerateDeclarationCommon(method); GenerateDeclarationCommon(method);

146
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -154,16 +154,11 @@ namespace CppSharp.Generators.CLI
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
foreach (var field in @class.Fields)
{
if (ASTUtils.CheckIgnoreField(@class, field))
continue;
GenerateFieldProperty(field);
}
} }
foreach (var property in @class.Properties)
GenerateProperty(property);
foreach (var @event in @class.Events) foreach (var @event in @class.Events)
{ {
if (@event.Ignore) if (@event.Ignore)
@ -239,82 +234,119 @@ namespace CppSharp.Generators.CLI
printer.Context = oldCtx; printer.Context = oldCtx;
} }
private void GenerateFieldProperty(Field field) private void GenerateProperty(Property property)
{ {
var @class = field.Class; if (property.Ignore) return;
PushBlock(CLIBlockKind.Property);
var @class = property.Namespace as Class;
GeneratePropertyGetter(field, @class); if (property.Field != null)
GeneratePropertySetter(field, @class); {
GeneratePropertyGetter(property.Field, @class, property.Name, property.Type);
GeneratePropertySetter(property.Field, @class, property.Name, property.Type);
}
else
{
GeneratePropertyGetter(property.GetMethod, @class, property.Name, property.Type);
GeneratePropertySetter(property.SetMethod, @class, property.Name, property.Type);
}
PopBlock();
} }
private void GeneratePropertySetter<T>(T decl, Class @class) private void GeneratePropertySetter<T>(T decl, Class @class, string name, Type type)
where T : Declaration, ITypedDecl where T : Declaration, ITypedDecl
{ {
if (decl == null)
return;
WriteLine("void {0}::{1}::set({2} value)", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::set({2} value)", QualifiedIdentifier(@class),
decl.Name, decl.Type); name, type);
WriteStartBraceIndent(); WriteStartBraceIndent();
var param = new Parameter if (decl is Function)
{ {
Name = "value", var func = decl as Function;
QualifiedType = decl.QualifiedType if(func.Parameters[0].Name != "value")
}; WriteLine("auto {0} = value;", func.Parameters[0].Name);
GenerateFunctionCall(func, @class);
}
else
{
var param = new Parameter
{
Name = "value",
QualifiedType = decl.QualifiedType
};
var ctx = new MarshalContext(Driver) var ctx = new MarshalContext(Driver)
{ {
Parameter = param, Parameter = param,
ArgName = param.Name, ArgName = param.Name,
}; };
var marshal = new CLIMarshalManagedToNativePrinter(ctx); var marshal = new CLIMarshalManagedToNativePrinter(ctx);
param.Visit(marshal); param.Visit(marshal);
string variable; string variable;
if (decl is Variable) if (decl is Variable)
variable = string.Format("::{0}::{1}", variable = string.Format("::{0}::{1}",
@class.QualifiedOriginalName, decl.OriginalName); @class.QualifiedOriginalName, decl.OriginalName);
else else
variable = string.Format("((::{0}*)NativePtr)->{1}", variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, decl.OriginalName); @class.QualifiedOriginalName, decl.OriginalName);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
WriteLine("{0} = {1};", variable, marshal.Context.Return); WriteLine("{0} = {1};", variable, marshal.Context.Return);
}
WriteCloseBraceIndent(); WriteCloseBraceIndent();
NewLine(); NewLine();
} }
private void GeneratePropertyGetter<T>(T decl, Class @class) private void GeneratePropertyGetter<T>(T decl, Class @class, string name, Type type)
where T : Declaration, ITypedDecl where T : Declaration, ITypedDecl
{ {
WriteLine("{0} {1}::{2}::get()", decl.Type, QualifiedIdentifier(@class), if (decl == null)
decl.Name); return;
WriteLine("{0} {1}::{2}::get()", type, QualifiedIdentifier(@class),
name);
WriteStartBraceIndent(); WriteStartBraceIndent();
string variable; if (decl is Function)
if (decl is Variable) {
variable = string.Format("::{0}::{1}", var func = decl as Function;
@class.QualifiedOriginalName, decl.OriginalName); GenerateFunctionCall(func, @class);
}
else else
variable = string.Format("((::{0}*)NativePtr)->{1}", {
string variable;
if (decl is Variable)
variable = string.Format("::{0}::{1}",
@class.QualifiedOriginalName, decl.OriginalName); @class.QualifiedOriginalName, decl.OriginalName);
else
variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, decl.OriginalName);
var ctx = new MarshalContext(Driver) var ctx = new MarshalContext(Driver)
{ {
ArgName = decl.Name, ArgName = decl.Name,
ReturnVarName = variable, ReturnVarName = variable,
ReturnType = decl.QualifiedType ReturnType = decl.QualifiedType
}; };
var marshal = new CLIMarshalNativeToManagedPrinter(ctx); var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
decl.Visit(marshal); decl.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
WriteLine("return {0};", marshal.Context.Return); WriteLine("return {0};", marshal.Context.Return);
}
WriteCloseBraceIndent(); WriteCloseBraceIndent();
NewLine(); NewLine();
@ -428,10 +460,10 @@ namespace CppSharp.Generators.CLI
private void GenerateVariable(Variable variable, Class @class) private void GenerateVariable(Variable variable, Class @class)
{ {
GeneratePropertyGetter(variable, @class); GeneratePropertyGetter(variable, @class, variable.Name, variable.Type);
if (!variable.QualifiedType.Qualifiers.IsConst) if (!variable.QualifiedType.Qualifiers.IsConst)
GeneratePropertySetter(variable, @class); GeneratePropertySetter(variable, @class, variable.Name, variable.Type);
} }
private void GenerateClassConstructor(Class @class, bool isIntPtr) private void GenerateClassConstructor(Class @class, bool isIntPtr)

Loading…
Cancel
Save