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
Type type; Type type;
function.Type.IsPointerTo(out type); function.Type.IsPointerTo(out type);
PrimitiveType primitiveType; PrimitiveType primitiveType;
string internalFunction = GetFunctionNativeIdentifier(function); var internalFunction = GetFunctionNativeIdentifier(function);
if (type.IsPrimitiveType(out primitiveType)) if (type.IsPrimitiveType(out primitiveType))
{ {
WriteLine("*Internal.{0}({1}, {2}) = value;", internalFunction, WriteLine("*Internal.{0}({1}, {2}) = value;", internalFunction,
@ -937,9 +937,10 @@ namespace CppSharp.Generators.CSharp
Class @class; Class @class;
var isValueType = (type.GetFinalPointee() ?? type).TryGetClass(out @class) && var isValueType = (type.GetFinalPointee() ?? type).TryGetClass(out @class) &&
@class.IsValueType; @class.IsValueType;
var paramMarshal = GenerateFunctionParamMarshal(function.Parameters[0], 0, function);
WriteLine("*({0}.Internal*) Internal.{1}({2}, {3}) = {4}value.{5};", WriteLine("*({0}.Internal*) Internal.{1}({2}, {3}) = {4}value.{5};",
typeString, internalFunction, GetInstanceParam(function), typeString, internalFunction, GetInstanceParam(function),
function.Parameters[0].Name, paramMarshal.Context == null ? paramMarshal.Name : paramMarshal.Context.Return,
isValueType ? string.Empty : string.Format("*({0}.Internal*) ", typeString), isValueType ? string.Empty : string.Format("*({0}.Internal*) ", typeString),
Helpers.InstanceIdentifier); Helpers.InstanceIdentifier);
} }

4
tests/CSharp/CSharp.cs

@ -27,7 +27,7 @@ namespace CppSharp.Tests
public override string CSharpSignature(CSharpTypePrinterContext ctx) public override string CSharpSignature(CSharpTypePrinterContext ctx)
{ {
return this.CSharpSignatureType(ctx).ToString(); return CSharpSignatureType(ctx).ToString();
} }
public override void CSharpMarshalToNative(MarshalContext ctx) public override void CSharpMarshalToNative(MarshalContext ctx)
@ -48,7 +48,7 @@ namespace CppSharp.Tests
{ {
get get
{ {
var type = (TemplateSpecializationType) this.Type; var type = (TemplateSpecializationType) Type;
var pointeeType = type.Arguments[0].Type; var pointeeType = type.Arguments[0].Type;
var checker = new TypeIgnoreChecker(TypeMapDatabase); var checker = new TypeIgnoreChecker(TypeMapDatabase);
pointeeType.Visit(checker); pointeeType.Visit(checker);

55
tests/Common/Common.Tests.cs

@ -331,33 +331,40 @@ public class CommonTests : GeneratorTestFixture
} }
[Test] [Test]
public unsafe void TestIndexers() public void TestIndexers()
{ {
var indexedProperties = new TestIndexedProperties(); using (var indexedProperties = new TestIndexedProperties())
Assert.AreEqual(1, indexedProperties[0]); {
Assert.AreEqual(1, indexedProperties["foo"]); Assert.AreEqual(1, indexedProperties[0]);
indexedProperties[0] = 2; Assert.AreEqual(1, indexedProperties["foo"]);
Assert.AreEqual(2, indexedProperties[0]); indexedProperties[0] = 2;
indexedProperties[0f] = 3; Assert.AreEqual(2, indexedProperties[0]);
Assert.AreEqual(3, indexedProperties[0f]); indexedProperties[0f] = 3;
var properties = indexedProperties[(byte) 0]; Assert.AreEqual(3, indexedProperties[0f]);
Assert.AreEqual(0, properties.Field); var properties = indexedProperties[(byte) 0];
var newProperties = new TestProperties(); Assert.AreEqual(0, properties.Field);
newProperties.Field = 4; var newProperties = new TestProperties();
indexedProperties[(byte) 0] = newProperties; newProperties.Field = 4;
Assert.AreEqual(4, indexedProperties[(byte) 0].Field); indexedProperties[(byte) 0] = newProperties;
newProperties = indexedProperties[(short) 0]; Assert.AreEqual(4, indexedProperties[(byte) 0].Field);
Assert.AreEqual(4, newProperties.Field); newProperties = indexedProperties[(short) 0];
newProperties.Field = 5; Assert.AreEqual(4, newProperties.Field);
Assert.AreEqual(5, indexedProperties[(byte) 0].Field); newProperties.Field = 5;
} Assert.AreEqual(5, indexedProperties[(byte) 0].Field);
var bar = new Bar { A = 5 };
[Test] indexedProperties[0u] = bar;
public unsafe void TestOperators() 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(); var @class = new ClassWithOverloadedOperators();
Assert.AreEqual(1, (char) @class); Assert.AreEqual(1, (char) @class);
Assert.AreEqual(2, (int) @class); Assert.AreEqual(2, @class);
Assert.AreEqual(3, (short) @class); Assert.AreEqual(3, (short) @class);
} }

4
tests/Common/Common.cpp

@ -421,6 +421,10 @@ std::string HasStdString::testStdString(std::string s)
return s + "_test"; return s + "_test";
} }
TypeMappedIndex::TypeMappedIndex()
{
}
InternalCtorAmbiguity::InternalCtorAmbiguity(void* param) InternalCtorAmbiguity::InternalCtorAmbiguity(void* param)
{ {
// cause a crash to indicate this is the incorrect ctor to invoke // cause a crash to indicate this is the incorrect ctor to invoke

37
tests/Common/Common.cs

@ -1,11 +1,48 @@
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Generators; using CppSharp.Generators;
using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp;
using CppSharp.Passes; using CppSharp.Passes;
using CppSharp.Types;
using CppSharp.Utils; using CppSharp.Utils;
namespace CppSharp.Tests 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 class CommonTestsGenerator : GeneratorTest
{ {
public CommonTestsGenerator(GeneratorKind kind) public CommonTestsGenerator(GeneratorKind kind)

13
tests/Common/Common.h

@ -525,6 +525,12 @@ TestProperties::TestProperties() : Field(0) {}
int TestProperties::getFieldValue() { return Field; } int TestProperties::getFieldValue() { return Field; }
void TestProperties::setFieldValue(int Value) { Field = Value; } void TestProperties::setFieldValue(int Value) { Field = Value; }
class DLL_API TypeMappedIndex
{
public:
TypeMappedIndex();
};
class DLL_API TestIndexedProperties class DLL_API TestIndexedProperties
{ {
public: public:
@ -544,6 +550,7 @@ public:
// Should lead to a read-only indexer with argument type TestProperties // Should lead to a read-only indexer with argument type TestProperties
foo_t operator[](TestProperties b); foo_t operator[](TestProperties b);
Bar& operator[](unsigned long i); Bar& operator[](unsigned long i);
Bar& operator[](const TypeMappedIndex& key);
private: private:
foo_t p; foo_t p;
TestProperties f; TestProperties f;
@ -562,6 +569,10 @@ Bar& TestIndexedProperties::operator[](unsigned long i)
{ {
return bar; return bar;
} }
Bar& TestIndexedProperties::operator[](const TypeMappedIndex& key)
{
return bar;
}
struct DLL_API TestIndexedPropertiesInValueType struct DLL_API TestIndexedPropertiesInValueType
{ {
@ -584,7 +595,7 @@ void TestVariables::SetValue(int value) { VALUE = value; }
typedef const wchar_t * LPCWSTR; typedef const wchar_t * LPCWSTR;
struct DLL_API TestWideStrings struct DLL_API TestWideStrings
{ {
LPCWSTR GetWidePointer(); LPCWSTR GetWidePointer();
}; };
LPCWSTR TestWideStrings::GetWidePointer() { return L"Hello"; } LPCWSTR TestWideStrings::GetWidePointer() { return L"Hello"; }

Loading…
Cancel
Save