Browse Source

Fixed overloading of an indexed properties by removing the hard coded "int index" part.

pull/225/head
Elias Holzer 12 years ago
parent
commit
e6cb543c36
  1. 15
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 46
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  3. 7
      src/Generator/Passes/CheckOperatorsOverloads.cs
  4. 3
      tests/Basic/Basic.Tests.cs
  5. 1
      tests/Basic/Basic.h

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

@ -615,16 +615,19 @@ namespace CppSharp.Generators.CLI @@ -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();
}

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

@ -329,36 +329,37 @@ namespace CppSharp.Generators.CLI @@ -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>(T decl, Class @class, string name, Type type)
}
private void GeneratePropertySetter<T>(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<string>();
if (isIndexer)
args.Add("int index");
var args = new List<string>();
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 @@ -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 @@ -419,9 +420,12 @@ namespace CppSharp.Generators.CLI
var isIndexer = method != null &&
method.OperatorKind == CXXOperatorKind.Subscript;
var args = new List<string>();
if (isIndexer)
args.Add("int index");
var args = new List<string>();
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));

7
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -102,12 +102,7 @@ namespace CppSharp.Passes @@ -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);

3
tests/Basic/Basic.Tests.cs

@ -233,7 +233,8 @@ public class BasicTests : GeneratorTestFixture @@ -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]

1
tests/Basic/Basic.h

@ -317,6 +317,7 @@ typedef struct DLL_API SomeStruct @@ -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;

Loading…
Cancel
Save