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
8383 // True if the record is a POD (Plain Old Data) type.
8484 public bool IsPOD ;
8585
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+
8690 // Semantic type of the class.
8791 public ClassType Type ;
8892
@@ -140,6 +144,7 @@ public Class()
140144 IsUnion = false ;
141145 IsFinal = false ;
142146 IsPOD = false ;
147+ IsSingleton = false ;
143148 Type = ClassType . RefType ;
144149 Layout = new ClassLayout ( ) ;
145150 templateParameters = new List < Declaration > ( ) ;
Original file line number Diff line number Diff line change @@ -2478,16 +2478,37 @@ private void GenerateNativeConstructor(Class @class)
24782478 {
24792479 @new = @class . HasBase && HasVirtualTables ( @class . Bases . First ( ) . Class ) ;
24802480
2481- WriteLines ( $@ "
2481+ if ( @class . IsSingleton )
2482+ {
2483+ WriteLines ( $@ "
2484+ private static { printedClass } singletonInstance;
24822485internal static{ ( @new ? " new" : string . Empty ) } { printedClass } __GetInstance({ TypePrinter . IntPtrType } native)
24832486{{
2487+ if (singletonInstance != null)
2488+ return singletonInstance
24842489 if (!{ Helpers . TryGetNativeToManagedMappingIdentifier } (native, out var managed))
24852490 throw new global::System.Exception(""No managed instance was found"");
24862491 var result = ({ printedClass } )managed;
24872492 if (result.{ Helpers . OwnsNativeInstanceIdentifier } )
24882493 result.SetupVTables();
2494+ singletonInstance = result;
24892495 return result;
24902496}}" ) ;
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+ }
24912512 NewLine ( ) ;
24922513 }
24932514 }
You can’t perform that action at this time.
0 commit comments