/// <summary> /// Writes the specified array T[] data to a memory location. /// </summary> /// <typeparam name="T">Type of a data to write</typeparam> /// <param name="destination">Memory location to write to.</param> /// <param name="data">The array of T data to write.</param> /// <param name="offset">The offset in the array to read from.</param> /// <param name="count">The number of T element to write to the memory location</param> /// <returns>destination pointer + sizeof(T) * count</returns> public static IntPtr Write <T>(IntPtr destination, T[] data, int offset, int count) where T : struct { unsafe { return((IntPtr)Interop.Write((void *)destination, data, offset, count)); } }
/// <summary> /// Writes the specified T data to a memory location. /// </summary> /// <typeparam name="T">Type of a data to write</typeparam> /// <param name="destination">Memory location to write to.</param> /// <param name="data">The data to write.</param> /// <returns>destination pointer + sizeof(T)</returns> public static IntPtr WriteAndPosition <T>(IntPtr destination, ref T data) where T : struct { unsafe { return((IntPtr)Interop.Write((void *)destination, ref data)); } }
public T this[int index] { get { if (index >= Length) { throw new ArgumentOutOfRangeException(); } var res = new T(); unsafe { var bptr = (byte *)Pointer; bptr += index * sizeOfT; Interop.Read <T>(bptr, ref res); } return(res); } set { if (index >= Length) { throw new ArgumentOutOfRangeException(); } unsafe { var bptr = (byte *)Pointer; bptr += index * sizeOfT; Interop.Write <T>(bptr, ref value); } } }
public void Write([NotNull] T[] source, int offset = 0) { if (offset + source.Length > Length) { throw new ArgumentOutOfRangeException(); } unsafe { Interop.Write((void *)Pointer, source, offset, source.Length); } }
public void Write(T[] source, int pointerByteOffset, int arrayOffset, int arrayLen) { if (arrayOffset + arrayLen > Length) { throw new ArgumentOutOfRangeException(); } unsafe { var ptr = (byte *)Pointer; ptr += pointerByteOffset; Interop.Write(ptr, source, arrayOffset, arrayLen); } }
/// <summary> /// Covnerts a structured array to an equivalent byte array. /// </summary> /// <param name="source">The source.</param> /// <returns>The byte array.</returns> public static byte[] ToByteArray <T>(T[] source) where T : struct { if (source == null) { return(null); } var buffer = new byte[SizeOf <T>() * source.Length]; if (source.Length == 0) { return(buffer); } unsafe { fixed(void *pBuffer = buffer) Interop.Write(pBuffer, source, 0, source.Length); } return(buffer); }