Files
DotNetAlpm/Alpm/Wrapper.cs
2020-04-29 23:47:06 -04:00

46 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Foodsoft.Alpm
{
internal static class Wrapper
{
internal static unsafe IntPtr ListNext(IntPtr list) => ((alpm_list_t*) list)->next;
internal static unsafe IntPtr ListData(IntPtr list) => ((alpm_list_t*) list)->data;
internal static TResult UseHandle<TResult>(this SafeHandle handle, IntPtr ptr, Func<IntPtr, TResult> op)
{
var release = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
handle.DangerousAddRef(ref release);
if (!release) throw new ObjectDisposedException(handle.GetType().FullName);
return op(ptr);
}
finally
{
if (release)
handle.DangerousRelease();
}
}
internal static TResult UseHandle<TResult>(this SafeHandle handle, Func<IntPtr, TResult> op) =>
handle.UseHandle(handle.DangerousGetHandle(), op);
internal static void SetStringCollection(IEnumerable<string> value, SafeAlpmHandle safeAlpmHandle, Func<string, int> op)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var s in value)
{
if (op(s) < 0)
throw new AlpmException(safeAlpmHandle);
}
}
}
}