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() { _ptr = IntPtr.Zero };
///
/// True if the pointer is null, false otherwise.
///
public bool IsNull => _ptr == IntPtr.Zero;
///
/// Gets a reference to the value.
///
public 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 override bool Equals(object obj)
{
return obj is Ptr other && Equals(other);
}
public bool Equals([AllowNull] Ptr other)
{
return _ptr == other._ptr;
}
public 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);
}
}
}