RID.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Godot
  4. {
  5. /// <summary>
  6. /// The RID type is used to access the unique integer ID of a resource.
  7. /// They are opaque, which means they do not grant access to the associated
  8. /// resource by themselves. They are used by and with the low-level Server
  9. /// classes such as <see cref="RenderingServer"/>.
  10. /// </summary>
  11. public sealed partial class RID : IDisposable
  12. {
  13. private bool _disposed = false;
  14. internal IntPtr ptr;
  15. internal static IntPtr GetPtr(RID instance)
  16. {
  17. if (instance == null)
  18. throw new NullReferenceException($"The instance of type {nameof(RID)} is null.");
  19. if (instance._disposed)
  20. throw new ObjectDisposedException(instance.GetType().FullName);
  21. return instance.ptr;
  22. }
  23. ~RID()
  24. {
  25. Dispose(false);
  26. }
  27. /// <summary>
  28. /// Disposes of this <see cref="RID"/>.
  29. /// </summary>
  30. public void Dispose()
  31. {
  32. Dispose(true);
  33. GC.SuppressFinalize(this);
  34. }
  35. private void Dispose(bool disposing)
  36. {
  37. if (_disposed)
  38. return;
  39. if (ptr != IntPtr.Zero)
  40. {
  41. godot_icall_RID_Dtor(ptr);
  42. ptr = IntPtr.Zero;
  43. }
  44. _disposed = true;
  45. }
  46. internal RID(IntPtr ptr)
  47. {
  48. this.ptr = ptr;
  49. }
  50. /// <summary>
  51. /// The pointer to the native instance of this <see cref="RID"/>.
  52. /// </summary>
  53. public IntPtr NativeInstance
  54. {
  55. get { return ptr; }
  56. }
  57. internal RID()
  58. {
  59. this.ptr = IntPtr.Zero;
  60. }
  61. /// <summary>
  62. /// Constructs a new <see cref="RID"/> for the given <see cref="Object"/> <paramref name="from"/>.
  63. /// </summary>
  64. public RID(Object from)
  65. {
  66. this.ptr = godot_icall_RID_Ctor(Object.GetPtr(from));
  67. }
  68. /// <summary>
  69. /// Returns the ID of the referenced resource.
  70. /// </summary>
  71. /// <returns>The ID of the referenced resource.</returns>
  72. public int GetId()
  73. {
  74. return godot_icall_RID_get_id(GetPtr(this));
  75. }
  76. /// <summary>
  77. /// Converts this <see cref="RID"/> to a string.
  78. /// </summary>
  79. /// <returns>A string representation of this RID.</returns>
  80. public override string ToString() => "[RID]";
  81. [MethodImpl(MethodImplOptions.InternalCall)]
  82. internal static extern IntPtr godot_icall_RID_Ctor(IntPtr from);
  83. [MethodImpl(MethodImplOptions.InternalCall)]
  84. internal static extern void godot_icall_RID_Dtor(IntPtr ptr);
  85. [MethodImpl(MethodImplOptions.InternalCall)]
  86. internal static extern int godot_icall_RID_get_id(IntPtr ptr);
  87. }
  88. }