Skip to content

Commit 6e897b9

Browse files
committed
Made GetOrCreateInstance use the singletonInstance
1 parent 3c89304 commit 6e897b9

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,23 +2453,49 @@ private void GenerateNativeConstructor(Class @class)
24532453

24542454
if (@class.IsRefType)
24552455
{
2456+
if(@class.IsSingleton)
2457+
{
2458+
WriteLine($"private static {printedClass} singletonInstance;");
2459+
}
2460+
24562461
var @new = @class.HasBase && @class.HasRefBase();
24572462

24582463
bool generateNativeToManaged = Options.GenerateNativeToManagedFor(@class);
24592464
if (generateNativeToManaged)
24602465
{
2461-
WriteLines($@"
2466+
if (@class.IsSingleton)
2467+
{
2468+
WriteLines($@"
24622469
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInstance({TypePrinter.IntPtrType} native, bool saveInstance = false, bool skipVTables = false)
24632470
{{
24642471
if (native == {TypePrinter.IntPtrType}.Zero)
24652472
return null;
2473+
if (singletonInstance != null)
2474+
return singletonInstance;
24662475
if ({Helpers.TryGetNativeToManagedMappingIdentifier}(native, out var managed))
24672476
return ({printedClass})managed;
24682477
var result = {Helpers.CreateInstanceIdentifier}(native, skipVTables);
2478+
singletonInstance = result;
24692479
if (saveInstance)
24702480
{Helpers.RecordNativeToManagedMappingIdentifier}(native, result);
24712481
return result;
24722482
}}");
2483+
}
2484+
else
2485+
{
2486+
WriteLines($@"
2487+
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInstance({TypePrinter.IntPtrType} native, bool saveInstance = false, bool skipVTables = false)
2488+
{{
2489+
if (native == {TypePrinter.IntPtrType}.Zero)
2490+
return null;
2491+
if ({Helpers.TryGetNativeToManagedMappingIdentifier}(native, out var managed))
2492+
return ({printedClass})managed;
2493+
var result = {Helpers.CreateInstanceIdentifier}(native, skipVTables);
2494+
if (saveInstance)
2495+
{Helpers.RecordNativeToManagedMappingIdentifier}(native, result);
2496+
return result;
2497+
}}");
2498+
}
24732499
NewLine();
24742500
}
24752501

@@ -2481,7 +2507,6 @@ private void GenerateNativeConstructor(Class @class)
24812507
if (@class.IsSingleton)
24822508
{
24832509
WriteLines($@"
2484-
private static {printedClass} singletonInstance;
24852510
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({TypePrinter.IntPtrType} native)
24862511
{{
24872512
if (singletonInstance != null)

0 commit comments

Comments
 (0)