From 463864e71cfe63782b20390ba557d31e64d20f87 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Mon, 9 Sep 2013 18:38:56 +0300 Subject: [PATCH] Moved the generation of a virtual table call to a separate function, for independence on back-ends. Signed-off-by: Dimitar Dobrev --- src/Generator/AST/VTables.cs | 40 +++++++++++++++++++ .../Generators/CSharp/CSharpTextTemplate.cs | 23 ++--------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/Generator/AST/VTables.cs b/src/Generator/AST/VTables.cs index fa6cb5cf..f7e15e76 100644 --- a/src/Generator/AST/VTables.cs +++ b/src/Generator/AST/VTables.cs @@ -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 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(); + } } } diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 941f5590..b95260d9 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -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 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); }