Skip to content

Commit

Permalink
Add throw helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jzebedee committed Sep 19, 2023
1 parent 5ef4dca commit 7f3c9fc
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/LibDeflate/Imports/libdeflate_options.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace LibDeflate.Imports;

Expand All @@ -19,23 +20,29 @@ internal readonly struct libdeflate_options
public libdeflate_options(malloc_func malloc, free_func free)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(malloc);
ArgumentNullException.ThrowIfNull(free);
ArgumentNullException.ThrowIfNull(malloc);
ArgumentNullException.ThrowIfNull(free);
#else
//TODO: add throwhelpers
if (malloc is null)
{
throw new ArgumentNullException(nameof(malloc));
}

if (free is null)
{
throw new ArgumentNullException(nameof(free));
}
ThrowIfNull(malloc);
ThrowIfNull(free);
#endif

this.sizeof_options = Size;
this.malloc = malloc;
this.free = free;

#if !NET6_0_OR_GREATER
static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if(argument is null)
{
ThrowHelperArgumentNull(paramName!);
}

[DoesNotReturn]
static void ThrowHelperArgumentNull(string paramName) => throw new ArgumentNullException(paramName);
}
#endif
}

/// <summary>
Expand Down

0 comments on commit 7f3c9fc

Please sign in to comment.