mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-27 13:38:25 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
1 changed files with 768360 additions and 0 deletions
103
brutal.core/Brutal.Core/Pointers/Ptr.cs
Normal file
103
brutal.core/Brutal.Core/Pointers/Ptr.cs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
using Brutal.Attributes;
|
||||
|
||||
namespace Brutal.Pointers;
|
||||
|
||||
/// <summary>
|
||||
/// A wrapper for an untyped pointer.
|
||||
/// </summary>
|
||||
public readonly unsafe struct Ptr(void* pointer) : INullable, IEquatable<Ptr>
|
||||
{
|
||||
/// <summary>
|
||||
/// The raw void pointer.
|
||||
/// </summary>
|
||||
public void* Pointer => pointer;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new <see cref="Ptr"/>, offset by the given byte count.
|
||||
/// </summary>
|
||||
public Ptr Offset(ByteSize byteOffset)
|
||||
{
|
||||
return new Ptr((byte*)Pointer + byteOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the pointer to a Span of the given type and length.
|
||||
/// </summary>
|
||||
[return: Fixed]
|
||||
public Span<TValueType> AsSpan<TValueType>(ElementCount length)
|
||||
{
|
||||
return new Span<TValueType>(Pointer, length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Casts the pointer to a typed pointer.
|
||||
/// </summary>
|
||||
public Ptr<TCastType> Cast<TCastType>()
|
||||
where TCastType : unmanaged
|
||||
{
|
||||
return new Ptr<TCastType>((TCastType*)Pointer);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsNull()
|
||||
{
|
||||
return Pointer == null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from void* to <see cref="Ptr"/>
|
||||
/// </summary>
|
||||
public static implicit operator Ptr(void* pointer)
|
||||
{
|
||||
return new Ptr(pointer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from <see cref="Ptr"/> to void*.
|
||||
/// </summary>
|
||||
public static implicit operator void*(Ptr ptr)
|
||||
{
|
||||
return ptr.Pointer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicit conversion from nint to <see cref="Ptr"/>.
|
||||
/// </summary>
|
||||
public static explicit operator Ptr(nint ptr)
|
||||
{
|
||||
return new Ptr((void*)ptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicit cast to nint.
|
||||
/// </summary>
|
||||
public static explicit operator nint(Ptr ptr)
|
||||
{
|
||||
return (nint)ptr.Pointer;
|
||||
}
|
||||
|
||||
public bool Equals(Ptr other)
|
||||
{
|
||||
return Pointer == other.Pointer;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is Ptr other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return ((nint)Pointer).GetHashCode();
|
||||
}
|
||||
|
||||
public static bool operator ==(Ptr left, Ptr right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Ptr left, Ptr right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue