summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.Interop/Interop/Helpers/NativeList.cs
blob: b63789943fd0592583d72d1b48f951dca9d78ed1 (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
126
127
128
129
130
131
132
133
134
135
// --------------------------------------------------------------------------------------------------------------------
// <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.Interop.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    using HandBrake.Interop.Interop.HbLib;
    using HandBrake.Interop.Interop.HbLib.Wrappers.Interfaces;
    using HandBrake.Interop.Interop.Providers;
    using HandBrake.Interop.Interop.Providers.Interfaces;

    /// <summary>
    /// Represents a HandBrake style native list.
    /// </summary>
    internal class NativeList : IDisposable
    {
        private static IHbFunctions hbFunctions;

        /// <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;
            IHbFunctionsProvider hbFunctionsProvider = new HbFunctionsProvider();
            hbFunctions = hbFunctionsProvider.GetHbFunctionsWrapper();
        }
        
        /// <summary>
        /// The list of native memory locations allocated for this list.
        /// </summary>
        private readonly List<IntPtr> allocatedMemory = new List<IntPtr>();

        /// <summary>
        /// Gets 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
            {
                Debug.WriteLine("Got a Zero Pointer in the NativeList");
                return this.Ptr == IntPtr.Zero ? 0 : 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 this.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()
        {
            IHbFunctionsProvider hbFunctionsProvider = new HbFunctionsProvider();
            return new NativeList(hbFunctionsProvider.GetHbFunctionsWrapper().hb_list_init());
        }
    }
}