namespace Brutal.Pointers;
///
/// Wrapper for a pointer to a pointer of type TValueType.
///
public readonly unsafe struct PtrPtr(TValueType** address) : INullable
where TValueType : unmanaged
{
///
/// The pointer-pointer.
///
public TValueType** Pointer => address;
///
/// Convert the pointer to a span of pointers of type TValueType.
///
public Span> AsSpan(ElementCount length)
{
// Ptr is equivalent to TValueType*,
// So we can cast TValueType** to Ptr*,
// Or so we will hope
return new Span>((Ptr*)Pointer, length);
}
///
public bool IsNull()
{
return Pointer == null;
}
///
/// Get the value of the pointer as a reference.
///
public ref TValueType* Value => ref *Pointer;
///
/// Access the pointer at the given index.
///
public Ptr this[int index]
{
get => Pointer[index];
set => Pointer[index] = value.Pointer;
}
///
/// Implicit conversion from void** to PtrPtr.
///
public static implicit operator PtrPtr(TValueType** address)
{
return new PtrPtr(address);
}
///
/// Implicit conversion from PtrPtr to void**.
///
public static implicit operator void**(PtrPtr ptr)
{
return (void**)ptr.Pointer;
}
///
/// Implicit conversion from PtrPtr to TValueType**
///
public static implicit operator TValueType**(PtrPtr ptr)
{
return ptr.Pointer;
}
///
/// Explicit conversion from nint to PtrPtr.
///
public static explicit operator PtrPtr(nint ptr)
{
return new PtrPtr((TValueType**)ptr);
}
///
/// Explicit cast to nint.
///
public static explicit operator nint(PtrPtr ptr)
{
return (nint)ptr.Pointer;
}
}