Browse Source

Extended type maps with the ability to insert custom code instead of a copy ctor invocation.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/170/head
Dimitar Dobrev 12 years ago
parent
commit
cfeed2619a
  1. 22
      src/AST/Class.cs
  2. 25
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  3. 9
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 5
      src/Generator/Types/TypeMap.cs

22
src/AST/Class.cs

@ -33,10 +33,26 @@ namespace CppSharp.AST @@ -33,10 +33,26 @@ namespace CppSharp.AST
get
{
Class @class;
if (!Type.IsTagDecl(out @class))
throw new NotSupportedException();
if (Type.IsTagDecl(out @class))
return @class;
var type = Type.Desugar() as TemplateSpecializationType;
if (type == null)
{
TypedefType typedef;
if (Type.IsPointerTo(out typedef))
{
type = (TemplateSpecializationType) typedef.Desugar();
}
else
{
Type.IsPointerTo(out type);
}
}
var templatedClass = ((ClassTemplate) type.Template).TemplatedClass;
return templatedClass.IsIncomplete
? (Class) templatedClass.CompleteDeclaration
: templatedClass;
}
}

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

@ -252,10 +252,6 @@ namespace CppSharp.Generators.CSharp @@ -252,10 +252,6 @@ namespace CppSharp.Generators.CSharp
if (VarSuffix > 0)
instanceName += VarSuffix;
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
instanceName, @class.Layout.Size);
if (@class.HasNonTrivialCopyConstructor)
{
// Find a valid copy constructor overload.
@ -266,13 +262,27 @@ namespace CppSharp.Generators.CSharp @@ -266,13 +262,27 @@ namespace CppSharp.Generators.CSharp
throw new NotSupportedException("Expected a valid copy constructor");
// Call the copy constructor.
TypeMap typeMap;
if (copyCtorMethod.Ignore && FindTypeMap(ctx.Driver.TypeDatabase, @class, out typeMap))
{
typeMap.CSharpMarshalCopyCtorToManaged(Context);
}
else
{
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
instanceName, @class.Layout.Size);
Context.SupportBefore.WriteLine("{0}.Internal.{1}({2}, new global::System.IntPtr(&{3}));",
QualifiedIdentifier(@class),
CSharpTextTemplate.GetFunctionNativeIdentifier(copyCtorMethod),
instanceName, instance);
}
}
else
{
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
instanceName, @class.Layout.Size);
instance = instance.Trim('*');
Context.SupportBefore.WriteLine(
"CppSharp.Runtime.Helpers.memcpy({0}, new IntPtr(&{1}), new UIntPtr({2}));",
@ -291,6 +301,13 @@ namespace CppSharp.Generators.CSharp @@ -291,6 +301,13 @@ namespace CppSharp.Generators.CSharp
return true;
}
private static bool FindTypeMap(ITypeMapDatabase typeMapDatabase,
Class @class, out TypeMap typeMap)
{
return typeMapDatabase.FindTypeMap(@class, out typeMap) ||
(@class.HasBase && FindTypeMap(typeMapDatabase, @class.Bases[0].Class, out typeMap));
}
public override bool VisitEnumDecl(Enumeration @enum)
{
Context.Return.Write("{0}", Context.ReturnVarName);

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

@ -7,6 +7,7 @@ using System.Text; @@ -7,6 +7,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Web.Util;
using CppSharp.AST;
using CppSharp.Types;
using CppSharp.Utils;
using Attribute = CppSharp.AST.Attribute;
using Type = CppSharp.AST.Type;
@ -777,13 +778,11 @@ namespace CppSharp.Generators.CSharp @@ -777,13 +778,11 @@ namespace CppSharp.Generators.CSharp
{
foreach (var @base in @class.Bases)
{
if (!@base.IsClass) continue;
var baseClass = @base.Class;
if (baseClass.Ignore)
TypeMap typeMap;
if (!Driver.TypeDatabase.FindTypeMap(@base.Type, out typeMap) && @base.Class.Ignore)
continue;
GenerateClassFields(baseClass, action);
GenerateClassFields(@base.Class, action);
}
foreach (var field in @class.Fields)

5
src/Generator/Types/TypeMap.cs

@ -59,6 +59,11 @@ namespace CppSharp.Types @@ -59,6 +59,11 @@ namespace CppSharp.Types
throw new NotImplementedException();
}
public virtual void CSharpMarshalCopyCtorToManaged(MarshalContext ctx)
{
}
#endregion
#region C++/CLI backend

Loading…
Cancel
Save