SessionToken.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.App;
  3. /// <summary>Defines a session token for a running <see cref="Toplevel"/>.</summary>
  4. public class SessionToken : IDisposable
  5. {
  6. /// <summary>Initializes a new <see cref="SessionToken"/> class.</summary>
  7. /// <param name="view"></param>
  8. public SessionToken (Toplevel view) { Toplevel = view; }
  9. /// <summary>The <see cref="Toplevel"/> belonging to this <see cref="SessionToken"/>.</summary>
  10. public Toplevel Toplevel { get; internal set; }
  11. /// <summary>Releases all resource used by the <see cref="SessionToken"/> object.</summary>
  12. /// <remarks>Call <see cref="Dispose()"/> when you are finished using the <see cref="SessionToken"/>.</remarks>
  13. /// <remarks>
  14. /// <see cref="Dispose()"/> method leaves the <see cref="SessionToken"/> in an unusable state. After calling
  15. /// <see cref="Dispose()"/>, you must release all references to the <see cref="SessionToken"/> so the garbage collector can
  16. /// reclaim the memory that the <see cref="SessionToken"/> was occupying.
  17. /// </remarks>
  18. public void Dispose ()
  19. {
  20. Dispose (true);
  21. GC.SuppressFinalize (this);
  22. #if DEBUG_IDISPOSABLE
  23. WasDisposed = true;
  24. #endif
  25. }
  26. /// <summary>Releases all resource used by the <see cref="SessionToken"/> object.</summary>
  27. /// <param name="disposing">If set to <see langword="true"/> we are disposing and should dispose held objects.</param>
  28. protected virtual void Dispose (bool disposing)
  29. {
  30. if (Toplevel is { } && disposing)
  31. {
  32. // Previously we were requiring Toplevel be disposed here.
  33. // But that is not correct becaue `Begin` didn't create the TopLevel, `Init` did; thus
  34. // disposing should be done by `Shutdown`, not `End`.
  35. throw new InvalidOperationException (
  36. "Toplevel must be null before calling Application.SessionToken.Dispose"
  37. );
  38. }
  39. }
  40. #if DEBUG_IDISPOSABLE
  41. #pragma warning disable CS0419 // Ambiguous reference in cref attribute
  42. /// <summary>
  43. /// Gets whether <see cref="SessionToken.Dispose"/> was called on this SessionToken or not.
  44. /// For debug purposes to verify objects are being disposed properly.
  45. /// Only valid when DEBUG_IDISPOSABLE is defined.
  46. /// </summary>
  47. public bool WasDisposed { get; private set; }
  48. /// <summary>
  49. /// Gets the number of times <see cref="SessionToken.Dispose"/> was called on this object.
  50. /// For debug purposes to verify objects are being disposed properly.
  51. /// Only valid when DEBUG_IDISPOSABLE is defined.
  52. /// </summary>
  53. public int DisposedCount { get; private set; } = 0;
  54. /// <summary>
  55. /// Gets the list of SessionToken objects that have been created and not yet disposed.
  56. /// Note, this is a static property and will affect all SessionToken objects.
  57. /// For debug purposes to verify objects are being disposed properly.
  58. /// Only valid when DEBUG_IDISPOSABLE is defined.
  59. /// </summary>
  60. public static ConcurrentBag<SessionToken> Instances { get; private set; } = [];
  61. /// <summary>Creates a new SessionToken object.</summary>
  62. public SessionToken ()
  63. {
  64. Instances.Add (this);
  65. }
  66. #pragma warning restore CS0419 // Ambiguous reference in cref attribute
  67. #endif
  68. }