using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.LowLevel;
///
/// A structure that encapsulates a function pointer and provides methods to convert between
///
///
/// This structure used marshalling to convert between function pointers and delegates, which is not ideal for high-performance scenarios.
/// Use this only when necessary, and prefer using delegate* unmanaged for better performance.
///
/// The delegate type that the function pointer represents.
public readonly struct FunctionPointer
where T : Delegate
{
private readonly nint _ptr;
///
/// Gets the native function pointer associated with this function pointer instance.
///
public readonly nint Pointer => _ptr;
///
/// Gets the delegate instance associated with the specified function pointer.
///
/// This property uses to convert the function
/// pointer to a delegate. Ensure that the function pointer is valid and compatible with the delegate type
/// .
public T Delegate => Marshal.GetDelegateForFunctionPointer(_ptr);
///
/// Creates a new instance of this function pointer with the following native pointer.
///
///
public FunctionPointer(T func)
{
_ptr = Marshal.GetFunctionPointerForDelegate(func);
}
}