Struct SafePointer
Represents a safe wrapper around an unmanaged memory pointer with bounds checking and shifting capabilities.
public struct SafePointer
- Inherited Members
- Extension Methods
Constructors
SafePointer(IntPtr, int?)
Initializes a new instance of the SafePointer struct with a specified pointer and optional size.
public SafePointer(IntPtr pointer, int? size)
Parameters
pointerIntPtrThe unmanaged memory pointer to wrap. Must not be Zero.
sizeint?The optional size of the memory block in bytes. If specified, must be non-negative.
Exceptions
- ArgumentOutOfRangeException
Thrown when
pointeris Zero orsizeis negative.
Properties
Pointer
Gets the current unmanaged memory pointer.
public IntPtr Pointer { get; }
Property Value
Methods
CopyTo(byte[], bool)
Copies data from the current pointer position to a byte array.
public void CopyTo(byte[] buffer, bool autoShift = false)
Parameters
bufferbyte[]The target byte array to copy data into.
autoShiftboolIf
true, shifts the pointer by the length of the copied data after copying.
Exceptions
- ArgumentOutOfRangeException
Thrown when the operation exceeds the defined size boundary.
CopyTo(byte[], int, int, bool)
Copies a specified amount of data from the current pointer position to a byte array.
public void CopyTo(byte[] buffer, int offset, int length, bool autoShift = false)
Parameters
bufferbyte[]The target byte array to copy data into.
offsetintThe starting index in the buffer where data should be copied.
lengthintThe number of bytes to copy.
autoShiftboolIf
true, shifts the pointer bylengthafter copying.
Exceptions
- ArgumentOutOfRangeException
Thrown when the operation exceeds the defined size boundary.
Read<TValue>(bool)
Reads a value of type TValue from the current pointer position.
public TValue Read<TValue>(bool autoShift = false) where TValue : struct
Parameters
autoShiftboolIf
true, shifts the pointer by the size ofTValueafter reading.
Returns
- TValue
The value read from the pointer.
Type Parameters
TValueThe type of the value to read. Must be a value type.
Exceptions
- ArgumentOutOfRangeException
Thrown when the operation exceeds the defined size boundary.
Shift(int)
Shifts the pointer forward by the specified offset in bytes.
public void Shift(int offset)
Parameters
offsetintThe number of bytes to shift the pointer.
Exceptions
- ArgumentOutOfRangeException
Thrown when the shift exceeds the defined size boundary.
Shift<TStruct>()
Shifts the pointer forward by the size of the specified structure type.
public void Shift<TStruct>() where TStruct : struct
Type Parameters
TStructThe type of the structure whose size determines the shift. Must be a value type.
Exceptions
- ArgumentOutOfRangeException
Thrown when the shift exceeds the defined size boundary.
ToStruct<TStruct>(bool)
Reads a structure of type TStruct from the current pointer position.
public TStruct ToStruct<TStruct>(bool autoShift = false) where TStruct : struct
Parameters
autoShiftboolIf
true, shifts the pointer by the size ofTStructafter reading.
Returns
- TStruct
The structure read from the pointer.
Type Parameters
TStructThe type of the structure to read. Must be a value type.
Exceptions
- ArgumentOutOfRangeException
Thrown when the operation exceeds the defined size boundary.
Operators
implicit operator IntPtr(SafePointer)
Implicitly converts a SafePointer to an IntPtr.
public static implicit operator IntPtr(SafePointer pointer)
Parameters
pointerSafePointerThe SafePointer instance to convert.