File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Generator/Generators/CSharp Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -83,6 +83,10 @@ public class Class : DeclarationContext
83
83
// True if the record is a POD (Plain Old Data) type.
84
84
public bool IsPOD ;
85
85
86
+ // True if the class is a singleton and should be accessed as such
87
+ // from the generated GetInstance method.
88
+ public bool IsSingleton ;
89
+
86
90
// Semantic type of the class.
87
91
public ClassType Type ;
88
92
@@ -140,6 +144,7 @@ public Class()
140
144
IsUnion = false ;
141
145
IsFinal = false ;
142
146
IsPOD = false ;
147
+ IsSingleton = false ;
143
148
Type = ClassType . RefType ;
144
149
Layout = new ClassLayout ( ) ;
145
150
templateParameters = new List < Declaration > ( ) ;
Original file line number Diff line number Diff line change @@ -2478,16 +2478,37 @@ private void GenerateNativeConstructor(Class @class)
2478
2478
{
2479
2479
@new = @class . HasBase && HasVirtualTables ( @class . Bases . First ( ) . Class ) ;
2480
2480
2481
- WriteLines ( $@ "
2481
+ if ( @class . IsSingleton )
2482
+ {
2483
+ WriteLines ( $@ "
2484
+ private static { printedClass } singletonInstance;
2482
2485
internal static{ ( @new ? " new" : string . Empty ) } { printedClass } __GetInstance({ TypePrinter . IntPtrType } native)
2483
2486
{{
2487
+ if (singletonInstance != null)
2488
+ return singletonInstance
2484
2489
if (!{ Helpers . TryGetNativeToManagedMappingIdentifier } (native, out var managed))
2485
2490
throw new global::System.Exception(""No managed instance was found"");
2486
2491
var result = ({ printedClass } )managed;
2487
2492
if (result.{ Helpers . OwnsNativeInstanceIdentifier } )
2488
2493
result.SetupVTables();
2494
+ singletonInstance = result;
2489
2495
return result;
2490
2496
}}" ) ;
2497
+ }
2498
+ // Not a singleton.
2499
+ else
2500
+ {
2501
+ WriteLines ( $@ "
2502
+ internal static{ ( @new ? " new" : string . Empty ) } { printedClass } __GetInstance({ TypePrinter . IntPtrType } native)
2503
+ {{
2504
+ if (!{ Helpers . TryGetNativeToManagedMappingIdentifier } (native, out var managed))
2505
+ throw new global::System.Exception(""No managed instance was found"");
2506
+ var result = ({ printedClass } )managed;
2507
+ if (result.{ Helpers . OwnsNativeInstanceIdentifier } )
2508
+ result.SetupVTables();
2509
+ return result;
2510
+ }}" ) ;
2511
+ }
2491
2512
NewLine ( ) ;
2492
2513
}
2493
2514
}
You can’t perform that action at this time.
0 commit comments