diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 1c0c8fbf..0d35e9b6 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -475,39 +475,33 @@ namespace CppSharp.Generators.CLI public void GenerateClassProperties(Class @class) { - PushIndent(); - foreach (var field in @class.Fields) - { - if (ASTUtils.CheckIgnoreField(@class, field)) - continue; - - GenerateDeclarationCommon(field); - GenerateProperty(field); - } - PopIndent(); - PushIndent(); foreach (var prop in @class.Properties) { if (prop.Ignore) continue; 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(); } - public void GenerateProperty(T decl) + public void GenerateProperty(T decl, bool isGetter = true, bool isSetter = true) where T : Declaration, ITypedDecl { + if (!(isGetter || isSetter)) + return; + PushBlock(CLIBlockKind.Property, decl); var type = decl.Type.Visit(TypePrinter, decl.QualifiedType.Qualifiers); WriteLine("property {0} {1}", type, decl.Name); WriteStartBraceIndent(); - WriteLine("{0} get();", type); - WriteLine("void set({0});", type); + if(isGetter) WriteLine("{0} get();", type); + if(isSetter) WriteLine("void set({0});", type); WriteCloseBraceIndent(); PopBlock(); @@ -518,8 +512,6 @@ namespace CppSharp.Generators.CLI if (method.Ignore) return; if (method.Access != AccessSpecifier.Public) - return; - PushBlock(CLIBlockKind.Method, method); GenerateDeclarationCommon(method); diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 944c03a6..2a5bf296 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -154,16 +154,11 @@ namespace CppSharp.Generators.CLI WriteCloseBraceIndent(); 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) { if (@event.Ignore) @@ -239,82 +234,119 @@ namespace CppSharp.Generators.CLI 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); - GeneratePropertySetter(field, @class); + if (property.Field != null) + { + 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 decl, Class @class) + private void GeneratePropertySetter(T decl, Class @class, string name, Type type) where T : Declaration, ITypedDecl { + if (decl == null) + return; + WriteLine("void {0}::{1}::set({2} value)", QualifiedIdentifier(@class), - decl.Name, decl.Type); + name, type); WriteStartBraceIndent(); - var param = new Parameter - { - Name = "value", - QualifiedType = decl.QualifiedType - }; + if (decl is Function) + { + var func = decl as Function; + 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) - { - Parameter = param, - ArgName = param.Name, - }; + var ctx = new MarshalContext(Driver) + { + Parameter = param, + ArgName = param.Name, + }; - var marshal = new CLIMarshalManagedToNativePrinter(ctx); - param.Visit(marshal); + var marshal = new CLIMarshalManagedToNativePrinter(ctx); + param.Visit(marshal); - string variable; - if (decl is Variable) - variable = string.Format("::{0}::{1}", - @class.QualifiedOriginalName, decl.OriginalName); - else - variable = string.Format("((::{0}*)NativePtr)->{1}", - @class.QualifiedOriginalName, decl.OriginalName); + string variable; + if (decl is Variable) + variable = string.Format("::{0}::{1}", + @class.QualifiedOriginalName, decl.OriginalName); + else + variable = string.Format("((::{0}*)NativePtr)->{1}", + @class.QualifiedOriginalName, decl.OriginalName); - if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) - Write(marshal.Context.SupportBefore); + if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) + Write(marshal.Context.SupportBefore); - WriteLine("{0} = {1};", variable, marshal.Context.Return); + WriteLine("{0} = {1};", variable, marshal.Context.Return); + } WriteCloseBraceIndent(); NewLine(); } - private void GeneratePropertyGetter(T decl, Class @class) + private void GeneratePropertyGetter(T decl, Class @class, string name, Type type) where T : Declaration, ITypedDecl { - WriteLine("{0} {1}::{2}::get()", decl.Type, QualifiedIdentifier(@class), - decl.Name); + if (decl == null) + return; + + WriteLine("{0} {1}::{2}::get()", type, QualifiedIdentifier(@class), + name); WriteStartBraceIndent(); - string variable; - if (decl is Variable) - variable = string.Format("::{0}::{1}", - @class.QualifiedOriginalName, decl.OriginalName); + if (decl is Function) + { + var func = decl as Function; + GenerateFunctionCall(func, @class); + } else - variable = string.Format("((::{0}*)NativePtr)->{1}", + { + string variable; + if (decl is Variable) + variable = string.Format("::{0}::{1}", @class.QualifiedOriginalName, decl.OriginalName); + else + variable = string.Format("((::{0}*)NativePtr)->{1}", + @class.QualifiedOriginalName, decl.OriginalName); - var ctx = new MarshalContext(Driver) - { - ArgName = decl.Name, - ReturnVarName = variable, - ReturnType = decl.QualifiedType - }; + var ctx = new MarshalContext(Driver) + { + ArgName = decl.Name, + ReturnVarName = variable, + ReturnType = decl.QualifiedType + }; - var marshal = new CLIMarshalNativeToManagedPrinter(ctx); - decl.Visit(marshal); + var marshal = new CLIMarshalNativeToManagedPrinter(ctx); + decl.Visit(marshal); - if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) - Write(marshal.Context.SupportBefore); + if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) + Write(marshal.Context.SupportBefore); - WriteLine("return {0};", marshal.Context.Return); + WriteLine("return {0};", marshal.Context.Return); + } + WriteCloseBraceIndent(); NewLine(); @@ -428,10 +460,10 @@ namespace CppSharp.Generators.CLI private void GenerateVariable(Variable variable, Class @class) { - GeneratePropertyGetter(variable, @class); + GeneratePropertyGetter(variable, @class, variable.Name, variable.Type); if (!variable.QualifiedType.Qualifiers.IsConst) - GeneratePropertySetter(variable, @class); + GeneratePropertySetter(variable, @class, variable.Name, variable.Type); } private void GenerateClassConstructor(Class @class, bool isIntPtr)