From f07ddf79fc0a31cab151ff032b7af9466a803c76 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Thu, 19 Sep 2013 16:12:45 +0300 Subject: [PATCH] Replaced the vague checks for explicit interface impls with a specific property. Signed-off-by: Dimitar Dobrev --- src/AST/Method.cs | 2 ++ src/AST/Property.cs | 2 ++ .../Generators/CSharp/CSharpTextTemplate.cs | 24 +++++++++---------- .../Passes/MultipleInheritancePass.cs | 4 ++-- tests/CSharpTemp/CSharpTemp.Tests.cs | 1 + 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/AST/Method.cs b/src/AST/Method.cs index 65bbf895..c51746da 100644 --- a/src/AST/Method.cs +++ b/src/AST/Method.cs @@ -126,6 +126,8 @@ namespace CppSharp.AST public MethodConversionKind Conversion { get; set; } + public Class ExplicitInterfaceImpl { get; set; } + public override QualifiedType GetFunctionType() { var qualifiedType = base.GetFunctionType(); diff --git a/src/AST/Property.cs b/src/AST/Property.cs index cf2ebb5b..9b0bdb90 100644 --- a/src/AST/Property.cs +++ b/src/AST/Property.cs @@ -62,6 +62,8 @@ namespace CppSharp.AST // The field that should be get and set by this property public Field Field { get; set; } + public Class ExplicitInterfaceImpl { get; set; } + private readonly List parameters = new List(); /// diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 3a9c234e..b12af4bd 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -1000,13 +1000,13 @@ namespace CppSharp.Generators.CSharp var type = prop.Type; if (prop.Parameters.Count > 0 && prop.Type.IsPointerToPrimitiveType()) type = ((PointerType) prop.Type).Pointee; - // explicit impl - if (prop.Name.Contains('.')) - WriteLine("{0} {1}", type, GetPropertyName(prop)); - else + + if (prop.ExplicitInterfaceImpl == null) WriteLine("{0} {1} {2}", prop.Access == AccessSpecifier.Public ? "public" : "protected", type, GetPropertyName(prop)); + else + WriteLine("{0} {1}.{2}", type, prop.ExplicitInterfaceImpl.Name, GetPropertyName(prop)); WriteStartBraceIndent(); if (prop.Field != null) @@ -1033,12 +1033,8 @@ namespace CppSharp.Generators.CSharp private string GetPropertyName(Property prop) { - // check for explicit interface implementations - var indexOfDot = prop.Name.IndexOf('.'); - string name = prop.Name.Substring(prop.Name.IndexOf('.') + 1); - return prop.Name.Substring(0, indexOfDot + 1) + - (prop.Parameters.Count == 0 ? SafeIdentifier(name) - : string.Format("this[{0}]", FormatMethodParameters(prop.Parameters))); + return prop.Parameters.Count == 0 ? SafeIdentifier(prop.Name) + : string.Format("this[{0}]", this.FormatMethodParameters(prop.Parameters)); } private void GenerateVariable(Class @class, Type type, Variable variable) @@ -1602,8 +1598,7 @@ namespace CppSharp.Generators.CSharp PushBlock(CSharpBlockKind.Method); GenerateDeclarationCommon(method); - // check if this is an explicit interface implementation - if (!method.Name.Contains('.')) + if (method.ExplicitInterfaceImpl == null) { switch (GetValidMethodAccess(method, @class)) { @@ -1637,8 +1632,11 @@ namespace CppSharp.Generators.CSharp if (method.IsConstructor || method.IsDestructor) Write("{0}(", functionName); - else + else if (method.ExplicitInterfaceImpl == null) Write("{0} {1}(", method.OriginalReturnType, functionName); + else + Write("{0} {1}.{2}(", method.OriginalReturnType, + method.ExplicitInterfaceImpl.Name, functionName); Write(FormatMethodParameters(method.Parameters)); diff --git a/src/Generator/Passes/MultipleInheritancePass.cs b/src/Generator/Passes/MultipleInheritancePass.cs index 0ffe9570..082bf41d 100644 --- a/src/Generator/Passes/MultipleInheritancePass.cs +++ b/src/Generator/Passes/MultipleInheritancePass.cs @@ -85,7 +85,7 @@ namespace CppSharp.Passes }; var rootBaseMethod = @class.GetRootBaseMethod(method, true); if (rootBaseMethod != null && !rootBaseMethod.Ignore) - impl.Name = @interface.Name + "." + impl.Name; + impl.ExplicitInterfaceImpl = @interface; @class.Methods.Add(impl); } foreach (var @base in @interface.Bases) @@ -99,7 +99,7 @@ namespace CppSharp.Passes var impl = new Property(property) { Namespace = @class }; var rootBaseProperty = @class.GetRootBaseProperty(property, true); if (rootBaseProperty != null && !rootBaseProperty.Ignore) - impl.Name = @interface.Name + "." + impl.Name; + impl.ExplicitInterfaceImpl = @interface; @class.Properties.Add(impl); } foreach (var @base in @interface.Bases) diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index a31dbde8..b3a1c7cd 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -41,5 +41,6 @@ public class CSharpTempTests Assert.That(baz[0], Is.EqualTo(50)); bar[0] = new Foo { A = 1000 }; Assert.That(bar[0].A, Is.EqualTo(1000)); + Assert.That(baz.farAwayFunc(), Is.EqualTo(20)); } } \ No newline at end of file