diff --git a/src/AST/Type.cs b/src/AST/Type.cs index 84818c3a..a9264223 100644 --- a/src/AST/Type.cs +++ b/src/AST/Type.cs @@ -71,6 +71,26 @@ namespace CppSharp.AST return pointer.IsReference; } + public bool IsPointerToPrimitiveType() + { + var ptr = this as PointerType; + if (ptr == null) + return false; + PrimitiveType primitiveType; + return ptr.Pointee.IsPrimitiveType(out primitiveType); + } + + public bool IsPointerToPrimitiveType(out PrimitiveType primitive) + { + var ptr = this as PointerType; + if (ptr == null) + { + primitive = PrimitiveType.Null; + return false; + } + return ptr.Pointee.IsPrimitiveType(out primitive); + } + public bool IsPointerToPrimitiveType(PrimitiveType primitive) { var ptr = this as PointerType; diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index e50dea52..d97fd17b 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -754,7 +754,7 @@ namespace CppSharp.Generators.CSharp param.QualifiedType = function.Parameters[0].QualifiedType; var method = function as Method; - if (method != null && method.IsSynthetized) + if (method != null && method.OperatorKind == CXXOperatorKind.Subscript) { GenerateIndexerSetter(returnType, method); } @@ -794,7 +794,9 @@ namespace CppSharp.Generators.CSharp PrimitiveType primitiveType; if (type.IsPrimitiveType(out primitiveType)) { - WriteLine("*this[{0}] = *value;", function.Parameters[0].Name); + string internalFunction = GetFunctionNativeIdentifier(function); + WriteLine("*Internal.{0}({1}, {2}) = value;", internalFunction, + Helpers.InstanceIdentifier, function.Parameters[0].Name); } else { @@ -813,7 +815,13 @@ namespace CppSharp.Generators.CSharp if (decl is Function) { var function = decl as Function; + bool isPrimitiveIndexer = function.OperatorKind == CXXOperatorKind.Subscript && + function.ReturnType.Type.IsPointerToPrimitiveType(); + if (isPrimitiveIndexer) + TypePrinter.PushContext(CSharpTypePrinterContextKind.PrimitiveIndexer); GenerateInternalFunctionCall(function); + if (isPrimitiveIndexer) + TypePrinter.PopContext(); } else if (decl is Field) { @@ -920,9 +928,12 @@ namespace CppSharp.Generators.CSharp foreach (var prop in @class.Properties.Where(p => !p.Ignore)) { PushBlock(CSharpBlockKind.Property); + var type = prop.Type; + if (prop.Parameters.Count > 0 && prop.Type.IsPointerToPrimitiveType()) + type = ((PointerType) prop.Type).Pointee; WriteLine("{0} {1} {2}", prop.Access == AccessSpecifier.Public ? "public" : "protected", - prop.Type, GetPropertyName(prop)); + type, GetPropertyName(prop)); WriteStartBraceIndent(); if (prop.Field != null) @@ -1831,7 +1842,10 @@ namespace CppSharp.Generators.CSharp if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) Write(marshal.Context.SupportBefore); - WriteLine("return {0};", marshal.Context.Return); + WriteLine( + TypePrinter.ContextKind == CSharpTypePrinterContextKind.PrimitiveIndexer + ? "return *{0};" + : "return {0};", marshal.Context.Return); } } diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 33dedb20..e0aa9103 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -11,7 +11,8 @@ namespace CppSharp.Generators.CSharp Native, Managed, ManagedPointer, - GenericDelegate + GenericDelegate, + PrimitiveIndexer } public class CSharpTypePrinterContext : TypePrinterContext diff --git a/src/Generator/Passes/CheckOperatorsOverloads.cs b/src/Generator/Passes/CheckOperatorsOverloads.cs index 7fd91748..2d03ba3c 100644 --- a/src/Generator/Passes/CheckOperatorsOverloads.cs +++ b/src/Generator/Passes/CheckOperatorsOverloads.cs @@ -95,14 +95,7 @@ namespace CppSharp.Passes }; property.Parameters.AddRange(@operator.Parameters); if (!@operator.ReturnType.Qualifiers.IsConst && @operator.ReturnType.Type.IsAddress()) - { - property.SetMethod = new Method(@operator) - { - ReturnType = new QualifiedType( - new BuiltinType(PrimitiveType.Void)), - IsSynthetized = true - }; - } + property.SetMethod = @operator; @class.Properties.Add(property); @operator.IsGenerated = false; } diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 1cf80a9a..47a60df1 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -12,13 +12,9 @@ public class CSharpTempTests { var foo = new Foo(); - // TODO: Most of the ugliness below will disappear when pointers to simple types are represented by C#-pointers or ref modifiers instead of the nasty IntPtr - var value = *foo[0]; - Assert.That(value, Is.EqualTo(50)); - int x = 250; - foo[0] = &x; - value = *foo[0]; - Assert.That(value, Is.EqualTo(x)); + Assert.That(foo[0], Is.EqualTo(50)); + foo[0] = 250; + Assert.That(foo[0], Is.EqualTo(250)); Assert.That(foo[(uint) 0], Is.EqualTo(15));