Object.base.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. return instance == null ? IntPtr.Zero : instance.ptr;
  27. }
  28. ~Object()
  29. {
  30. Dispose(false);
  31. }
  32. public void Dispose()
  33. {
  34. Dispose(true);
  35. GC.SuppressFinalize(this);
  36. }
  37. protected virtual void Dispose(bool disposing)
  38. {
  39. if (disposed)
  40. return;
  41. if (ptr != IntPtr.Zero)
  42. {
  43. if (memoryOwn)
  44. {
  45. memoryOwn = false;
  46. godot_icall_Object_Dtor(this, ptr);
  47. }
  48. this.ptr = IntPtr.Zero;
  49. }
  50. disposed = true;
  51. }
  52. public SignalAwaiter ToSignal(Object source, string signal)
  53. {
  54. return new SignalAwaiter(source, signal, this);
  55. }
  56. [MethodImpl(MethodImplOptions.InternalCall)]
  57. internal extern static IntPtr godot_icall_Object_Ctor(Object obj);
  58. [MethodImpl(MethodImplOptions.InternalCall)]
  59. internal extern static void godot_icall_Object_Dtor(object obj, IntPtr ptr);
  60. // Used by the generated API
  61. [MethodImpl(MethodImplOptions.InternalCall)]
  62. internal extern static IntPtr godot_icall_Object_ClassDB_get_method(string type, string method);
  63. }
  64. }