|
|
|
@ -438,6 +438,8 @@ namespace CppSharp.Generators.CSharp
@@ -438,6 +438,8 @@ namespace CppSharp.Generators.CSharp
|
|
|
|
|
var valueType = Options.GenerateFinalizerFor(@class) ? $"global::System.WeakReference<{printedClass}>" : $"{printedClass}"; |
|
|
|
|
var dict = $@"global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, {valueType}>"; |
|
|
|
|
|
|
|
|
|
var generateNativeToManaged = Options.GenerateNativeToManagedFor(@class); |
|
|
|
|
if (generateNativeToManaged) |
|
|
|
|
WriteLine("internal static readonly {0} NativeToManagedMap = new {0}();", dict); |
|
|
|
|
PopBlock(NewLineKind.BeforeNextBlock); |
|
|
|
|
|
|
|
|
@ -446,6 +448,8 @@ namespace CppSharp.Generators.CSharp
@@ -446,6 +448,8 @@ namespace CppSharp.Generators.CSharp
|
|
|
|
|
// will never get invoked: unless the managed object is Disposed, there will always be a reference
|
|
|
|
|
// in the dictionary.
|
|
|
|
|
PushBlock(BlockKind.Method); |
|
|
|
|
if (generateNativeToManaged) |
|
|
|
|
{ |
|
|
|
|
if (Options.GenerateFinalizerFor(@class)) |
|
|
|
|
{ |
|
|
|
|
WriteLines($@"
|
|
|
|
@ -474,6 +478,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
@@ -474,6 +478,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
|
|
|
|
|
}}");
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
PopBlock(NewLineKind.BeforeNextBlock); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -2270,6 +2275,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
@@ -2270,6 +2275,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
|
|
|
|
|
WriteLineIndent("return;"); |
|
|
|
|
|
|
|
|
|
// The local var must be of the exact type in the object map because of TryRemove
|
|
|
|
|
if (Options.GenerateNativeToManagedFor(@class)) |
|
|
|
|
WriteLine("NativeToManagedMap.TryRemove({0}, out _);", Helpers.InstanceIdentifier); |
|
|
|
|
var realClass = @class.IsTemplate ? @class.Specializations[0] : @class; |
|
|
|
|
var classInternal = TypePrinter.PrintNative(realClass); |
|
|
|
@ -2402,6 +2408,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
@@ -2402,6 +2408,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
|
|
|
|
|
{ |
|
|
|
|
var @new = @class.HasBase && @class.HasRefBase(); |
|
|
|
|
|
|
|
|
|
bool generateNativeToManaged = Options.GenerateNativeToManagedFor(@class); |
|
|
|
|
if (generateNativeToManaged) |
|
|
|
|
{ |
|
|
|
|
WriteLines($@"
|
|
|
|
|
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInstance({TypePrinter.IntPtrType} native, bool saveInstance = false, bool skipVTables = false) |
|
|
|
|
{{ |
|
|
|
@ -2415,6 +2424,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInst
@@ -2415,6 +2424,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInst
|
|
|
|
|
return result; |
|
|
|
|
}}");
|
|
|
|
|
NewLine(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (HasVirtualTables(@class)) |
|
|
|
|
{ |
|
|
|
@ -2422,9 +2432,16 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInst
@@ -2422,9 +2432,16 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInst
|
|
|
|
|
|
|
|
|
|
WriteLines($@"
|
|
|
|
|
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({TypePrinter.IntPtrType} native) |
|
|
|
|
{{ |
|
|
|
|
{{");
|
|
|
|
|
|
|
|
|
|
if (generateNativeToManaged) |
|
|
|
|
{ |
|
|
|
|
WriteLines($@"
|
|
|
|
|
if (!{Helpers.TryGetNativeToManagedMappingIdentifier}(native, out var managed)) |
|
|
|
|
throw new global::System.Exception(""No managed instance was found""); |
|
|
|
|
throw new global::System.Exception(""No managed instance was found"");");
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
WriteLines($@"
|
|
|
|
|
var result = ({printedClass})managed; |
|
|
|
|
if (result.{Helpers.OwnsNativeInstanceIdentifier}) |
|
|
|
|
result.SetupVTables(); |
|
|
|
@ -2540,6 +2557,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
@@ -2540,6 +2557,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
|
|
|
|
|
if (@class.IsRefType) |
|
|
|
|
{ |
|
|
|
|
WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;"); |
|
|
|
|
if (Options.GenerateNativeToManagedFor(@class)) |
|
|
|
|
WriteLine($"{Helpers.RecordNativeToManagedMappingIdentifier}({Helpers.InstanceIdentifier}, this);"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
@ -2985,6 +3003,8 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
@@ -2985,6 +3003,8 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
|
|
|
|
|
@class.IsAbstractImpl ? @class.BaseClass : @class); |
|
|
|
|
WriteLine($"{Helpers.InstanceIdentifier} = Marshal.AllocHGlobal(sizeof({@internal}));"); |
|
|
|
|
WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;"); |
|
|
|
|
|
|
|
|
|
if (Options.GenerateNativeToManagedFor(@class)) |
|
|
|
|
WriteLine($"{Helpers.RecordNativeToManagedMappingIdentifier}({Helpers.InstanceIdentifier}, this);"); |
|
|
|
|
|
|
|
|
|
if (method.IsCopyConstructor) |
|
|
|
|