using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Ryujinx.Common.Memory
{
///
/// Represents a pointer to an unmanaged resource.
///
/// Type of the unmanaged resource
public unsafe struct Ptr : IEquatable> where T : unmanaged
{
private IntPtr _ptr;
///
/// Null pointer.
///
public static Ptr Null => new() { _ptr = IntPtr.Zero };
///
/// True if the pointer is null, false otherwise.
///
public readonly bool IsNull => _ptr == IntPtr.Zero;
///
/// Gets a reference to the value.
///
public readonly ref T Value => ref Unsafe.AsRef((void*)_ptr);
///
/// Creates a new pointer to an unmanaged resource.
///
///
/// For data on the heap, proper pinning is necessary during
/// use. Failure to do so will result in memory corruption and crashes.
///
/// Reference to the unmanaged resource
public Ptr(ref T value)
{
_ptr = (IntPtr)Unsafe.AsPointer(ref value);
}
public readonly override bool Equals(object obj)
{
return obj is Ptr other && Equals(other);
}
public readonly bool Equals([AllowNull] Ptr other)
{
return _ptr == other._ptr;
}
public readonly override int GetHashCode()
{
return _ptr.GetHashCode();
}
public static bool operator ==(Ptr left, Ptr right)
{
return left.Equals(right);
}
public static bool operator !=(Ptr left, Ptr right)
{
return !(left == right);
}
}
}