Browse Source

Moved the generation of a virtual table call to a separate function, for independence on back-ends.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/53/head
Dimitar Dobrev 12 years ago
parent
commit
463864e71c
  1. 40
      src/Generator/AST/VTables.cs
  2. 23
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

40
src/Generator/AST/VTables.cs

@ -1,5 +1,9 @@ @@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CppSharp.Generators;
using CppSharp.Generators.CSharp;
namespace CppSharp.AST
{
@ -78,5 +82,41 @@ namespace CppSharp.AST @@ -78,5 +82,41 @@ namespace CppSharp.AST
throw new NotSupportedException();
}
public static string GetVirtualCallDelegate(INamedDecl method, Class @class, out string delegateId)
{
var virtualCallBuilder = new StringBuilder();
virtualCallBuilder.AppendFormat(
"void* vtable = *((void**) {0}.ToPointer());",
Helpers.InstanceIdentifier);
virtualCallBuilder.AppendLine();
int i;
switch (@class.Layout.ABI)
{
case CppAbi.Microsoft:
i = (from table in @class.Layout.VFTables
let j = table.Layout.Components.FindIndex(m => m.Method == method)
where j >= 0
select j).First();
break;
default:
i = @class.Layout.Layout.Components.FindIndex(m => m.Method == method);
break;
}
virtualCallBuilder.AppendFormat(
"void* slot = *((void**) vtable + {0} * IntPtr.Size);", i);
virtualCallBuilder.AppendLine();
string @delegate = method.Name + "Delegate";
delegateId = Generator.GeneratedIdentifier(@delegate);
virtualCallBuilder.AppendFormat(
"var {1} = ({0}) Marshal.GetDelegateForFunctionPointer(new IntPtr(slot), typeof({0}));",
@delegate, delegateId);
virtualCallBuilder.AppendLine();
return virtualCallBuilder.ToString();
}
}
}

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

@ -3,6 +3,7 @@ using System.Collections.Generic; @@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using CppSharp.AST;
using CppSharp.Utils;
using Type = CppSharp.AST.Type;
@ -1569,26 +1570,8 @@ namespace CppSharp.Generators.CSharp @@ -1569,26 +1570,8 @@ namespace CppSharp.Generators.CSharp
private void GenerateVirtualTableFunctionCall(Function method, Class @class)
{
WriteLine("void* vtable = *((void**) {0}.ToPointer());",
Helpers.InstanceIdentifier);
int i;
switch (Driver.Options.Abi)
{
case CppAbi.Microsoft:
i = (from table in @class.Layout.VFTables
let j = table.Layout.Components.FindIndex(m => m.Method == method)
where j >= 0
select j).First();
break;
default:
i = @class.Layout.Layout.Components.FindIndex(m => m.Method == method);
break;
}
WriteLine("void* slot = *((void**) vtable + {0} * IntPtr.Size);", i);
string @delegate = method.Name + "Delegate";
string delegateId = GeneratedIdentifier(@delegate);
WriteLine("var {1} = ({0}) Marshal.GetDelegateForFunctionPointer(new IntPtr(slot), typeof({0}));",
@delegate, delegateId);
string delegateId;
Write(VTables.GetVirtualCallDelegate(method, @class, out delegateId));
GenerateFunctionCall(delegateId, method.Parameters, method);
}

Loading…
Cancel
Save