public static TOut Reinterpret <TIn, TOut>(TIn curValue, int sizeBytes) where TIn : struct where TOut : struct { Assure.Equal(SizeOf <TIn>(), SizeOf <TOut>(), "TOut must be a type of identical byte size to TIn."); Assure.Equal( sizeBytes, SizeOf <TIn>(), "Provided sizeBytes value (" + sizeBytes + ") is incorrect (sizeof(TIn) == " + SizeOf <TIn>() + ")" ); Assure.True(typeof(TIn).IsBlittable(), "TIn type is not blittable."); Assure.True(typeof(TOut).IsBlittable(), "TOut type is not blittable."); TOut result = default(TOut); /* What I'm doing is using TypedReference as { IntPtr, IntPtr }; * where the first IntPtr is a pointer to the __makeref referenced value type */ TypedReference resultRef = __makeref(result); byte * resultPtr = (byte *)*((IntPtr *)&resultRef); // Now do the same with curValue TypedReference curValueRef = __makeref(curValue); byte * curValuePtr = (byte *)*((IntPtr *)&curValueRef); // Finally, copy for (int i = 0; i < sizeBytes; ++i) { resultPtr[i] = curValuePtr[i]; } return(result); }
public void MasterWaitOnExternalLock(object lockObj, Func <bool> completionPredicate) { Assure.NotNull(lockObj); Assure.NotNull(completionPredicate); Assure.True(Monitor.IsEntered(lockObj)); Assure.Equal(Thread.CurrentThread, LosgapSystem.MasterThread); Monitor.Enter(externalLockObjLock); externalLockObj = lockObj; try { while (!completionPredicate()) { while (masterInvocationQueue.Count > 0) { PipelineMasterInvocation pmi; if (!masterInvocationQueue.TryDequeue(out pmi)) { continue; } try { if (pmi.IsSynchronousCall) { Monitor.Enter(pmi.InvocationCompleteMonitor); pmi.Action(); } else { pmi.Action(); } } catch (Exception e) { if (pmi.IsSynchronousCall) { pmi.RaisedException = e; } else { LosgapSystem.ExitWithError("Exception raised in asynchronous pipeline master invocation.", e); } } finally { if (pmi.IsSynchronousCall) { Monitor.Pulse(pmi.InvocationCompleteMonitor); Monitor.Exit(pmi.InvocationCompleteMonitor); } } } Monitor.Exit(externalLockObjLock); Monitor.Wait(lockObj); Monitor.Enter(externalLockObjLock); } } finally { externalLockObj = null; Monitor.Exit(externalLockObjLock); } }
/// <summary> /// Returns the Y co-ordinate of the requested <paramref name="corner"/> of this rectangle. /// </summary> /// <param name="corner">The corner whose position it is you wish to obtain.</param> /// <returns>The position on the Y-axis of the requested <paramref name="corner"/> of this rectangle.</returns> public float GetCornerY(RectangleCorner corner) { Assure.True(Enum.IsDefined(typeof(RectangleCorner), corner), "Invalid corner constant: " + corner + " is not a valid RectangleCorner value."); switch (corner) { case RectangleCorner.TopLeft: case RectangleCorner.TopRight: return(BottomLeft.Y + Height); default: return(BottomLeft.Y); } }
public static void WriteGenericToPtr <T>(IntPtr dest, T value, int sizeOfT) where T : struct { Assure.False(dest == IntPtr.Zero); Assure.True(typeof(T).IsBlittable()); Assure.Equal(sizeOfT, (uint)SizeOf <T>()); byte * bytePtr = (byte *)dest; TypedReference valueref = __makeref(value); byte * valuePtr = (byte *)*((IntPtr *)&valueref); for (uint i = 0U; i < sizeOfT; ++i) { bytePtr[i] = valuePtr[i]; } }
/// <summary> /// Copies data from the <paramref name="source"/> pointer to the <paramref name="destination"/> array. /// </summary> /// <typeparam name="T">The array element type. Must be a <see cref="Extensions.IsBlittable">blittable</see> struct.</typeparam> /// <param name="source">The pointer to the source data. Must not be <see cref="IntPtr.Zero"/>.</param> /// <param name="destination">The destination array to which the data will be copied. This method will copy /// <see cref="ArraySlice{T}.Length"/> elements' worth of data, starting at the <see cref="ArraySlice{T}.Offset"/>.</param> /// <param name="arrayElementSizeBytes">The size of <typeparamref name="T"/>. Use either sizeof() (preferred), or /// <see cref="SizeOf{T}"/> if the type is not known at compile-time.</param> public static unsafe void CopyGenericArray <T>(IntPtr source, ArraySlice <T> destination, uint arrayElementSizeBytes) where T : struct { Assure.False(source == IntPtr.Zero); Assure.True(typeof(T).IsBlittable()); Assure.Equal((int)arrayElementSizeBytes, SizeOf <T>()); GCHandle pinnedArrayHandle = GCHandle.Alloc(destination.ContainingArray, GCHandleType.Pinned); try { MemCopy(source, pinnedArrayHandle.AddrOfPinnedObject() + (int)(arrayElementSizeBytes * destination.Offset), destination.Length * arrayElementSizeBytes); } finally { pinnedArrayHandle.Free(); } }
/// <summary> /// Returns the Z co-ordinate of the requested <paramref name="corner"/> of this cuboid. /// </summary> /// <param name="corner">The corner whose position it is you wish to obtain.</param> /// <returns>The position on the Z-axis of the requested <paramref name="corner"/> of this cuboid.</returns> public float GetCornerZ(CuboidCorner corner) { Assure.True(Enum.IsDefined(typeof(CuboidCorner), corner), "Invalid corner constant: " + corner + " is not a valid CuboidCorner value."); switch (corner) { case CuboidCorner.BackTopLeft: case CuboidCorner.BackTopRight: case CuboidCorner.BackBottomLeft: case CuboidCorner.BackBottomRight: return(FrontBottomLeft.Z + Depth); default: return(FrontBottomLeft.Z); } }
/// <summary> /// Returns a rectangle describing the requested <paramref name="side"/> of this cuboid (transposed in to the 2D XY plane). /// </summary> /// <param name="side">The side that you wish to get a description of.</param> /// <returns>A <see cref="Rectangle"/> whose dimensions represent the requested <paramref name="side"/> /// of this cuboid.</returns> public Rectangle GetSide(CuboidSide side) { Assure.True(Enum.IsDefined(typeof(CuboidSide), side), "Invalid side constant: " + side + " is not a valid CuboidSide value."); switch (side) { case CuboidSide.Top: case CuboidSide.Bottom: return(new Rectangle((Vector2)FrontBottomLeft[0, 2], Width, Depth)); case CuboidSide.Left: case CuboidSide.Right: return(new Rectangle((Vector2)FrontBottomLeft[2, 1], Depth, Height)); default: return(new Rectangle((Vector2)FrontBottomLeft[0, 1], Width, Height)); } }
public static T ReadGenericFromPtr <T>(IntPtr source, int sizeOfT) where T : struct { Assure.False(source == IntPtr.Zero); Assure.True(typeof(T).IsBlittable()); Assure.Equal(sizeOfT, (uint)SizeOf <T>()); byte *bytePtr = (byte *)source; T result = default(T); TypedReference resultRef = __makeref(result); byte * resultPtr = (byte *)*((IntPtr *)&resultRef); for (uint i = 0U; i < sizeOfT; ++i) { resultPtr[i] = bytePtr[i]; } return(result); }
public void True() { // Define variables and constants // Set up context // Execute Assure.True(true); try { Assure.True(false); Assert.Fail(); } catch (AssuranceFailedException) { } // Assert outcome }
/// <summary> /// Relinquish control of the protected state, allowing mutations to proceed once again. /// </summary> public void UnfreezeMutations() { Assure.True(Monitor.IsEntered(freezeLock), "Mutations are not currently frozen!"); Monitor.Exit(freezeLock); }
private bool FreezeMasterBlockPredicate() { Assure.True(Monitor.IsEntered(freezeLock)); Assure.Equal(Thread.CurrentThread, LosgapSystem.MasterThread); return(activeMutatorsCounter == 0); }