// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // Represents a HandBrake style native list. // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrake.ApplicationServices.Interop.Helpers { using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using HandBrake.ApplicationServices.Interop.HbLib; /// /// Represents a HandBrake style native list. /// internal class NativeList : IDisposable { /// /// Initializes a new instance of the NativeList class. /// /// The pointer to use for the list. public NativeList(IntPtr listPtr) { this.Ptr = listPtr; } /// /// The list of native memory locations allocated for this list. /// private readonly List allocatedMemory = new List(); /// /// Gets the pointer to the native list. /// public IntPtr Ptr { get; private set; } /// /// Gets the number of items in the list. /// public int Count { get { Debug.WriteLine("Got a Zero Pointer in the NativeList"); return this.Ptr == IntPtr.Zero ? 0 : HBFunctions.hb_list_count(this.Ptr); } } /// /// Gets the list of native memory locations allocated for this list. /// public List AllocatedMemory { get { return this.allocatedMemory; } } /// /// Adds an item to the end of the list. /// /// The item to add. public void Add(IntPtr item) { HBFunctions.hb_list_add(this.Ptr, item); } /// /// Inserts an item into the list. /// /// The index to insert the item at. /// The item to insert. public void Insert(int position, IntPtr item) { HBFunctions.hb_list_insert(this.Ptr, position, item); } /// /// Removes an item from the list. /// /// The item to remove. public void Remove(IntPtr item) { HBFunctions.hb_list_rem(this.Ptr, item); } /// /// Gets an item out of the list. /// /// Index in the list. /// The item at that index in the list. public IntPtr this[int i] { get { return HBFunctions.hb_list_item(this.Ptr, i); } } /// /// Disposes resources associated with this object. /// public void Dispose() { IntPtr listPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); Marshal.WriteIntPtr(listPtrPtr, this.Ptr); HBFunctions.hb_list_close(listPtrPtr); Marshal.FreeHGlobal(listPtrPtr); } /// /// Creates a new list in unmanaged memory. /// /// The created list. public static NativeList CreateList() { return new NativeList(HBFunctions.hb_list_init()); } } }