Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FunctionPointer.cs 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #if !UNITY_DOTSPLAYER
  30. [NativeDisableUnsafePtrRestriction]
  31. #endif
  32. private readonly IntPtr _ptr;
  33. /// <summary>
  34. /// Creates a new instance of this function pointer with the following native pointer.
  35. /// </summary>
  36. /// <param name="ptr"></param>
  37. public FunctionPointer(IntPtr ptr)
  38. {
  39. _ptr = ptr;
  40. }
  41. /// <summary>
  42. /// Gets the underlying pointer.
  43. /// </summary>
  44. public IntPtr Value => _ptr;
  45. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  46. private void CheckIsCreated()
  47. {
  48. if (!IsCreated)
  49. {
  50. throw new NullReferenceException("Object reference not set to an instance of an object");
  51. }
  52. }
  53. /// <summary>
  54. /// Gets the delegate associated to this function pointer in order to call the function pointer.
  55. /// This delegate can be called from a Burst Job or from regular C#.
  56. /// If calling from regular C#, it is recommended to cache the returned delegate of this property
  57. /// instead of using this property every time you need to call the delegate.
  58. /// </summary>
  59. public T Invoke
  60. {
  61. get
  62. {
  63. CheckIsCreated();
  64. return Marshal.GetDelegateForFunctionPointer<T>(_ptr);
  65. }
  66. }
  67. /// <summary>
  68. /// Whether the function pointer is valid.
  69. /// </summary>
  70. public bool IsCreated => _ptr != IntPtr.Zero;
  71. /// <summary>
  72. /// Converts a pointer to a function pointer.
  73. /// </summary>
  74. /// <param name="ptr">The native pointer.</param>
  75. /// <returns>An instance of this interface.</returns>
  76. IFunctionPointer IFunctionPointer.FromIntPtr(IntPtr ptr) => new FunctionPointer<T>(ptr);
  77. }
  78. }