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(this SafeHandle handle, IntPtr ptr, Func 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(this SafeHandle handle, Func op) => handle.UseHandle(handle.DangerousGetHandle(), op); internal static void SetStringCollection(IEnumerable value, SafeAlpmHandle safeAlpmHandle, Func op) { // ReSharper disable once LoopCanBeConvertedToQuery foreach (var s in value) { if (op(s) < 0) throw new AlpmException(safeAlpmHandle); } } } }