설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FunctionPointer.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. namespace Unity.Burst
  6. {
  7. /// <summary>
  8. /// Base interface for a function pointer.
  9. /// </summary>
  10. public interface IFunctionPointer
  11. {
  12. /// <summary>
  13. /// Converts a pointer to a function pointer.
  14. /// </summary>
  15. /// <param name="ptr">The native pointer.</param>
  16. /// <returns>An instance of this interface.</returns>
  17. [Obsolete("This method will be removed in a future version of Burst")]
  18. IFunctionPointer FromIntPtr(IntPtr ptr);
  19. }
  20. /// <summary>
  21. /// A function pointer that can be used from a Burst Job or from regular C#.
  22. /// It needs to be compiled through <see cref="BurstCompiler.CompileFunctionPointer{T}"/>
  23. /// </summary>
  24. /// <typeparam name="T">Type of the delegate of this function pointer</typeparam>
  25. public readonly struct FunctionPointer<T> : IFunctionPointer
  26. {
  27. // DOTSPLAYER's shim package relies on Burst for it's jobs code
  28. // so Burst does not see the DOTSPLAYER definition of this attribute
  29. [NativeDisableUnsafePtrRestriction]
  30. private readonly IntPtr _ptr;
  31. /// <summary>
  32. /// Creates a new instance of this function pointer with the following native pointer.
  33. /// </summary>
  34. /// <param name="ptr"></param>
  35. public FunctionPointer(IntPtr ptr)
  36. {
  37. _ptr = ptr;
  38. }
  39. /// <summary>
  40. /// Gets the underlying pointer.
  41. /// </summary>
  42. public IntPtr Value => _ptr;
  43. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  44. private void CheckIsCreated()
  45. {
  46. if (!IsCreated)
  47. {
  48. throw new NullReferenceException("Object reference not set to an instance of an object");
  49. }
  50. }
  51. /// <summary>
  52. /// Gets the delegate associated to this function pointer in order to call the function pointer.
  53. /// This delegate can be called from a Burst Job or from regular C#.
  54. /// If calling from regular C#, it is recommended to cache the returned delegate of this property
  55. /// instead of using this property every time you need to call the delegate.
  56. /// </summary>
  57. public T Invoke
  58. {
  59. get
  60. {
  61. CheckIsCreated();
  62. return Marshal.GetDelegateForFunctionPointer<T>(_ptr);
  63. }
  64. }
  65. /// <summary>
  66. /// Whether the function pointer is valid.
  67. /// </summary>
  68. public bool IsCreated => _ptr != IntPtr.Zero;
  69. /// <summary>
  70. /// Converts a pointer to a function pointer.
  71. /// </summary>
  72. /// <param name="ptr">The native pointer.</param>
  73. /// <returns>An instance of this interface.</returns>
  74. IFunctionPointer IFunctionPointer.FromIntPtr(IntPtr ptr) => new FunctionPointer<T>(ptr);
  75. }
  76. }