Object.base.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. public override string ToString()
  61. {
  62. return godot_icall_Object_ToString(GetPtr(this));
  63. }
  64. /// <summary>
  65. /// Returns a new <see cref="Godot.SignalAwaiter"/> awaiter configured to complete when the instance
  66. /// <paramref name="source"/> emits the signal specified by the <paramref name="signal"/> parameter.
  67. /// </summary>
  68. /// <param name="source">
  69. /// The instance the awaiter will be listening to.
  70. /// </param>
  71. /// <param name="signal">
  72. /// The signal the awaiter will be waiting for.
  73. /// </param>
  74. /// <example>
  75. /// This sample prints a message once every frame up to 100 times.
  76. /// <code>
  77. /// public override void _Ready()
  78. /// {
  79. /// for (int i = 0; i &lt; 100; i++)
  80. /// {
  81. /// await ToSignal(GetTree(), "idle_frame");
  82. /// GD.Print($"Frame {i}");
  83. /// }
  84. /// }
  85. /// </code>
  86. /// </example>
  87. public SignalAwaiter ToSignal(Object source, string signal)
  88. {
  89. return new SignalAwaiter(source, signal, this);
  90. }
  91. /// <summary>
  92. /// Gets a new <see cref="Godot.DynamicGodotObject"/> associated with this instance.
  93. /// </summary>
  94. public dynamic DynamicObject => new DynamicGodotObject(this);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. internal extern static IntPtr godot_icall_Object_Ctor(Object obj);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. internal extern static void godot_icall_Object_Disposed(Object obj, IntPtr ptr);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. internal extern static void godot_icall_Reference_Disposed(Object obj, IntPtr ptr, bool isFinalizer);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. internal extern static string godot_icall_Object_ToString(IntPtr ptr);
  103. // Used by the generated API
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. internal extern static IntPtr godot_icall_Object_ClassDB_get_method(string type, string method);
  106. }
  107. }