using System;
using System.Runtime.InteropServices;
namespace Susurrus.XAudio2
{
///
/// Calls the methods of a native object through its vtable, as XAudio2's voices aren't COM objects.
///
public static class Vtable
{
///
/// Returns the delegate for the method at in the vtable of the object at . The delegate takes the instance pointer as its first argument.
///
public static DelegateType Bind(IntPtr instance, int slot)
{
if (instance == IntPtr.Zero)
{
throw new ArgumentException("A null object has no vtable.", nameof(instance));
}
if (slot < 0)
{
throw new ArgumentOutOfRangeException(nameof(slot), slot, "A vtable slot can't be negative.");
}
IntPtr table = Marshal.ReadIntPtr(instance);
IntPtr method = Marshal.ReadIntPtr(table, slot * IntPtr.Size);
return Marshal.GetDelegateForFunctionPointer(method);
}
}
}