From 477dd914cb2a1bd0680a69facae8ca39603507b9 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Thu, 19 Nov 2015 19:08:47 +0200 Subject: [PATCH] Fixed setters of indexers when the key is type-mapped. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpTextTemplate.cs | 5 +- tests/CSharp/CSharp.cs | 4 +- tests/Common/Common.Tests.cs | 55 +++++++++++-------- tests/Common/Common.cpp | 4 ++ tests/Common/Common.cs | 37 +++++++++++++ tests/Common/Common.h | 13 ++++- 6 files changed, 89 insertions(+), 29 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index cdc80e43..44a9afe1 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -925,7 +925,7 @@ namespace CppSharp.Generators.CSharp Type type; function.Type.IsPointerTo(out type); PrimitiveType primitiveType; - string internalFunction = GetFunctionNativeIdentifier(function); + var internalFunction = GetFunctionNativeIdentifier(function); if (type.IsPrimitiveType(out primitiveType)) { WriteLine("*Internal.{0}({1}, {2}) = value;", internalFunction, @@ -937,9 +937,10 @@ namespace CppSharp.Generators.CSharp Class @class; var isValueType = (type.GetFinalPointee() ?? type).TryGetClass(out @class) && @class.IsValueType; + var paramMarshal = GenerateFunctionParamMarshal(function.Parameters[0], 0, function); WriteLine("*({0}.Internal*) Internal.{1}({2}, {3}) = {4}value.{5};", typeString, internalFunction, GetInstanceParam(function), - function.Parameters[0].Name, + paramMarshal.Context == null ? paramMarshal.Name : paramMarshal.Context.Return, isValueType ? string.Empty : string.Format("*({0}.Internal*) ", typeString), Helpers.InstanceIdentifier); } diff --git a/tests/CSharp/CSharp.cs b/tests/CSharp/CSharp.cs index 3271307c..e6be507d 100644 --- a/tests/CSharp/CSharp.cs +++ b/tests/CSharp/CSharp.cs @@ -27,7 +27,7 @@ namespace CppSharp.Tests public override string CSharpSignature(CSharpTypePrinterContext ctx) { - return this.CSharpSignatureType(ctx).ToString(); + return CSharpSignatureType(ctx).ToString(); } public override void CSharpMarshalToNative(MarshalContext ctx) @@ -48,7 +48,7 @@ namespace CppSharp.Tests { get { - var type = (TemplateSpecializationType) this.Type; + var type = (TemplateSpecializationType) Type; var pointeeType = type.Arguments[0].Type; var checker = new TypeIgnoreChecker(TypeMapDatabase); pointeeType.Visit(checker); diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 58dcfdb9..62e72810 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -331,33 +331,40 @@ public class CommonTests : GeneratorTestFixture } [Test] - public unsafe void TestIndexers() - { - var indexedProperties = new TestIndexedProperties(); - Assert.AreEqual(1, indexedProperties[0]); - Assert.AreEqual(1, indexedProperties["foo"]); - indexedProperties[0] = 2; - Assert.AreEqual(2, indexedProperties[0]); - indexedProperties[0f] = 3; - Assert.AreEqual(3, indexedProperties[0f]); - var properties = indexedProperties[(byte) 0]; - Assert.AreEqual(0, properties.Field); - var newProperties = new TestProperties(); - newProperties.Field = 4; - indexedProperties[(byte) 0] = newProperties; - Assert.AreEqual(4, indexedProperties[(byte) 0].Field); - newProperties = indexedProperties[(short) 0]; - Assert.AreEqual(4, newProperties.Field); - newProperties.Field = 5; - Assert.AreEqual(5, indexedProperties[(byte) 0].Field); - } - - [Test] - public unsafe void TestOperators() + public void TestIndexers() + { + using (var indexedProperties = new TestIndexedProperties()) + { + Assert.AreEqual(1, indexedProperties[0]); + Assert.AreEqual(1, indexedProperties["foo"]); + indexedProperties[0] = 2; + Assert.AreEqual(2, indexedProperties[0]); + indexedProperties[0f] = 3; + Assert.AreEqual(3, indexedProperties[0f]); + var properties = indexedProperties[(byte) 0]; + Assert.AreEqual(0, properties.Field); + var newProperties = new TestProperties(); + newProperties.Field = 4; + indexedProperties[(byte) 0] = newProperties; + Assert.AreEqual(4, indexedProperties[(byte) 0].Field); + newProperties = indexedProperties[(short) 0]; + Assert.AreEqual(4, newProperties.Field); + newProperties.Field = 5; + Assert.AreEqual(5, indexedProperties[(byte) 0].Field); + var bar = new Bar { A = 5 }; + indexedProperties[0u] = bar; + Assert.That(bar.A, Is.EqualTo(indexedProperties[0u].A)); + indexedProperties[(ushort) 0] = bar; + Assert.That(bar.A, Is.EqualTo(indexedProperties[(ushort) 0].A)); + } + } + + [Test] + public void TestOperators() { var @class = new ClassWithOverloadedOperators(); Assert.AreEqual(1, (char) @class); - Assert.AreEqual(2, (int) @class); + Assert.AreEqual(2, @class); Assert.AreEqual(3, (short) @class); } diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 81df2dc2..7aadc413 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -421,6 +421,10 @@ std::string HasStdString::testStdString(std::string s) return s + "_test"; } +TypeMappedIndex::TypeMappedIndex() +{ +} + InternalCtorAmbiguity::InternalCtorAmbiguity(void* param) { // cause a crash to indicate this is the incorrect ctor to invoke diff --git a/tests/Common/Common.cs b/tests/Common/Common.cs index 7634cfb2..9ad6d250 100644 --- a/tests/Common/Common.cs +++ b/tests/Common/Common.cs @@ -1,11 +1,48 @@ using System.Linq; using CppSharp.AST; using CppSharp.Generators; +using CppSharp.Generators.CLI; +using CppSharp.Generators.CSharp; using CppSharp.Passes; +using CppSharp.Types; using CppSharp.Utils; namespace CppSharp.Tests { + [TypeMap("TypeMappedIndex")] + public class TypeMappedIndex : TypeMap + { + public override string CLISignature(CLITypePrinterContext ctx) + { + return "unsigned short"; + } + + public override void CLIMarshalToManaged(MarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + + public override void CLIMarshalToNative(MarshalContext ctx) + { + ctx.Return.Write("::TypeMappedIndex()"); + } + + public override string CSharpSignature(CSharpTypePrinterContext ctx) + { + return "ushort"; + } + + public override void CSharpMarshalToManaged(MarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + + public override void CSharpMarshalToNative(MarshalContext ctx) + { + ctx.Return.Write("IntPtr.Zero"); + } + } + public class CommonTestsGenerator : GeneratorTest { public CommonTestsGenerator(GeneratorKind kind) diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 39282654..6656f8d1 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -525,6 +525,12 @@ TestProperties::TestProperties() : Field(0) {} int TestProperties::getFieldValue() { return Field; } void TestProperties::setFieldValue(int Value) { Field = Value; } +class DLL_API TypeMappedIndex +{ +public: + TypeMappedIndex(); +}; + class DLL_API TestIndexedProperties { public: @@ -544,6 +550,7 @@ public: // Should lead to a read-only indexer with argument type TestProperties foo_t operator[](TestProperties b); Bar& operator[](unsigned long i); + Bar& operator[](const TypeMappedIndex& key); private: foo_t p; TestProperties f; @@ -562,6 +569,10 @@ Bar& TestIndexedProperties::operator[](unsigned long i) { return bar; } +Bar& TestIndexedProperties::operator[](const TypeMappedIndex& key) +{ + return bar; +} struct DLL_API TestIndexedPropertiesInValueType { @@ -584,7 +595,7 @@ void TestVariables::SetValue(int value) { VALUE = value; } typedef const wchar_t * LPCWSTR; struct DLL_API TestWideStrings { - LPCWSTR GetWidePointer(); + LPCWSTR GetWidePointer(); }; LPCWSTR TestWideStrings::GetWidePointer() { return L"Hello"; }