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. 9
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 26
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  3. 5
      src/Generator/Passes/CheckOperatorsOverloads.cs
  4. 1
      tests/Basic/Basic.Tests.cs
  5. 1
      tests/Basic/Basic.h

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

@ -616,15 +616,18 @@ namespace CppSharp.Generators.CLI
public void GenerateIndexer(Property property) 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(); WriteStartBraceIndent();
if (property.HasGetter) if (property.HasGetter)
WriteLine("{0} get(int index);", type); WriteLine("{0} get({1} {2});", type, indexParameterType, indexParameter.Name);
if (property.HasSetter) if (property.HasSetter)
WriteLine("void set(int index, {0});", type); WriteLine("void set({1} {2}, {0} value);", type, indexParameterType, indexParameter.Name);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }

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

@ -330,28 +330,29 @@ namespace CppSharp.Generators.CLI
property.Type); property.Type);
if (property.HasSetter) if (property.HasSetter)
GeneratePropertySetter(property.SetMethod, realOwner, property.Name, if (property.IsIndexer)
property.Type); GeneratePropertySetter(property.SetMethod, realOwner, property.Name,
property.Type, property.GetMethod.Parameters[0]);
else
GeneratePropertySetter(property.SetMethod, realOwner, property.Name,
property.Type);
} }
PopBlock(); 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 where T : Declaration, ITypedDecl
{ {
if (decl == null) if (decl == null)
return; return;
var method = decl as Method;
var isIndexer = method != null &&
method.OperatorKind == CXXOperatorKind.Subscript;
var args = new List<string>(); var args = new List<string>();
var isIndexer = indexParameter != null;
if (isIndexer) if (isIndexer)
args.Add("int index"); args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name));
var function = decl as Function; 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)); args.Add(string.Format("{0} {1}", type, argName));
WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class),
@ -397,7 +398,7 @@ namespace CppSharp.Generators.CLI
@class.QualifiedOriginalName, decl.OriginalName); @class.QualifiedOriginalName, decl.OriginalName);
if (isIndexer) if (isIndexer)
variable += "(index)"; variable += string.Format("({0})", indexParameter.Name);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
@ -421,7 +422,10 @@ namespace CppSharp.Generators.CLI
var args = new List<string>(); var args = new List<string>();
if (isIndexer) if (isIndexer)
args.Add("int index"); {
var indexParameter = method.Parameters[0];
args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name));
}
WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class), WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class),
name, string.Join(", ", args)); name, string.Join(", ", args));

5
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -104,11 +104,6 @@ namespace CppSharp.Passes
if (Driver.Options.IsCLIGenerator) if (Driver.Options.IsCLIGenerator)
property.Name = "default"; property.Name = "default";
property.GetMethod.Parameters[0].Name = "index";
if (property.SetMethod != null)
property.SetMethod.Parameters[0].Name = "index";
property.Parameters.AddRange(@operator.Parameters); property.Parameters.AddRange(@operator.Parameters);
@class.Properties.Add(property); @class.Properties.Add(property);

1
tests/Basic/Basic.Tests.cs

@ -234,6 +234,7 @@ public class BasicTests : GeneratorTestFixture
{ {
var someStruct = new SomeStruct(); 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] [Test]

1
tests/Basic/Basic.h

@ -317,6 +317,7 @@ typedef struct DLL_API SomeStruct
SomeStruct() : p(1) {} SomeStruct() : p(1) {}
const foo_t& operator[](int i) const { return p; } const foo_t& operator[](int i) const { return p; }
foo_t operator[](int i) { return p; } foo_t operator[](int i) { return p; }
foo_t operator[](const char* name) { return p; }
foo_t p; foo_t p;
} }
SomeStruct; SomeStruct;

Loading…
Cancel
Save