/// <summary> /// Returns a vector with the same orientation as this one but with a <see cref="Length"/> of <c>1f</c>. /// </summary> /// <remarks> /// This operation is also referred to as Normalization. /// </remarks> /// <returns>[this.X / Length, this.Y / Length, this.Z / Length, this.W / Length]</returns> public Vector4 ToUnit() { Assure.False(EqualsExactly(ZERO), "Zero vector has no corresponding unit."); if (IsUnit) { return(this); } float magnitude = Length; return(new Vector4(X / magnitude, Y / magnitude, Z / magnitude, W / magnitude)); }
/// <summary> /// Returns a unit-length quaternion with the same inter-component proportions. /// </summary> /// <returns>A unit length Quaternion.</returns> public Quaternion ToUnit() { Assure.False(EqualsExactly(ZERO), "Zero quaternion has no corresponding unit."); if (IsUnit) { return(this); } float magnitude = Norm; return(new Quaternion(X / magnitude, Y / magnitude, Z / magnitude, W / magnitude)); }
/// <summary> /// Freeze mutations to sensitive state, blocking any callers to <see cref="AcquirePermit()"/>. /// This method will block if mutations are currently ongoing on another thread; and will return once all active <see cref="MutationPermit"/>s /// have been disposed. /// </summary> public void FreezeMutations() { Assure.False(Monitor.IsEntered(freezeLock), "MutationPermits have not been disposed on current thread or mutations are already frozen."); Monitor.Enter(freezeLock); var msb = LosgapSystem.ActiveMSB; if (msb != null) { msb.MasterWaitOnExternalLock(freezeLock, freezeMasterBlockPredicate); } Assure.Equal(activeMutatorsCounter, 0); }
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(); } }
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); }
/// <summary> /// Relinquishes permission for mutation, allowing state-freezing to happen. /// </summary> public void Dispose() { if (additionalLockObject != null) { Monitor.Exit(additionalLockObject); } lock (barrier.freezeLock) { #if DEBUG Assure.False(isDisposed, "Can not dispose same MutationPermit more than once!"); isDisposed = true; #endif --barrier.activeMutatorsCounter; if (barrier.activeMutatorsCounter == 0) { Monitor.PulseAll(barrier.freezeLock); } } }
public void False() { // Define variables and constants // Set up context // Execute Assure.False(false); try { Assure.False(true); Assert.Fail(); } catch (AssuranceFailedException) { } // Assert outcome }