Browse Source

Some refactoring to remove redundant members and casts.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/560/head
Dimitar Dobrev 10 years ago
parent
commit
f848f62180
  1. 5
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 27
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  3. 4
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 9
      src/Generator/Generators/Marshal.cs
  5. 22
      tests/CSharp/CSharp.Tests.cs

5
src/Generator/Generators/CLI/CLIMarshal.cs

@ -10,12 +10,11 @@ using Type = CppSharp.AST.Type; @@ -10,12 +10,11 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.CLI
{
public class CLIMarshalNativeToManagedPrinter : MarshalPrinter
public class CLIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext>
{
public CLIMarshalNativeToManagedPrinter(MarshalContext marshalContext)
: base(marshalContext)
{
Context.MarshalToManaged = this;
}
public override bool VisitType(Type type, TypeQualifiers quals)
@ -387,7 +386,7 @@ namespace CppSharp.Generators.CLI @@ -387,7 +386,7 @@ namespace CppSharp.Generators.CLI
}
}
public class CLIMarshalManagedToNativePrinter : MarshalPrinter
public class CLIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext>
{
public readonly TextGenerator VarPrefix;
public readonly TextGenerator ArgumentPrefix;

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

@ -33,13 +33,8 @@ namespace CppSharp.Generators.CSharp @@ -33,13 +33,8 @@ namespace CppSharp.Generators.CSharp
public bool HasFixedBlock { get; set; }
}
public abstract class CSharpMarshalPrinter : MarshalPrinter
public abstract class CSharpMarshalPrinter : MarshalPrinter<CSharpMarshalContext>
{
public CSharpMarshalContext CSharpContext
{
get { return Context as CSharpMarshalContext; }
}
protected CSharpMarshalPrinter(CSharpMarshalContext context)
: base(context)
{
@ -59,7 +54,6 @@ namespace CppSharp.Generators.CSharp @@ -59,7 +54,6 @@ namespace CppSharp.Generators.CSharp
public CSharpMarshalNativeToManagedPrinter(CSharpMarshalContext context)
: base(context)
{
Context.MarshalToManaged = this;
}
public bool MarshalsParameter { get; set; }
@ -357,7 +351,6 @@ namespace CppSharp.Generators.CSharp @@ -357,7 +351,6 @@ namespace CppSharp.Generators.CSharp
public CSharpMarshalManagedToNativePrinter(CSharpMarshalContext context)
: base(context)
{
Context.MarshalToNative = this;
}
public override bool VisitType(Type type, TypeQualifiers quals)
@ -402,7 +395,7 @@ namespace CppSharp.Generators.CSharp @@ -402,7 +395,7 @@ namespace CppSharp.Generators.CSharp
Context.SupportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, ptr, Context.Parameter.Name);
Context.SupportBefore.WriteStartBraceIndent();
Context.Return.Write("new global::System.IntPtr({0})", ptr);
CSharpContext.HasFixedBlock = true;
Context.HasFixedBlock = true;
}
else
{
@ -458,10 +451,10 @@ namespace CppSharp.Generators.CSharp @@ -458,10 +451,10 @@ namespace CppSharp.Generators.CSharp
if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef())
{
string refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex);
var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex);
Context.SupportBefore.WriteLine("fixed ({0} {1} = &{2})",
pointer, refParamPtr, Context.Parameter.Name);
CSharpContext.HasFixedBlock = true;
Context.HasFixedBlock = true;
Context.SupportBefore.WriteStartBraceIndent();
Context.Return.Write(refParamPtr);
return true;
@ -478,17 +471,17 @@ namespace CppSharp.Generators.CSharp @@ -478,17 +471,17 @@ namespace CppSharp.Generators.CSharp
if (param.IsOut)
{
Context.Return.Write("IntPtr.Zero");
CSharpContext.ArgumentPrefix.Write("&");
Context.ArgumentPrefix.Write("&");
}
else if (param.IsInOut)
{
Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
CSharpContext.ArgumentPrefix.Write("&");
Context.ArgumentPrefix.Write("&");
}
else
{
Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
CSharpContext.Cleanup.WriteLine("Marshal.FreeHGlobal({0});", Context.ArgName);
Context.Cleanup.WriteLine("Marshal.FreeHGlobal({0});", Context.ArgName);
}
return true;
}
@ -602,7 +595,7 @@ namespace CppSharp.Generators.CSharp @@ -602,7 +595,7 @@ namespace CppSharp.Generators.CSharp
var decl = typedef.Declaration;
FunctionType func;
if (decl.Type.IsPointerTo<FunctionType>(out func))
if (decl.Type.IsPointerTo(out func))
{
VisitDelegateType(func, typedef.Declaration.OriginalName);
return true;
@ -724,7 +717,7 @@ namespace CppSharp.Generators.CSharp @@ -724,7 +717,7 @@ namespace CppSharp.Generators.CSharp
public static void CSharpMarshalToNative(this QualifiedType type,
CSharpMarshalManagedToNativePrinter printer)
{
(printer.Context as CSharpMarshalContext).FullType = type;
printer.Context.FullType = type;
type.Visit(printer);
}
@ -743,7 +736,7 @@ namespace CppSharp.Generators.CSharp @@ -743,7 +736,7 @@ namespace CppSharp.Generators.CSharp
public static void CSharpMarshalToManaged(this QualifiedType type,
CSharpMarshalNativeToManagedPrinter printer)
{
(printer.Context as CSharpMarshalContext).FullType = type;
printer.Context.FullType = type;
type.Visit(printer);
}

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

@ -2638,8 +2638,8 @@ namespace CppSharp.Generators.CSharp @@ -2638,8 +2638,8 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} = {1};", param.Name, marshal.Context.Return);
if (!string.IsNullOrWhiteSpace(marshal.CSharpContext.Cleanup))
cleanups.Add(marshal.CSharpContext.Cleanup);
if (!string.IsNullOrWhiteSpace(marshal.Context.Cleanup))
cleanups.Add(marshal.Context.Cleanup);
}
}

9
src/Generator/Generators/Marshal.cs

@ -14,8 +14,7 @@ namespace CppSharp.Generators @@ -14,8 +14,7 @@ namespace CppSharp.Generators
public Driver Driver { get; private set; }
public MarshalPrinter MarshalToManaged;
public MarshalPrinter MarshalToNative;
public MarshalPrinter<MarshalContext> MarshalToNative;
public TextGenerator SupportBefore { get; private set; }
public TextGenerator Return { get; private set; }
@ -33,11 +32,11 @@ namespace CppSharp.Generators @@ -33,11 +32,11 @@ namespace CppSharp.Generators
public string MarshalVarPrefix { get; set; }
}
public abstract class MarshalPrinter : AstVisitor
public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext
{
public MarshalContext Context { get; private set; }
public T Context { get; private set; }
protected MarshalPrinter(MarshalContext ctx)
protected MarshalPrinter(T ctx)
{
Context = ctx;
}

22
tests/CSharp/CSharp.Tests.cs

@ -102,8 +102,7 @@ public class CSharpTests : GeneratorTestFixture @@ -102,8 +102,7 @@ public class CSharpTests : GeneratorTestFixture
Assert.That(proprietor.Value, Is.EqualTo(20));
proprietor.Prop = 50;
Assert.That(proprietor.Prop, Is.EqualTo(50));
var p = new P((IQux) null);
p.Value = 20;
var p = new P((IQux) null) { Value = 20 };
Assert.That(p.Value, Is.EqualTo(30));
p.Prop = 50;
Assert.That(p.Prop, Is.EqualTo(150));
@ -299,9 +298,9 @@ public class CSharpTests : GeneratorTestFixture @@ -299,9 +298,9 @@ public class CSharpTests : GeneratorTestFixture
var bar = new Bar();
testNativeToManagedMap.PropertyWithNoVirtualDtor = bar;
Assert.AreSame(bar, testNativeToManagedMap.PropertyWithNoVirtualDtor);
Assert.IsTrue(Bar.NativeToManagedMap.ContainsKey(bar.__Instance));
Assert.IsTrue(Qux.NativeToManagedMap.ContainsKey(bar.__Instance));
bar.Dispose();
Assert.IsFalse(Bar.NativeToManagedMap.ContainsKey(bar.__Instance));
Assert.IsFalse(Qux.NativeToManagedMap.ContainsKey(bar.__Instance));
}
}
@ -393,16 +392,11 @@ public class CSharpTests : GeneratorTestFixture @@ -393,16 +392,11 @@ public class CSharpTests : GeneratorTestFixture
public void TestFixedArrayRefType()
{
Foo[] foos = new Foo[4];
foos[0] = new Foo();
foos[0].A = 5;
foos[1] = new Foo();
foos[1].A = 6;
foos[2] = new Foo();
foos[2].A = 7;
foos[3] = new Foo();
foos[3].A = 8;
Bar bar = new Bar();
bar.Foos = foos;
foos[0] = new Foo { A = 5 };
foos[1] = new Foo { A = 6 };
foos[2] = new Foo { A = 7 };
foos[3] = new Foo { A = 8 };
Bar bar = new Bar { Foos = foos };
Foo[] retFoos = bar.Foos;
Assert.AreEqual(5, retFoos[0].A);

Loading…
Cancel
Save