From 800d14f40b8ad99ebaf0efd29a191fc86642083b Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Wed, 2 Mar 2022 11:08:30 -0700 Subject: [PATCH] Add more assertion options --- src/LibHac/Diag/Assert.cs | 147 ++++++++++++++++++++++++++++- src/LibHac/Diag/Impl/AssertImpl.cs | 14 ++- 2 files changed, 158 insertions(+), 3 deletions(-) diff --git a/src/LibHac/Diag/Assert.cs b/src/LibHac/Diag/Assert.cs index 02b4353d..5b8a9bf0 100644 --- a/src/LibHac/Diag/Assert.cs +++ b/src/LibHac/Diag/Assert.cs @@ -572,7 +572,99 @@ public static class Assert } // --------------------------------------------------------------------- - // In range + // Null UniqueRef + // --------------------------------------------------------------------- + + private static void NullImpl(AssertionType assertionType, in UniqueRef value, + string valueText, string functionName, string fileName, int lineNumber) where T : class, IDisposable + { + if (AssertImpl.Null(in value)) + return; + + AssertImpl.InvokeAssertionNotNull(assertionType, valueText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + public static void Null(in UniqueRef value, + [CallerArgumentExpression("value")] string valueText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + where T : class, IDisposable + { + NullImpl(AssertionType.UserAssert, in value, valueText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + internal static void SdkNull(in UniqueRef value, + [CallerArgumentExpression("value")] string valueText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + where T : class, IDisposable + { + NullImpl(AssertionType.SdkAssert, in value, valueText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + internal static void SdkRequiresNull(in UniqueRef value, + [CallerArgumentExpression("value")] string valueText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + where T : class, IDisposable + { + NullImpl(AssertionType.SdkRequires, in value, valueText, functionName, fileName, lineNumber); + } + + // --------------------------------------------------------------------- + // Null SharedRef + // --------------------------------------------------------------------- + + private static void NullImpl(AssertionType assertionType, in SharedRef value, + string valueText, string functionName, string fileName, int lineNumber) where T : class, IDisposable + { + if (AssertImpl.Null(in value)) + return; + + AssertImpl.InvokeAssertionNotNull(assertionType, valueText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + public static void Null(in SharedRef value, + [CallerArgumentExpression("value")] string valueText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + where T : class, IDisposable + { + NullImpl(AssertionType.UserAssert, in value, valueText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + internal static void SdkNull(in SharedRef value, + [CallerArgumentExpression("value")] string valueText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + where T : class, IDisposable + { + NullImpl(AssertionType.SdkAssert, in value, valueText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + internal static void SdkRequiresNull(in SharedRef value, + [CallerArgumentExpression("value")] string valueText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + where T : class, IDisposable + { + NullImpl(AssertionType.SdkRequires, in value, valueText, functionName, fileName, lineNumber); + } + + // --------------------------------------------------------------------- + // In range int // --------------------------------------------------------------------- private static void InRangeImpl(AssertionType assertionType, int value, int lower, int upper, string valueText, @@ -624,6 +716,59 @@ public static class Assert upperExclusiveText, functionName, fileName, lineNumber); } + // --------------------------------------------------------------------- + // In range long + // --------------------------------------------------------------------- + + private static void InRangeImpl(AssertionType assertionType, long value, long lower, long upper, string valueText, + string lowerText, string upperText, string functionName, string fileName, int lineNumber) + { + if (AssertImpl.WithinRange(value, lower, upper)) + return; + + AssertImpl.InvokeAssertionInRange(assertionType, value, lower, upper, valueText, lowerText, upperText, functionName, + fileName, lineNumber); + } + + [Conditional(AssertCondition)] + public static void InRange(long value, long lowerInclusive, long upperExclusive, + [CallerArgumentExpression("value")] string valueText = "", + [CallerArgumentExpression("lowerInclusive")] string lowerInclusiveText = "", + [CallerArgumentExpression("upperExclusive")] string upperExclusiveText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + { + InRangeImpl(AssertionType.UserAssert, value, lowerInclusive, upperExclusive, valueText, lowerInclusiveText, + upperExclusiveText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + internal static void SdkInRange(long value, long lowerInclusive, long upperExclusive, + [CallerArgumentExpression("value")] string valueText = "", + [CallerArgumentExpression("lowerInclusive")] string lowerInclusiveText = "", + [CallerArgumentExpression("upperExclusive")] string upperExclusiveText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + { + InRangeImpl(AssertionType.SdkAssert, value, lowerInclusive, upperExclusive, valueText, lowerInclusiveText, + upperExclusiveText, functionName, fileName, lineNumber); + } + + [Conditional(AssertCondition)] + internal static void SdkRequiresInRange(long value, long lowerInclusive, long upperExclusive, + [CallerArgumentExpression("value")] string valueText = "", + [CallerArgumentExpression("lowerInclusive")] string lowerInclusiveText = "", + [CallerArgumentExpression("upperExclusive")] string upperExclusiveText = "", + [CallerMemberName] string functionName = "", + [CallerFilePath] string fileName = "", + [CallerLineNumber] int lineNumber = 0) + { + InRangeImpl(AssertionType.SdkRequires, value, lowerInclusive, upperExclusive, valueText, lowerInclusiveText, + upperExclusiveText, functionName, fileName, lineNumber); + } + // --------------------------------------------------------------------- // Within min-max int // --------------------------------------------------------------------- diff --git a/src/LibHac/Diag/Impl/AssertImpl.cs b/src/LibHac/Diag/Impl/AssertImpl.cs index cc16becc..cc2e262e 100644 --- a/src/LibHac/Diag/Impl/AssertImpl.cs +++ b/src/LibHac/Diag/Impl/AssertImpl.cs @@ -22,7 +22,7 @@ internal static class AssertImpl $"{valueText} must be nullptr."); } - internal static void InvokeAssertionInRange(AssertionType assertionType, int value, int lower, int upper, + internal static void InvokeAssertionInRange(AssertionType assertionType, long value, long lower, long upper, string valueText, string lowerText, string upperText, string functionName, string fileName, int lineNumber) { string message = @@ -126,6 +126,16 @@ internal static class AssertImpl return Unsafe.IsNullRef(ref item); } + public static bool Null(in UniqueRef item) where T : class, IDisposable + { + return !item.HasValue; + } + + public static bool Null(in SharedRef item) where T : class, IDisposable + { + return !item.HasValue; + } + public static bool NotNull(T item) where T : class { return item is not null; @@ -240,4 +250,4 @@ internal static class AssertImpl { return Alignment.IsAlignedPow2(value, (uint)alignment); } -} +} \ No newline at end of file