RunState.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.App;
  3. /// <summary>The execution state for a <see cref="Toplevel"/> view.</summary>
  4. public class RunState : IDisposable
  5. {
  6. /// <summary>Initializes a new <see cref="RunState"/> class.</summary>
  7. /// <param name="view"></param>
  8. public RunState (Toplevel view) { Toplevel = view; }
  9. /// <summary>The <see cref="Toplevel"/> belonging to this <see cref="RunState"/>.</summary>
  10. public Toplevel Toplevel { get; internal set; }
  11. /// <summary>Releases all resource used by the <see cref="RunState"/> object.</summary>
  12. /// <remarks>Call <see cref="Dispose()"/> when you are finished using the <see cref="RunState"/>.</remarks>
  13. /// <remarks>
  14. /// <see cref="Dispose()"/> method leaves the <see cref="RunState"/> in an unusable state. After calling
  15. /// <see cref="Dispose()"/>, you must release all references to the <see cref="RunState"/> so the garbage collector can
  16. /// reclaim the memory that the <see cref="RunState"/> 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="RunState"/> 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.RunState.Dispose"
  37. );
  38. }
  39. }
  40. #if DEBUG_IDISPOSABLE
  41. /// <summary>
  42. /// Gets whether <see cref="Dispose"/> was called on this RunState or not.
  43. /// For debug purposes to verify objects are being disposed properly.
  44. /// Only valid when DEBUG_IDISPOSABLE is defined.
  45. /// </summary>
  46. public bool WasDisposed { get; private set; }
  47. /// <summary>
  48. /// Gets the number of times <see cref="Dispose"/> was called on this object.
  49. /// For debug purposes to verify objects are being disposed properly.
  50. /// Only valid when DEBUG_IDISPOSABLE is defined.
  51. /// </summary>
  52. public int DisposedCount { get; private set; } = 0;
  53. /// <summary>
  54. /// Gets the list of RunState objects that have been created and not yet disposed.
  55. /// Note, this is a static property and will affect all RunState objects.
  56. /// For debug purposes to verify objects are being disposed properly.
  57. /// Only valid when DEBUG_IDISPOSABLE is defined.
  58. /// </summary>
  59. public static ConcurrentBag<RunState> Instances { get; private set; } = [];
  60. /// <summary>Creates a new RunState object.</summary>
  61. public RunState ()
  62. {
  63. Instances.Add (this);
  64. }
  65. #endif
  66. }