79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Foodsoft.Alpm;
|
|
|
|
namespace Foodsoft.Alpm
|
|
{
|
|
internal struct CollectionWrapper<TImpl, TElement> : ICollection<TElement> where TImpl : struct,
|
|
IAlpmItemsAccessor<TElement>
|
|
{
|
|
private TImpl _impl;
|
|
|
|
public CollectionWrapper(TImpl impl)
|
|
{
|
|
_impl = impl;
|
|
}
|
|
|
|
IEnumerator<TElement> IEnumerable<TElement>.GetEnumerator()
|
|
{
|
|
return (IEnumerator<TElement>) GetEnumerator();
|
|
}
|
|
|
|
public IEnumerator GetEnumerator()
|
|
{
|
|
var result = new List<TElement>();
|
|
var intPtr = _impl.GetItems();
|
|
unsafe
|
|
{
|
|
for (var list = (alpm_list_t*) intPtr; list != null; list = alpm.alpm_list_next(list))
|
|
{
|
|
result.Add(_impl.PtrToItem(list->data));
|
|
}
|
|
}
|
|
|
|
return result.GetEnumerator();
|
|
}
|
|
|
|
|
|
public void Add(TElement item)
|
|
{
|
|
_impl.AddItem(item);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_impl.SetItems(IntPtr.Zero);
|
|
}
|
|
|
|
|
|
public bool Contains(TElement item) =>
|
|
_impl.FindItem(item) != IntPtr.Zero;
|
|
|
|
public void CopyTo(TElement[] array, int arrayIndex)
|
|
{
|
|
var intPtr = _impl.GetItems();
|
|
unsafe
|
|
{
|
|
for (var list = (alpm_list_t*) intPtr; list != null; list = alpm.alpm_list_next(list))
|
|
{
|
|
array[arrayIndex++] = _impl.PtrToItem(list->data);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Remove(TElement item)
|
|
{
|
|
var err = _impl.RemoveItem(item);
|
|
if (err < 0)
|
|
{
|
|
throw new Exception(alpm.alpm_errno(_impl.SafeAlpmHandle));
|
|
}
|
|
|
|
return err == 0;
|
|
}
|
|
|
|
public int Count => (int) alpm.alpm_list_count(_impl.GetItems());
|
|
public bool IsReadOnly => false;
|
|
}
|
|
} |