Browse Source

Fixed setters of indexers when the key is type-mapped.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/256/merge
Dimitar Dobrev 10 years ago
parent
commit
477dd914cb
  1. 5
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 4
      tests/CSharp/CSharp.cs
  3. 55
      tests/Common/Common.Tests.cs
  4. 4
      tests/Common/Common.cpp
  5. 37
      tests/Common/Common.cs
  6. 13
      tests/Common/Common.h

5
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -925,7 +925,7 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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);
}

4
tests/CSharp/CSharp.cs

@ -27,7 +27,7 @@ namespace CppSharp.Tests @@ -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 @@ -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);

55
tests/Common/Common.Tests.cs

@ -331,33 +331,40 @@ public class CommonTests : GeneratorTestFixture @@ -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);
}

4
tests/Common/Common.cpp

@ -421,6 +421,10 @@ std::string HasStdString::testStdString(std::string s) @@ -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

37
tests/Common/Common.cs

@ -1,11 +1,48 @@ @@ -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)

13
tests/Common/Common.h

@ -525,6 +525,12 @@ TestProperties::TestProperties() : Field(0) {} @@ -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: @@ -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) @@ -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; } @@ -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"; }

Loading…
Cancel
Save