using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Vanara.PInvoke; namespace Vanara.InteropServices { /// An abstract base class for a safe, unmanaged list of structures allocated by a memory scheme. /// The type of the list elements. /// The type of memory allocation to use. public abstract class SafeNativeListBase : SafeMemoryHandle, IReadOnlyList where TElem : struct where TMem : IMemoryMethods, new() { /// Initializes a new instance of the class. /// The handle. /// The size of memory allocated to the handle, in bytes. /// if set to true if this class is responsible for freeing the memory on disposal. protected SafeNativeListBase(IntPtr ptr, SizeT size, bool ownsHandle) : base(ptr, size, ownsHandle) { } /// Initializes a new instance of the class. /// The number of bytes to allocate for this new array. public SafeNativeListBase(SizeT byteCount) : base(byteCount) { } /// Gets the number of elements contained in the . public virtual int Count => IsInvalid ? 0 : Items.Count(); /// Enumerates the elements. /// An enumeration of values from the pointer. protected abstract IEnumerable Items { get; } /// Gets or sets the value at the specified index. /// The value. /// The index. /// /// index or index public virtual TElem this[int index] => Items.ElementAt(index); /// Determines whether this instance contains the object. /// The object to locate in the . /// /// true if is found in the ; otherwise, false. /// public virtual bool Contains(TElem item) => Items.Contains(item); /// Returns an enumerator that iterates through the collection. /// A that can be used to iterate through the collection. public virtual IEnumerator GetEnumerator() => Items.GetEnumerator(); /// Returns an enumerator that iterates through a collection. /// An object that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } }