checkpoint

This commit is contained in:
2020-04-29 23:47:06 -04:00
parent 64f7dd4b7d
commit 19a9fc06ba
31 changed files with 678 additions and 360 deletions

View File

@@ -1,17 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Foodsoft.Alpm
{
internal struct EnumerableWrapper<TImpl, TElement, THandle> : IEnumerable<TElement> where TImpl : struct,
IItemsReader<TElement, THandle>
where THandle : SafeHandle
internal static class EnumerableWrapper<TElement>
{
internal static EnumerableWrapper<TElement, TImpl> Create<TImpl>(TImpl impl)
where TImpl : struct, ICollectionImpl<TElement>
{
return new EnumerableWrapper<TElement, TImpl>(impl);
}
}
internal struct EnumerableWrapper<TElement, TImpl> : ICollection<TElement> where TImpl : struct,
ICollectionImpl<TElement>
{
private TImpl _impl;
public static EnumerableWrapper<TElement, TImpl> Create(TImpl impl)
{
return new EnumerableWrapper<TElement, TImpl>(impl);
}
public EnumerableWrapper(TImpl impl)
{
_impl = impl;
@@ -40,5 +54,67 @@ namespace Foodsoft.Alpm
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public int Count
{
get
{
var impl = _impl;
return impl.Handle.UseHandle((_) => (int) alpm.alpm_list_count(impl.GetItems()));
}
}
public bool Contains(TElement item)
{
var impl = _impl;
switch (item)
{
case string s:
return impl.Handle.UseHandle((_) => alpm.alpm_list_find_str(impl.GetItems(), s) != (IntPtr.Zero));
default:
var comparer = EqualityComparer<TElement>.Default;
return _impl.Handle.UseHandle((_) =>
{
for (var list = impl.GetItems(); list != IntPtr.Zero; list = Wrapper.ListNext(list))
{
if (comparer.Equals(item, impl.PtrToItem(Wrapper.ListData(list))))
return true;
}
return false;
});
}
}
public void CopyTo(TElement[] array, int arrayIndex)
{
var impl = _impl;
impl.Handle.UseHandle((_) =>
{
for (var list = impl.GetItems(); list != IntPtr.Zero; list = Wrapper.ListNext(list))
{
array[arrayIndex++] = impl.PtrToItem(Wrapper.ListData(list));
}
return arrayIndex;
});
}
public void Add(TElement item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Remove(TElement item)
{
throw new NotSupportedException();
}
public bool IsReadOnly => true;
}
}