using System.Runtime.CompilerServices; namespace Misaki.HighPerformance.LowLevel; /// /// Represents a strongly-typed, read-only pointer to an unmanaged value of type . /// /// /// When a pointer is wrapped in this struct, it indicates that the code does not intend to manage the lifetime of the data being pointed to. /// /// The unmanaged type to which the pointer refers. public readonly unsafe struct SharedPtr : IEquatable> where T : unmanaged { private readonly T* _value; public SharedPtr(T* value) { _value = value; } public T* Get() { return _value; } public bool Equals(SharedPtr other) { return _value == other._value; } public override bool Equals(object? obj) { return obj is SharedPtr ptr && Equals(ptr); } public override int GetHashCode() { return ((nint)_value).GetHashCode(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator T*(SharedPtr ptr) { return ptr._value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator SharedPtr(T* value) { return new SharedPtr(value); } public static bool operator ==(SharedPtr left, SharedPtr right) { return left.Equals(right); } public static bool operator !=(SharedPtr left, SharedPtr right) { return !(left == right); } } /// /// Provides exclusive ownership and management of an unmanaged pointer to a value of type . /// Ensures that the pointer is not shared and can be safely transferred or detached. /// /// /// is designed to encapsulate a raw pointer, enforcing unique ownership semantics similar to C++'s std::unique_ptr. /// /// The unmanaged type of the value to which the pointer refers. [NonCopyable] public unsafe struct UniquePtr : IEquatable> where T : unmanaged { private T* _value; public UniquePtr(T* value) { _value = value; } public readonly T* Get() { return _value; } public readonly SharedPtr Share() { return new SharedPtr(_value); } public T* Detach() { var temp = _value; _value = null; return temp; } public readonly bool Equals(UniquePtr other) { return _value == other._value; } public override readonly bool Equals(object? obj) { return obj is SharedPtr ptr && Equals(ptr); } public override readonly int GetHashCode() { return ((nint)_value).GetHashCode(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator T*(UniquePtr ptr) { return ptr._value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator UniquePtr(T* value) { return new UniquePtr(value); } public static bool operator ==(UniquePtr left, UniquePtr right) { return left.Equals(right); } public static bool operator !=(UniquePtr left, UniquePtr right) { return !(left == right); } } public ref struct Ref : IEquatable> { private ref T _value; public Ref(ref T value) { _value = ref value; } public ref T Get() { return ref _value; } public bool Equals(Ref other) { return Unsafe.AreSame(ref _value, ref other._value); } [Obsolete("Equals() on Ref will always throw an exception. Use the equality operator instead.")] #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member public override bool Equals(object? obj) { throw new NotSupportedException(); } [Obsolete("GetHashCode() on Ref will always throw an exception.")] public override int GetHashCode() #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member { throw new NotSupportedException(); } public static bool operator ==(Ref left, Ref right) { return left.Equals(right); } public static bool operator !=(Ref left, Ref right) { return !(left == right); } } /// /// Provides a wrapper for a value type that implements , ensuring proper disposal of the contained value. /// /// The class manages the lifetime of the contained value by calling its /// method when the wrapper is disposed or finalized. After disposal, accessing the value /// will throw an . /// The value type to wrap. Must be a struct that implements . public class Owner : IDisposable where T : struct, IDisposable { private T _value; private bool _disposed; public Owner(T value) { _value = value; } ~Owner() { Dispose(); } public ref T Get() { ObjectDisposedException.ThrowIf(_disposed, this); return ref _value; } public void Dispose() { if (_disposed) { return; } _value.Dispose(); _disposed = true; GC.SuppressFinalize(this); } }