From c490d25e74ad56fe888e303c863ae78dac615e73 Mon Sep 17 00:00:00 2001 From: triton Date: Thu, 15 Aug 2013 02:38:21 +0100 Subject: [PATCH] Reworked property handling to check if each property getter/setter can be generated. Fixes a bug where we tried to generate a setter for a const field. --- src/AST/Property.cs | 17 +++++++++++++++++ .../Generators/CLI/CLIHeadersTemplate.cs | 4 +--- .../Generators/CLI/CLISourcesTemplate.cs | 18 ++++++++++++++---- .../Generators/CSharp/CSharpTextTemplate.cs | 11 +++++++---- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/AST/Property.cs b/src/AST/Property.cs index 1b8b93a2..0514d111 100644 --- a/src/AST/Property.cs +++ b/src/AST/Property.cs @@ -16,6 +16,23 @@ namespace CppSharp.AST public Method SetMethod { get; set; } + public bool HasGetter + { + get + { + return (GetMethod != null) || (Field != null); + } + } + + public bool HasSetter + { + get + { + return (SetMethod != null) || + (Field != null && !Field.QualifiedType.Qualifiers.IsConst); + } + } + // The field that should be get and set by this property public Field Field { get; set; } diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index c505c982..e343e908 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -477,9 +477,7 @@ namespace CppSharp.Generators.CLI if (prop.Ignore) continue; GenerateDeclarationCommon(prop); - var isGetter = prop.GetMethod != null || prop.Field != null; - var isSetter = prop.SetMethod != null || prop.Field != null; - GenerateProperty(prop, isGetter, isSetter); + GenerateProperty(prop, prop.HasGetter, prop.HasSetter); } PopIndent(); } diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 36155b54..70e10e27 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -240,13 +240,23 @@ namespace CppSharp.Generators.CLI if (property.Field != null) { - GeneratePropertyGetter(property.Field, @class, property.Name, property.Type); - GeneratePropertySetter(property.Field, @class, property.Name, property.Type); + if (property.HasGetter) + GeneratePropertyGetter(property.Field, @class, property.Name, + property.Type); + + if (property.HasSetter) + 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); + if (property.HasGetter) + GeneratePropertyGetter(property.GetMethod, @class, property.Name, + property.Type); + + if (property.HasSetter) + GeneratePropertySetter(property.SetMethod, @class, property.Name, + property.Type); } PopBlock(); } diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 98a2a205..ceb82a2c 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -892,15 +892,18 @@ namespace CppSharp.Generators.CSharp if (prop.Field != null) { - GeneratePropertyGetter(prop.Field, @class); - GeneratePropertySetter(prop.Field, @class); + if (prop.HasGetter) + GeneratePropertyGetter(prop.Field, @class); + + if (prop.HasSetter) + GeneratePropertySetter(prop.Field, @class); } else { - if (prop.GetMethod != null) + if (prop.HasGetter) GeneratePropertyGetter(prop.GetMethod, @class); - if (prop.SetMethod != null) + if (prop.HasSetter) GeneratePropertySetter(prop.SetMethod, @class); }