summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.Interop/HandBrakeInterop/NativeList.cs
blob: 1e24f9ce40e811cca17e89540c92326e8c5a8c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NativeList.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   Represents a HandBrake style native list.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrake.Interop
{
	using System;
	using System.Collections.Generic;
	using System.Runtime.InteropServices;

	using HandBrake.Interop.HbLib;

	/// <summary>
	/// Represents a HandBrake style native list.
	/// </summary>
	public class NativeList : IDisposable
	{
		/// <summary>
		/// Initializes a new instance of the NativeList class.
		/// </summary>
		/// <param name="listPtr">The pointer to use for the list.</param>
		public NativeList(IntPtr listPtr)
		{
			this.Ptr = listPtr;
		}

		/// <summary>
		/// The list of native memory locations allocated for this list.
		/// </summary>
		private List<IntPtr> allocatedMemory = new List<IntPtr>();

		/// <summary>
		/// Gets or sets the pointer to the native list.
		/// </summary>
		public IntPtr Ptr { get; private set; }

		/// <summary>
		/// Gets the number of items in the list.
		/// </summary>
		public int Count
		{
			get
			{
				return HBFunctions.hb_list_count(this.Ptr);
			}
		}

		/// <summary>
		/// Gets the list of native memory locations allocated for this list.
		/// </summary>
		public List<IntPtr> AllocatedMemory
		{
			get
			{
				return allocatedMemory;
			}
		}

		/// <summary>
		/// Adds an item to the end of the list.
		/// </summary>
		/// <param name="item">The item to add.</param>
		public void Add(IntPtr item)
		{
			HBFunctions.hb_list_add(this.Ptr, item);
		}

		/// <summary>
		/// Inserts an item into the list.
		/// </summary>
		/// <param name="position">The index to insert the item at.</param>
		/// <param name="item">The item to insert.</param>
		public void Insert(int position, IntPtr item)
		{
			HBFunctions.hb_list_insert(this.Ptr, position, item);
		}

		/// <summary>
		/// Removes an item from the list.
		/// </summary>
		/// <param name="item">The item to remove.</param>
		public void Remove(IntPtr item)
		{
			HBFunctions.hb_list_rem(this.Ptr, item);
		}

		/// <summary>
		/// Gets an item out of the list.
		/// </summary>
		/// <param name="i">Index in the list.</param>
		/// <returns>The item at that index in the list.</returns>
		public IntPtr this[int i]
		{
			get
			{
				return HBFunctions.hb_list_item(this.Ptr, i);
			}
		}

		/// <summary>
		/// Disposes resources associated with this object.
		/// </summary>
		public void Dispose()
		{
			IntPtr listPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
			Marshal.WriteIntPtr(listPtrPtr, this.Ptr);
			HBFunctions.hb_list_close(listPtrPtr);
			Marshal.FreeHGlobal(listPtrPtr);
		}

		/// <summary>
		/// Creates a new list in unmanaged memory.
		/// </summary>
		/// <returns>The created list.</returns>
		public static NativeList CreateList()
		{
			return new NativeList(HBFunctions.hb_list_init());
		}
	}
}