Browse Source

Fixed the generated C# for const ref parameter of Indexer (#851)

Fixes #628
pull/849/merge
Mohit Mohta 9 years ago committed by Dimitar Dobrev
parent
commit
6bd00372c0
  1. 19
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 12
      tests/CSharp/CSharp.Tests.cs
  3. 11
      tests/CSharp/CSharp.h

19
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -245,7 +245,7 @@ namespace CppSharp.Generators.CSharp
Context.Before.WriteLine("var {0} = {1};", ptrName, Context.Before.WriteLine("var {0} = {1};", ptrName,
Context.ReturnVarName); Context.ReturnVarName);
var res = $"{ptrName} == IntPtr.Zero? null : ({typedef})Marshal.GetDelegateForFunctionPointer({ptrName}, typeof({typedef}))"; var res = $"{ptrName} == IntPtr.Zero? null : ({typedef})Marshal.GetDelegateForFunctionPointer({ptrName}, typeof({typedef}))";
Context.Return.Write(res); Context.Return.Write(res);
return true; return true;
@ -492,7 +492,8 @@ namespace CppSharp.Generators.CSharp
var pointee = pointer.Pointee.Desugar(); var pointee = pointer.Pointee.Desugar();
if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() && if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() &&
Context.MarshalKind != MarshalKind.VTableReturnValue) Context.MarshalKind != MarshalKind.VTableReturnValue &&
Context.Function.OperatorKind != CXXOperatorKind.Subscript)
{ {
var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex); var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex);
var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType; var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType;
@ -580,13 +581,17 @@ namespace CppSharp.Generators.CSharp
if (isRefParam) if (isRefParam)
{ {
var typeName = Type.TypePrinterDelegate(finalPointee); var typeName = Type.TypePrinterDelegate(finalPointee);
if (Context.Function.OperatorKind == CXXOperatorKind.Subscript)
if (param.IsInOut) Context.Return.Write(param.Name);
Context.Before.WriteLine("{0} _{1} = {1};", typeName, param.Name);
else else
Context.Before.WriteLine("{0} _{1};", typeName, param.Name); {
if (param.IsInOut)
Context.Before.WriteLine($"{typeName} _{param.Name} = {param.Name};");
else
Context.Before.WriteLine($"{typeName} _{param.Name};");
Context.Return.Write("&_{0}", param.Name); Context.Return.Write($"&_{param.Name}");
}
} }
else else
{ {

12
tests/CSharp/CSharp.Tests.cs

@ -834,4 +834,14 @@ public unsafe class CSharpTests : GeneratorTestFixture
complexArrayElement.Dispose(); complexArrayElement.Dispose();
} }
} }
}
[Test]
public void TestConstRefIndexer()
{
using (var indexproperty = new TestIndexedProperties())
{
int a = 2;
Assert.That(indexproperty[&a], Is.EqualTo(2));
}
}
}

11
tests/CSharp/CSharp.h

@ -1193,3 +1193,14 @@ struct HasComplexArray
{ {
ComplexArrayElement complexArray[ARRAY_LENGTH_MACRO]; ComplexArrayElement complexArray[ARRAY_LENGTH_MACRO];
}; };
class DLL_API TestIndexedProperties
{
public:
int operator[](const int& key);
};
int TestIndexedProperties::operator[](const int& key)
{
return key;
}
Loading…
Cancel
Save