Skip to content

Commit

Permalink
Added KytGenerateOnDispose method to help with composition
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanL8 committed Feb 1, 2024
1 parent b54eaba commit b94c3d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/AST/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ public class Class : DeclarationContext

// True if the class is a singleton and should be accessed as such
// from the generated GetInstance method.
public bool IsSingleton;
public bool KytIsSingleton;

// True if the class should generate an action that is called from Dispose.
// This allows you to hook into the native destruction of an object from
// outside the object, which helps when using the composition pattern.
public bool KytGenerateOnDispose;

// Semantic type of the class.
public ClassType Type;
Expand Down Expand Up @@ -144,7 +149,8 @@ public Class()
IsUnion = false;
IsFinal = false;
IsPOD = false;
IsSingleton = false;
KytIsSingleton = false;
KytGenerateOnDispose = false;
Type = ClassType.RefType;
Layout = new ClassLayout();
templateParameters = new List<Declaration>();
Expand Down
18 changes: 14 additions & 4 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,13 @@ private void GenerateDisposeMethods(Class @class)
}
}

if (@class.KytGenerateOnDispose)
{
PushBlock(BlockKind.Field);
WriteLine("public Action<bool> OnDispose;");
PopBlock(NewLineKind.BeforeNextBlock);
}

// Declare partial method that the partial class can implement to participate
// in dispose.
PushBlock(BlockKind.Method);
Expand Down Expand Up @@ -2337,7 +2344,10 @@ private void GenerateDisposeMethods(Class @class)
//
// Delegate to partial method if implemented
WriteLine("DisposePartial(disposing);");

if (@class.KytGenerateOnDispose)
{
WriteLine("OnDispose?.Invoke(disposing);");
}
var dtor = @class.Destructors.FirstOrDefault();
if (dtor != null && dtor.Access != AccessSpecifier.Private &&
@class.HasNonTrivialDestructor && !@class.IsAbstract)
Expand Down Expand Up @@ -2455,7 +2465,7 @@ private void GenerateNativeConstructor(Class @class)

if (@class.IsRefType)
{
if(@class.IsSingleton)
if(@class.KytIsSingleton)
{
WriteLine($"private static {printedClass} singletonInstance;");
}
Expand All @@ -2465,7 +2475,7 @@ private void GenerateNativeConstructor(Class @class)
bool generateNativeToManaged = Options.GenerateNativeToManagedFor(@class);
if (generateNativeToManaged)
{
if (@class.IsSingleton)
if (@class.KytIsSingleton)
{
WriteLines($@"
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetOrCreateInstance({TypePrinter.IntPtrType} native, bool saveInstance = false, bool skipVTables = false)
Expand Down Expand Up @@ -2506,7 +2516,7 @@ private void GenerateNativeConstructor(Class @class)
{
@new = @class.HasBase && HasVirtualTables(@class.Bases.First().Class);

if (@class.IsSingleton)
if (@class.KytIsSingleton)
{
WriteLines($@"
internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({TypePrinter.IntPtrType} native)
Expand Down

0 comments on commit b94c3d5

Please sign in to comment.