Object.base.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 static StringName nativeName = "Object";
  9. internal IntPtr ptr;
  10. internal bool memoryOwn;
  11. public Object() : this(false)
  12. {
  13. if (ptr == IntPtr.Zero)
  14. {
  15. ptr = godot_icall_Object_Ctor(this);
  16. }
  17. else
  18. {
  19. // This is called inside godot_icall_Object_Ctor, so we must call it as well in this case.
  20. godot_icall_Object_ConnectEventSignals(ptr);
  21. }
  22. }
  23. internal Object(bool memoryOwn)
  24. {
  25. this.memoryOwn = memoryOwn;
  26. }
  27. public IntPtr NativeInstance
  28. {
  29. get { return ptr; }
  30. }
  31. internal static IntPtr GetPtr(Object instance)
  32. {
  33. if (instance == null)
  34. return IntPtr.Zero;
  35. if (instance.disposed)
  36. throw new ObjectDisposedException(instance.GetType().FullName);
  37. return instance.ptr;
  38. }
  39. ~Object()
  40. {
  41. Dispose(false);
  42. }
  43. public void Dispose()
  44. {
  45. Dispose(true);
  46. GC.SuppressFinalize(this);
  47. }
  48. protected virtual void Dispose(bool disposing)
  49. {
  50. if (disposed)
  51. return;
  52. if (ptr != IntPtr.Zero)
  53. {
  54. if (memoryOwn)
  55. {
  56. memoryOwn = false;
  57. godot_icall_Reference_Disposed(this, ptr, !disposing);
  58. }
  59. else
  60. {
  61. godot_icall_Object_Disposed(this, ptr);
  62. }
  63. this.ptr = IntPtr.Zero;
  64. }
  65. disposed = true;
  66. }
  67. public override string ToString()
  68. {
  69. return godot_icall_Object_ToString(GetPtr(this));
  70. }
  71. /// <summary>
  72. /// Returns a new <see cref="Godot.SignalAwaiter"/> awaiter configured to complete when the instance
  73. /// <paramref name="source"/> emits the signal specified by the <paramref name="signal"/> parameter.
  74. /// </summary>
  75. /// <param name="source">
  76. /// The instance the awaiter will be listening to.
  77. /// </param>
  78. /// <param name="signal">
  79. /// The signal the awaiter will be waiting for.
  80. /// </param>
  81. /// <example>
  82. /// This sample prints a message once every frame up to 100 times.
  83. /// <code>
  84. /// public override void _Ready()
  85. /// {
  86. /// for (int i = 0; i &lt; 100; i++)
  87. /// {
  88. /// await ToSignal(GetTree(), "idle_frame");
  89. /// GD.Print($"Frame {i}");
  90. /// }
  91. /// }
  92. /// </code>
  93. /// </example>
  94. public SignalAwaiter ToSignal(Object source, StringName signal)
  95. {
  96. return new SignalAwaiter(source, signal, this);
  97. }
  98. /// <summary>
  99. /// Gets a new <see cref="Godot.DynamicGodotObject"/> associated with this instance.
  100. /// </summary>
  101. public dynamic DynamicObject => new DynamicGodotObject(this);
  102. internal static IntPtr __ClassDB_get_method(StringName type, string method)
  103. {
  104. return godot_icall_Object_ClassDB_get_method(StringName.GetPtr(type), method);
  105. }
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. internal static extern IntPtr godot_icall_Object_Ctor(Object obj);
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. internal static extern void godot_icall_Object_Disposed(Object obj, IntPtr ptr);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. internal static extern void godot_icall_Reference_Disposed(Object obj, IntPtr ptr, bool isFinalizer);
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. internal static extern void godot_icall_Object_ConnectEventSignals(IntPtr obj);
  114. [MethodImpl(MethodImplOptions.InternalCall)]
  115. internal static extern string godot_icall_Object_ToString(IntPtr ptr);
  116. // Used by the generated API
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. internal static extern IntPtr godot_icall_Object_ClassDB_get_method(IntPtr type, string method);
  119. }
  120. }