From e6cb543c36860b717b2f32c6b059a504f94a8086 Mon Sep 17 00:00:00 2001 From: Elias Holzer Date: Tue, 15 Apr 2014 22:27:48 +0200 Subject: [PATCH] Fixed overloading of an indexed properties by removing the hard coded "int index" part. --- .../Generators/CLI/CLIHeadersTemplate.cs | 15 +++--- .../Generators/CLI/CLISourcesTemplate.cs | 46 ++++++++++--------- .../Passes/CheckOperatorsOverloads.cs | 7 +-- tests/Basic/Basic.Tests.cs | 3 +- tests/Basic/Basic.h | 1 + 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 4654ab81..ef1c6adb 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -615,16 +615,19 @@ namespace CppSharp.Generators.CLI public void GenerateIndexer(Property property) { - var type = property.QualifiedType.Visit(TypePrinter); + var type = property.QualifiedType.Visit(TypePrinter); + var getter = property.GetMethod; + var indexParameter = getter.Parameters[0]; + var indexParameterType = indexParameter.QualifiedType.Visit(TypePrinter); - WriteLine("property {0} default[int]", type); + WriteLine("property {0} default[{1}]", type, indexParameterType); WriteStartBraceIndent(); - if (property.HasGetter) - WriteLine("{0} get(int index);", type); + if (property.HasGetter) + WriteLine("{0} get({1} {2});", type, indexParameterType, indexParameter.Name); - if (property.HasSetter) - WriteLine("void set(int index, {0});", type); + if (property.HasSetter) + WriteLine("void set({1} {2}, {0} value);", type, indexParameterType, indexParameter.Name); WriteCloseBraceIndent(); } diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 1102ff97..a6281968 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -329,36 +329,37 @@ namespace CppSharp.Generators.CLI GeneratePropertyGetter(property.GetMethod, realOwner, property.Name, property.Type); - if (property.HasSetter) - GeneratePropertySetter(property.SetMethod, realOwner, property.Name, - property.Type); + if (property.HasSetter) + if (property.IsIndexer) + GeneratePropertySetter(property.SetMethod, realOwner, property.Name, + property.Type, property.GetMethod.Parameters[0]); + else + GeneratePropertySetter(property.SetMethod, realOwner, property.Name, + property.Type); } PopBlock(); - } - - private void GeneratePropertySetter(T decl, Class @class, string name, Type type) + } + + private void GeneratePropertySetter(T decl, Class @class, string name, Type type, Parameter indexParameter = null) where T : Declaration, ITypedDecl { if (decl == null) - return; + return; - var method = decl as Method; - var isIndexer = method != null && - method.OperatorKind == CXXOperatorKind.Subscript; - - var args = new List(); - if (isIndexer) - args.Add("int index"); + var args = new List(); + var isIndexer = indexParameter != null; + if (isIndexer) + args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name)); var function = decl as Function; - var argName = function != null ? function.Parameters[0].Name : "value"; + var argName = function != null && !isIndexer ? function.Parameters[0].Name : "value"; args.Add(string.Format("{0} {1}", type, argName)); WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class), name, string.Join(", ", args)); - WriteStartBraceIndent(); - + WriteStartBraceIndent(); + if (decl is Function && !isIndexer) { var func = decl as Function; @@ -397,7 +398,7 @@ namespace CppSharp.Generators.CLI @class.QualifiedOriginalName, decl.OriginalName); if (isIndexer) - variable += "(index)"; + variable += string.Format("({0})", indexParameter.Name); if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) Write(marshal.Context.SupportBefore); @@ -419,9 +420,12 @@ namespace CppSharp.Generators.CLI var isIndexer = method != null && method.OperatorKind == CXXOperatorKind.Subscript; - var args = new List(); - if (isIndexer) - args.Add("int index"); + var args = new List(); + if (isIndexer) + { + var indexParameter = method.Parameters[0]; + args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name)); + } WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class), name, string.Join(", ", args)); diff --git a/src/Generator/Passes/CheckOperatorsOverloads.cs b/src/Generator/Passes/CheckOperatorsOverloads.cs index 841bcd5b..50bd0ecd 100644 --- a/src/Generator/Passes/CheckOperatorsOverloads.cs +++ b/src/Generator/Passes/CheckOperatorsOverloads.cs @@ -102,12 +102,7 @@ namespace CppSharp.Passes // C++/CLI uses "default" as the indexer property name. if (Driver.Options.IsCLIGenerator) - property.Name = "default"; - - property.GetMethod.Parameters[0].Name = "index"; - - if (property.SetMethod != null) - property.SetMethod.Parameters[0].Name = "index"; + property.Name = "default"; property.Parameters.AddRange(@operator.Parameters); diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 4f410326..0ad5c6b7 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -233,7 +233,8 @@ public class BasicTests : GeneratorTestFixture public unsafe void TestIndexers() { var someStruct = new SomeStruct(); - Assert.That(someStruct[0], Is.EqualTo(1)); + Assert.That(someStruct[0], Is.EqualTo(1)); + Assert.That(someStruct["foo"], Is.EqualTo(1)); } [Test] diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 23fd08c8..7a8162bb 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -317,6 +317,7 @@ typedef struct DLL_API SomeStruct SomeStruct() : p(1) {} const foo_t& operator[](int i) const { return p; } foo_t operator[](int i) { return p; } + foo_t operator[](const char* name) { return p; } foo_t p; } SomeStruct;