checkpoint
This commit is contained in:
44
Alpm/EnumerableWrapper`3.cs
Normal file
44
Alpm/EnumerableWrapper`3.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
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
|
||||
{
|
||||
private TImpl _impl;
|
||||
|
||||
public EnumerableWrapper(TImpl impl)
|
||||
{
|
||||
_impl = impl;
|
||||
}
|
||||
|
||||
public IEnumerator<TElement> GetEnumerator()
|
||||
{
|
||||
var handle = _impl.Handle;
|
||||
var release = false;
|
||||
|
||||
RuntimeHelpers.PrepareConstrainedRegions();
|
||||
try
|
||||
{
|
||||
handle.DangerousAddRef(ref release);
|
||||
if (!release) throw new ObjectDisposedException(_impl.GetType().FullName);
|
||||
for (var list = _impl.GetItems(); list != IntPtr.Zero; list = Wrapper.ListNext(list))
|
||||
{
|
||||
yield return _impl.PtrToItem(Wrapper.ListData(list));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (release)
|
||||
handle.DangerousRelease();
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user