aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-01-13 22:42:13 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-01-13 22:42:13 -0500
commit299c41bf07d22daf7a6707bf2d485193c0ef61b8 (patch)
treec77740a12e74689ed637bf177c3cc78aa0672e92
parentddead3ab60254fcc1c8029715e094480da51dcd0 (diff)
using new exception api
-rw-r--r--lib/Net.Http/src/Core/ConnectionInfo.cs4
-rw-r--r--lib/Net.Http/src/Core/HttpEvent.cs12
-rw-r--r--lib/Plugins.Essentials/src/Extensions/SingleCookieController.cs6
-rw-r--r--lib/Utils/src/ArgumentList.cs6
-rw-r--r--lib/Utils/src/Extensions/StringExtensions.cs4
-rw-r--r--lib/Utils/src/Extensions/ThreadingExtensions.cs4
-rw-r--r--lib/Utils/src/Memory/Caching/ObjectRental.cs2
-rw-r--r--lib/Utils/src/Memory/UnsafeMemoryHandle.cs7
-rw-r--r--lib/Utils/src/Memory/VnString.cs16
-rw-r--r--lib/Utils/src/Memory/VnTable.cs92
-rw-r--r--lib/Utils/src/Native/SafeLibraryHandle.cs2
-rw-r--r--lib/Utils/src/Resources/ManagedLibrary.cs8
-rw-r--r--lib/Utils/src/VnEncoding.cs2
13 files changed, 70 insertions, 95 deletions
diff --git a/lib/Net.Http/src/Core/ConnectionInfo.cs b/lib/Net.Http/src/Core/ConnectionInfo.cs
index ff73cce..3af2b44 100644
--- a/lib/Net.Http/src/Core/ConnectionInfo.cs
+++ b/lib/Net.Http/src/Core/ConnectionInfo.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Net.Http
@@ -94,7 +94,7 @@ namespace VNLib.Net.Http
public void SetCookie(string name, string value, string? domain, string? path, TimeSpan Expires, CookieSameSite sameSite, bool httpOnly, bool secure)
{
//name MUST not be null
- _ = name ?? throw new ArgumentNullException(nameof(name));
+ ArgumentNullException.ThrowIfNull(name);
//Create the new cookie
HttpCookie cookie = new(name)
diff --git a/lib/Net.Http/src/Core/HttpEvent.cs b/lib/Net.Http/src/Core/HttpEvent.cs
index ee6a380..a86bc20 100644
--- a/lib/Net.Http/src/Core/HttpEvent.cs
+++ b/lib/Net.Http/src/Core/HttpEvent.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Net.Http
@@ -71,8 +71,8 @@ namespace VNLib.Net.Http
{
throw new InvalidOperationException("A protocol handler was already specified");
}
-
- _ = protocolHandler ?? throw new ArgumentNullException(nameof(protocolHandler));
+
+ ArgumentNullException.ThrowIfNull(protocolHandler);
//Set 101 status code
Context.Respond(HttpStatusCode.SwitchingProtocols);
@@ -87,11 +87,7 @@ namespace VNLib.Net.Http
void IHttpEvent.CloseResponse(HttpStatusCode code, ContentType type, Stream stream, long length)
{
ArgumentNullException.ThrowIfNull(stream, nameof(stream));
-
- if(length < 0)
- {
- throw new ArgumentException("Length must be greater than or equal to 0", nameof(length));
- }
+ ArgumentOutOfRangeException.ThrowIfNegative(length);
//Check if the stream is valid. We will need to read the stream, and we will also need to get the length property
if (!stream.CanRead)
diff --git a/lib/Plugins.Essentials/src/Extensions/SingleCookieController.cs b/lib/Plugins.Essentials/src/Extensions/SingleCookieController.cs
index 74d23f7..2d41e4c 100644
--- a/lib/Plugins.Essentials/src/Extensions/SingleCookieController.cs
+++ b/lib/Plugins.Essentials/src/Extensions/SingleCookieController.cs
@@ -71,21 +71,21 @@ namespace VNLib.Plugins.Essentials.Extensions
///<inheritdoc/>
public void ExpireCookie(IHttpEvent entity, bool force)
{
- _ = entity ?? throw new ArgumentNullException(nameof(entity));
+ ArgumentNullException.ThrowIfNull(entity);
SetCookieInternal(entity, string.Empty, force);
}
///<inheritdoc/>
public string? GetCookie(IHttpEvent entity)
{
- _ = entity ?? throw new ArgumentNullException(nameof(entity));
+ ArgumentNullException.ThrowIfNull(entity);
return entity.Server.RequestCookies.GetValueOrDefault(Name);
}
///<inheritdoc/>
public void SetCookie(IHttpEvent entity, string value)
{
- _ = entity ?? throw new ArgumentNullException(nameof(entity));
+ ArgumentNullException.ThrowIfNull(entity);
SetCookieInternal(entity, value, true);
}
diff --git a/lib/Utils/src/ArgumentList.cs b/lib/Utils/src/ArgumentList.cs
index ae45e36..235e62c 100644
--- a/lib/Utils/src/ArgumentList.cs
+++ b/lib/Utils/src/ArgumentList.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2022 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -42,7 +42,7 @@ namespace VNLib.Utils
/// <exception cref="ArgumentNullException"></exception>
public ArgumentList(string[] args)
{
- _ = args ?? throw new ArgumentNullException(nameof(args));
+ ArgumentNullException.ThrowIfNull(args);
_args = args.ToList();
}
@@ -53,7 +53,7 @@ namespace VNLib.Utils
/// <exception cref="ArgumentNullException"></exception>
public ArgumentList(IReadOnlyList<string> args)
{
- _ = args ?? throw new ArgumentNullException(nameof(args));
+ ArgumentNullException.ThrowIfNull(args);
_args = args.ToList();
}
diff --git a/lib/Utils/src/Extensions/StringExtensions.cs b/lib/Utils/src/Extensions/StringExtensions.cs
index 7ffaaa2..c71d5a0 100644
--- a/lib/Utils/src/Extensions/StringExtensions.cs
+++ b/lib/Utils/src/Extensions/StringExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -132,7 +132,7 @@ namespace VNLib.Utils.Extensions
/// <exception cref="ArgumentNullException"></exception>
public static void Split<T>(this ReadOnlySpan<char> value, ReadOnlySpan<char> splitter, StringSplitOptions options, ReadOnlySpanAction<char, T> splitCb, T state)
{
- _ = splitCb ?? throw new ArgumentNullException(nameof(splitCb));
+ ArgumentNullException.ThrowIfNull(splitCb);
//Get span over string
ForwardOnlyReader<char> reader = new(value);
diff --git a/lib/Utils/src/Extensions/ThreadingExtensions.cs b/lib/Utils/src/Extensions/ThreadingExtensions.cs
index 5180a11..a80a0ae 100644
--- a/lib/Utils/src/Extensions/ThreadingExtensions.cs
+++ b/lib/Utils/src/Extensions/ThreadingExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2022 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -200,7 +200,7 @@ namespace VNLib.Utils.Extensions
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static Task<bool> WaitAsync(this WaitHandle handle, int timeoutMs = Timeout.Infinite)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
+ ArgumentNullException.ThrowIfNull(handle);
//test non-blocking handle state
if (handle.WaitOne(0))
{
diff --git a/lib/Utils/src/Memory/Caching/ObjectRental.cs b/lib/Utils/src/Memory/Caching/ObjectRental.cs
index e80b0b5..1b29665 100644
--- a/lib/Utils/src/Memory/Caching/ObjectRental.cs
+++ b/lib/Utils/src/Memory/Caching/ObjectRental.cs
@@ -106,7 +106,7 @@ namespace VNLib.Utils.Memory.Caching
/// <exception cref="ObjectDisposedException"></exception>
public virtual void Return(T item)
{
- _ = item ?? throw new ArgumentNullException(nameof(item));
+ ArgumentNullException.ThrowIfNull(item);
Check();
diff --git a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs
index f79a094..164306a 100644
--- a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs
+++ b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -93,10 +93,7 @@ namespace VNLib.Utils.Memory
/// <exception cref="ArgumentOutOfRangeException"></exception>
internal UnsafeMemoryHandle(ArrayPool<T> pool, T[] array, int elements)
{
- if (elements < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(elements));
- }
+ ArgumentOutOfRangeException.ThrowIfNegative(elements);
//Pool and array is required
_pool = pool ?? throw new ArgumentNullException(nameof(pool));
_poolArr = array ?? throw new ArgumentNullException(nameof(array));
diff --git a/lib/Utils/src/Memory/VnString.cs b/lib/Utils/src/Memory/VnString.cs
index d5b34ac..6c79598 100644
--- a/lib/Utils/src/Memory/VnString.cs
+++ b/lib/Utils/src/Memory/VnString.cs
@@ -114,7 +114,7 @@ namespace VNLib.Utils.Memory
/// <exception cref="ArgumentNullException"></exception>
public static VnString FromBinary(ReadOnlySpan<byte> data, Encoding encoding, IUnmangedHeap? heap = null)
{
- _ = encoding ?? throw new ArgumentNullException(nameof(encoding));
+ ArgumentNullException.ThrowIfNull(encoding);
if (data.IsEmpty)
{
@@ -186,9 +186,9 @@ namespace VNLib.Utils.Memory
/// <exception cref="InvalidOperationException"></exception>
public static VnString FromStream(Stream stream, Encoding encoding, IUnmangedHeap heap, int bufferSize)
{
- _ = stream ?? throw new ArgumentNullException(nameof(stream));
- _ = encoding ?? throw new ArgumentNullException(nameof(encoding));
- _ = heap ?? throw new ArgumentNullException(nameof(heap));
+ ArgumentNullException.ThrowIfNull(stream);
+ ArgumentNullException.ThrowIfNull(encoding);
+ ArgumentNullException.ThrowIfNull(heap);
//Make sure the stream is readable
if (!stream.CanRead)
@@ -273,9 +273,9 @@ namespace VNLib.Utils.Memory
/// <exception cref="OutOfMemoryException"></exception>
public static async ValueTask<VnString> FromStreamAsync(Stream stream, Encoding encoding, IUnmangedHeap heap, int bufferSize)
{
- _ = stream ?? throw new ArgumentNullException(nameof(stream));
- _ = encoding ?? throw new ArgumentNullException(nameof(encoding));
- _ = heap ?? throw new ArgumentNullException(nameof(heap));
+ ArgumentNullException.ThrowIfNull(stream);
+ ArgumentNullException.ThrowIfNull(encoding);
+ ArgumentNullException.ThrowIfNull(heap);
//Make sure the stream is readable
if (!stream.CanRead)
@@ -505,7 +505,7 @@ namespace VNLib.Utils.Memory
///<exception cref="ArgumentNullException"></exception>
public int CompareTo(VnString? other)
{
- _ = other ?? throw new ArgumentNullException(nameof(other));
+ ArgumentNullException.ThrowIfNull(other);
return AsSpan().CompareTo(other.AsSpan(), StringComparison.Ordinal);
}
diff --git a/lib/Utils/src/Memory/VnTable.cs b/lib/Utils/src/Memory/VnTable.cs
index 7769c23..ce105f9 100644
--- a/lib/Utils/src/Memory/VnTable.cs
+++ b/lib/Utils/src/Memory/VnTable.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -23,7 +23,7 @@
*/
using System;
-using System.Runtime.CompilerServices;
+using System.Diagnostics;
using VNLib.Utils.Extensions;
@@ -59,20 +59,20 @@ namespace VNLib.Utils.Memory
/// <param name="rows">Number of rows in the table</param>
/// <param name="cols">Number of columns in the table</param>
public VnTable(uint rows, uint cols) : this(MemoryUtil.Shared, rows, cols) { }
-
+
/// <summary>
/// Creates a new 2 dimensional table in unmanaged heap memory, using the specified heap.
/// User should dispose of the table when no longer in use
/// </summary>
- /// <param name="heap"><see cref="Win32PrivateHeap"/> to allocate table memory from</param>
+ /// <param name="heap"><see cref="IUnmangedHeap"/> to allocate table memory from</param>
/// <param name="rows">Number of rows in the table</param>
/// <param name="cols">Number of columns in the table</param>
+ /// <exception cref="ArgumentNullException"></exception>
+ /// <exception cref="ArgumentOutOfRangeException"></exception>
+ /// <exception cref="OverflowException"></exception>
+ /// <exception cref="OutOfMemoryException"></exception>
public VnTable(IUnmangedHeap heap, uint rows, uint cols)
{
- if (rows < 0 || cols < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(rows), "Row and coulmn number must be 0 or larger");
- }
//empty table
if (rows == 0 && cols == 0)
{
@@ -80,17 +80,14 @@ namespace VNLib.Utils.Memory
}
else
{
- _ = heap ?? throw new ArgumentNullException(nameof(heap));
-
- this.Rows = rows;
- this.Cols = cols;
-
ulong tableSize = checked((ulong)rows * (ulong)cols);
- if ((tableSize * (uint)Unsafe.SizeOf<T>()) > nuint.MaxValue)
- {
- throw new ArgumentOutOfRangeException("Rows and cols","Table size is too large");
- }
+ ArgumentNullException.ThrowIfNull(heap);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(tableSize, nuint.MinValue);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(MemoryUtil.ByteCount<T>((nuint)tableSize), nuint.MaxValue, nameof(rows));
+
+ Rows = rows;
+ Cols = cols;
//Alloc a buffer with zero memory enabled, with Rows * Cols number of elements
BufferHandle = heap.Alloc<T>((nuint)tableSize, true);
@@ -108,26 +105,11 @@ namespace VNLib.Utils.Memory
/// <exception cref="ArgumentOutOfRangeException"></exception>
public T Get(uint row, uint col)
{
- Check();
- if (this.Empty)
- {
- throw new InvalidOperationException("Table is empty");
- }
- if (row < 0 || col < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(row), "Row or column address less than 0");
- }
- if (row > this.Rows)
- {
- throw new ArgumentOutOfRangeException(nameof(row), "Row out of range of current table");
- }
- if (col > this.Cols)
- {
- throw new ArgumentOutOfRangeException(nameof(col), "Column address out of range of current table");
- }
+ ValidateArgs(row, col);
+
//Calculate the address in memory for the item
//Calc row offset
- ulong address = checked(row * this.Cols);
+ ulong address = checked(row * Cols);
//Calc column offset
address += col;
@@ -151,26 +133,10 @@ namespace VNLib.Utils.Memory
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Set(uint row, uint col, T item)
{
- Check();
- if (this.Empty)
- {
- throw new InvalidOperationException("Table is empty");
- }
- if (row < 0 || col < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(row), "Row or column address less than 0");
- }
- if (row > this.Rows)
- {
- throw new ArgumentOutOfRangeException(nameof(row), "Row out of range of current table");
- }
- if (col > this.Cols)
- {
- throw new ArgumentOutOfRangeException(nameof(col), "Column address out of range of current table");
- }
-
+ ValidateArgs(row, col);
+
//Calculate the address in memory for the item
-
+
//Calc row offset
ulong address = checked(Cols * row);
@@ -183,7 +149,23 @@ namespace VNLib.Utils.Memory
*BufferHandle!.GetOffset((nuint)address) = item;
}
}
-
+
+ private void ValidateArgs(uint row, uint col)
+ {
+ Check();
+
+ if (Empty)
+ {
+ throw new InvalidOperationException("Table is empty");
+ }
+
+ //If not empty expect a non-null handle
+ Debug.Assert(BufferHandle != null, nameof(BufferHandle) + " != null");
+
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(row, Rows);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(col, Cols);
+ }
+
/// <summary>
/// Equivalent to <see cref="VnTable{T}.Get(uint, uint)"/> and <see cref="VnTable{T}.Set(uint, uint, T)"/>
/// </summary>
diff --git a/lib/Utils/src/Native/SafeLibraryHandle.cs b/lib/Utils/src/Native/SafeLibraryHandle.cs
index b3594e1..867523d 100644
--- a/lib/Utils/src/Native/SafeLibraryHandle.cs
+++ b/lib/Utils/src/Native/SafeLibraryHandle.cs
@@ -61,7 +61,7 @@ namespace VNLib.Utils.Native
/// <exception cref="DllNotFoundException"></exception>
public static SafeLibraryHandle LoadLibrary(string libPath, DllImportSearchPath searchPath = DllImportSearchPath.ApplicationDirectory)
{
- _ = libPath ?? throw new ArgumentNullException(nameof(libPath));
+ ArgumentNullException.ThrowIfNull(libPath);
//See if the path includes a file extension
return TryLoadLibrary(libPath, searchPath, out SafeLibraryHandle? lib)
? lib
diff --git a/lib/Utils/src/Resources/ManagedLibrary.cs b/lib/Utils/src/Resources/ManagedLibrary.cs
index 56835c7..786a22d 100644
--- a/lib/Utils/src/Resources/ManagedLibrary.cs
+++ b/lib/Utils/src/Resources/ManagedLibrary.cs
@@ -189,7 +189,7 @@ namespace VNLib.Utils.Resources
/// <exception cref="FileNotFoundException"></exception>
public static ManagedLibrary LoadManagedAssembly(string assemblyName, AssemblyLoadContext loadContext)
{
- _ = loadContext ?? throw new ArgumentNullException(nameof(loadContext));
+ ArgumentNullException.ThrowIfNull(loadContext);
//Make sure the file exists
if (!FileOperations.FileExists(assemblyName))
@@ -218,8 +218,8 @@ namespace VNLib.Utils.Resources
BindingFlags flags = BindingFlags.Public
) where TDelegate : Delegate
{
- _ = obj ?? throw new ArgumentNullException(nameof(obj));
- return TryGetMethodInternal<TDelegate>(obj.GetType(), methodName, obj, flags | BindingFlags.Instance);
+ ArgumentNullException.ThrowIfNull(obj);
+ return TryGetMethodInternal<TDelegate>(obj.GetType(), methodName, obj, flags | BindingFlags.Instance);
}
/// <summary>
@@ -271,7 +271,7 @@ namespace VNLib.Utils.Resources
private static TDelegate? TryGetMethodInternal<TDelegate>(Type type, string methodName, object? target, BindingFlags flags) where TDelegate : Delegate
{
- _ = type ?? throw new ArgumentNullException(nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
//Get delegate argument types incase of a method overload
Type[] delegateArgs = typeof(TDelegate).GetMethod("Invoke")!
diff --git a/lib/Utils/src/VnEncoding.cs b/lib/Utils/src/VnEncoding.cs
index ffdebae..5509955 100644
--- a/lib/Utils/src/VnEncoding.cs
+++ b/lib/Utils/src/VnEncoding.cs
@@ -55,7 +55,7 @@ namespace VNLib.Utils
/// <returns>A <see cref="Stream"/> contating the encoded data</returns>
public static VnMemoryStream GetMemoryStream(ReadOnlySpan<char> data, Encoding encoding, IUnmangedHeap? heap = null)
{
- _ = encoding ?? throw new ArgumentNullException(nameof(encoding));
+ ArgumentNullException.ThrowIfNull(encoding);
//Assign default heap if not specified
heap ??= MemoryUtil.Shared;