using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Foodsoft.Alpm { internal static class Detail { 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); } } internal static void WrapError(SafeAlpmHandle alpmHandle, Func f) { var err = f(); if (err != 0) throw new AlpmException(alpmHandle); } internal delegate int OutFunc(THandle handle, out T result); internal static T WrapError(SafeAlpmHandle alpmHandle, THandle handle, OutFunc f) { var err = f(handle, out var result); if (err != 0) throw new AlpmException(alpmHandle); return result; } internal static bool WrapErrorBool(SafeAlpmHandle h, Func f) { var err = f(); if (err < 0) { throw new AlpmException(h); } return err == 0; } } }