Browse Source

Changed writable indexers of primitive types to use the types themselves instead of pointers.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/65/head
Dimitar Dobrev 12 years ago
parent
commit
6f3224ca60
  1. 20
      src/AST/Type.cs
  2. 22
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 3
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 9
      src/Generator/Passes/CheckOperatorsOverloads.cs
  5. 10
      tests/CSharpTemp/CSharpTemp.Tests.cs

20
src/AST/Type.cs

@ -71,6 +71,26 @@ namespace CppSharp.AST
return pointer.IsReference; 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) public bool IsPointerToPrimitiveType(PrimitiveType primitive)
{ {
var ptr = this as PointerType; var ptr = this as PointerType;

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

@ -754,7 +754,7 @@ namespace CppSharp.Generators.CSharp
param.QualifiedType = function.Parameters[0].QualifiedType; param.QualifiedType = function.Parameters[0].QualifiedType;
var method = function as Method; var method = function as Method;
if (method != null && method.IsSynthetized) if (method != null && method.OperatorKind == CXXOperatorKind.Subscript)
{ {
GenerateIndexerSetter(returnType, method); GenerateIndexerSetter(returnType, method);
} }
@ -794,7 +794,9 @@ namespace CppSharp.Generators.CSharp
PrimitiveType primitiveType; PrimitiveType primitiveType;
if (type.IsPrimitiveType(out 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 else
{ {
@ -813,7 +815,13 @@ namespace CppSharp.Generators.CSharp
if (decl is Function) if (decl is Function)
{ {
var function = decl as Function; var function = decl as Function;
bool isPrimitiveIndexer = function.OperatorKind == CXXOperatorKind.Subscript &&
function.ReturnType.Type.IsPointerToPrimitiveType();
if (isPrimitiveIndexer)
TypePrinter.PushContext(CSharpTypePrinterContextKind.PrimitiveIndexer);
GenerateInternalFunctionCall(function); GenerateInternalFunctionCall(function);
if (isPrimitiveIndexer)
TypePrinter.PopContext();
} }
else if (decl is Field) else if (decl is Field)
{ {
@ -920,9 +928,12 @@ namespace CppSharp.Generators.CSharp
foreach (var prop in @class.Properties.Where(p => !p.Ignore)) foreach (var prop in @class.Properties.Where(p => !p.Ignore))
{ {
PushBlock(CSharpBlockKind.Property); PushBlock(CSharpBlockKind.Property);
var type = prop.Type;
if (prop.Parameters.Count > 0 && prop.Type.IsPointerToPrimitiveType())
type = ((PointerType) prop.Type).Pointee;
WriteLine("{0} {1} {2}", WriteLine("{0} {1} {2}",
prop.Access == AccessSpecifier.Public ? "public" : "protected", prop.Access == AccessSpecifier.Public ? "public" : "protected",
prop.Type, GetPropertyName(prop)); type, GetPropertyName(prop));
WriteStartBraceIndent(); WriteStartBraceIndent();
if (prop.Field != null) if (prop.Field != null)
@ -1831,7 +1842,10 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(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);
} }
} }

3
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -11,7 +11,8 @@ namespace CppSharp.Generators.CSharp
Native, Native,
Managed, Managed,
ManagedPointer, ManagedPointer,
GenericDelegate GenericDelegate,
PrimitiveIndexer
} }
public class CSharpTypePrinterContext : TypePrinterContext public class CSharpTypePrinterContext : TypePrinterContext

9
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -95,14 +95,7 @@ namespace CppSharp.Passes
}; };
property.Parameters.AddRange(@operator.Parameters); property.Parameters.AddRange(@operator.Parameters);
if (!@operator.ReturnType.Qualifiers.IsConst && @operator.ReturnType.Type.IsAddress()) if (!@operator.ReturnType.Qualifiers.IsConst && @operator.ReturnType.Type.IsAddress())
{ property.SetMethod = @operator;
property.SetMethod = new Method(@operator)
{
ReturnType = new QualifiedType(
new BuiltinType(PrimitiveType.Void)),
IsSynthetized = true
};
}
@class.Properties.Add(property); @class.Properties.Add(property);
@operator.IsGenerated = false; @operator.IsGenerated = false;
} }

10
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -12,13 +12,9 @@ public class CSharpTempTests
{ {
var foo = new Foo(); 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 Assert.That(foo[0], Is.EqualTo(50));
var value = *foo[0]; foo[0] = 250;
Assert.That(value, Is.EqualTo(50)); Assert.That(foo[0], Is.EqualTo(250));
int x = 250;
foo[0] = &x;
value = *foo[0];
Assert.That(value, Is.EqualTo(x));
Assert.That(foo[(uint) 0], Is.EqualTo(15)); Assert.That(foo[(uint) 0], Is.EqualTo(15));

Loading…
Cancel
Save