Object.base.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Godot
  4. {
  5. public partial class Object : IDisposable
  6. {
  7. private bool disposed = false;
  8. private const string nativeName = "Object";
  9. internal IntPtr ptr;
  10. internal bool memoryOwn;
  11. public Object() : this(false)
  12. {
  13. if (ptr == IntPtr.Zero)
  14. ptr = godot_icall_Object_Ctor(this);
  15. }
  16. internal Object(bool memoryOwn)
  17. {
  18. this.memoryOwn = memoryOwn;
  19. }
  20. public IntPtr NativeInstance
  21. {
  22. get { return ptr; }
  23. }
  24. internal static IntPtr GetPtr(Object instance)
  25. {
  26. if (instance == null)
  27. return IntPtr.Zero;
  28. if (instance.disposed)
  29. throw new ObjectDisposedException(instance.GetType().FullName);
  30. return instance.ptr;
  31. }
  32. ~Object()
  33. {
  34. Dispose(false);
  35. }
  36. public void Dispose()
  37. {
  38. Dispose(true);
  39. GC.SuppressFinalize(this);
  40. }
  41. protected virtual void Dispose(bool disposing)
  42. {
  43. if (disposed)
  44. return;
  45. if (ptr != IntPtr.Zero)
  46. {
  47. if (memoryOwn)
  48. {
  49. memoryOwn = false;
  50. godot_icall_Reference_Disposed(this, ptr, !disposing);
  51. }
  52. else
  53. {
  54. godot_icall_Object_Disposed(this, ptr);
  55. }
  56. this.ptr = IntPtr.Zero;
  57. }
  58. disposed = true;
  59. }
  60. /// <summary>
  61. /// Returns a new <see cref="Godot.SignalAwaiter"/> awaiter configured to complete when the instance
  62. /// <paramref name="source"/> emits the signal specified by the <paramref name="signal"/> parameter.
  63. /// </summary>
  64. /// <param name="source">
  65. /// The instance the awaiter will be listening to.
  66. /// </param>
  67. /// <param name="signal">
  68. /// The signal the awaiter will be waiting for.
  69. /// </param>
  70. /// <example>
  71. /// This sample prints a message once every frame up to 100 times.
  72. /// <code>
  73. /// public override void _Ready()
  74. /// {
  75. /// for (int i = 0; i < 100; i++)
  76. /// {
  77. /// await ToSignal(GetTree(), "idle_frame");
  78. /// GD.Print($"Frame {i}");
  79. /// }
  80. /// }
  81. /// </code>
  82. /// </example>
  83. public SignalAwaiter ToSignal(Object source, string signal)
  84. {
  85. return new SignalAwaiter(source, signal, this);
  86. }
  87. /// <summary>
  88. /// Gets a new <see cref="Godot.DynamicGodotObject"/> associated with this instance.
  89. /// </summary>
  90. public dynamic DynamicObject => new DynamicGodotObject(this);
  91. [MethodImpl(MethodImplOptions.InternalCall)]
  92. internal extern static IntPtr godot_icall_Object_Ctor(Object obj);
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. internal extern static void godot_icall_Object_Disposed(Object obj, IntPtr ptr);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. internal extern static void godot_icall_Reference_Disposed(Object obj, IntPtr ptr, bool isFinalizer);
  97. // Used by the generated API
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. internal extern static IntPtr godot_icall_Object_ClassDB_get_method(string type, string method);
  100. }
  101. }