summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eina_mono/eina_inlist.cs
blob: 7e1cb60f3d2ca35d7a70803caab4f1e9ebc79bfb (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

using static eina.TraitFunctions;
using static eina.InlistNativeFunctions;
using eina.Callbacks;

namespace eina {

public static class InlistNativeFunctions
{
    [DllImport("eina")] public static extern IntPtr
        eina_inlist_append(IntPtr in_list, IntPtr in_item);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_prepend(IntPtr in_list, IntPtr in_item);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_append_relative(IntPtr in_list, IntPtr in_item, IntPtr in_relative);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_prepend_relative(IntPtr in_list, IntPtr in_item, IntPtr in_relative);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_remove(IntPtr in_list, IntPtr in_item);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_find(IntPtr in_list, IntPtr in_item);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_promote(IntPtr list, IntPtr item);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_demote(IntPtr list, IntPtr item);

    [DllImport("eina")] public static extern uint
        eina_inlist_count(IntPtr list);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_iterator_new(IntPtr in_list);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_accessor_new(IntPtr in_list);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_sorted_insert(IntPtr list, IntPtr item, IntPtr func);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_sorted_state_new(void);

    [DllImport("eina")] public static extern int
        eina_inlist_sorted_state_init(IntPtr state, IntPtr list);

    [DllImport("eina")] public static extern void
        eina_inlist_sorted_state_free(IntPtr state);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_sorted_state_insert(IntPtr list, IntPtr item, IntPtr func, IntPtr state);

    [DllImport("eina")] public static extern IntPtr
        eina_inlist_sort(IntPtr head, IntPtr func);

// static inline IntPtr eina_inlist_first(IntPtr list);
// static inline IntPtr eina_inlist_last(IntPtr list);
}

public class Inlist<T> : IEnumerable<T>, IDisposable
{
    public IntPtr Handle {get;set;} = IntPtr.Zero;
    public bool Own {get;set;}
    public bool OwnContent {get;set;}

    public int Length
    {
        get { return Count(); }
    }


    private void InitNew()
    {
        Handle = IntPtr.Zero;
        Own = true;
        OwnContent = true;
    }

    private IntPtr InternalLast()
    {
        return eina_inlist_last_custom_export_mono(Handle);
    }

    private static IntPtr InternalNext(IntPtr inlist)
    {
        return eina_inlist_next_custom_export_mono(inlist);
    }

    private static IntPtr InternalPrev(IntPtr inlist)
    {
        return eina_inlist_prev_custom_export_mono(inlist);
    }

    private static IntPtr InternalDataGet(IntPtr inlist)
    {
        return eina_inlist_data_get_custom_export_mono(inlist);
    }

    private static IntPtr InternalDataSet(IntPtr inlist, IntPtr data)
    {
        return eina_inlist_data_set_custom_export_mono(inlist, data);
    }


    public Inlist()
    {
        InitNew();
    }

    public Inlist(IntPtr handle, bool own)
    {
        Handle = handle;
        Own = own;
        OwnContent = own;
    }

    public Inlist(IntPtr handle, bool own, bool ownContent)
    {
        Handle = handle;
        Own = own;
        OwnContent = ownContent;
    }

    ~Inlist()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        IntPtr h = Handle;
        Handle = IntPtr.Zero;
        if (h == IntPtr.Zero)
            return;

        if (OwnContent)
        {
            for(IntPtr curr = h; curr != IntPtr.Zero; curr = InternalNext(curr))
            {
                NativeFree<T>(InternalDataGet(curr));
            }
        }

        if (Own)
            eina_inlist_free(h);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void Free()
    {
        Dispose();
    }

    public IntPtr Release()
    {
        IntPtr h = Handle;
        Handle = IntPtr.Zero;
        return h;
    }

    public void SetOwnership(bool ownAll)
    {
        Own = ownAll;
        OwnContent = ownAll;
    }

    public void SetOwnership(bool own, bool ownContent)
    {
        Own = own;
        OwnContent = ownContent;
    }

    public int Count()
    {
        return (int) eina_inlist_count_custom_export_mono(Handle);
    }

    public void Append(T val)
    {
        IntPtr ele = ManagedToNativeAlloc(val);
        Handle = eina_inlist_append(Handle, ele);
    }

    public void Prepend(T val)
    {
        IntPtr ele = ManagedToNativeAlloc(val);
        Handle = eina_inlist_prepend(Handle, ele);
    }

    public void SortedInsert(T val)
    {
        IntPtr ele = ManagedToNativeAlloc(val);
        Handle = eina_inlist_sorted_insert(Handle, EinaCompareCb<T>(), ele);
    }

    public void SortedInsert(Eina_Compare_Cb compareCb, T val)
    {
        IntPtr ele = ManagedToNativeAlloc(val);
        Handle = eina_inlist_sorted_insert(Handle, Marshal.GetFunctionPointerForDelegate(compareCb), ele);
    }

    public void Sort(int limit = 0)
    {
        Handle = eina_inlist_sort(Handle, (uint)limit, EinaCompareCb<T>());
    }

    public void Sort(Eina_Compare_Cb compareCb)
    {
        Handle = eina_inlist_sort(Handle, 0, Marshal.GetFunctionPointerForDelegate(compareCb));
    }

    public void Sort(int limit, Eina_Compare_Cb compareCb)
    {
        Handle = eina_inlist_sort(Handle, (uint)limit, Marshal.GetFunctionPointerForDelegate(compareCb));
    }

    public T Nth(int n)
    {
        // TODO: check bounds ???
        IntPtr ele = eina_inlist_nth(Handle, (uint)n);
        return NativeToManaged<T>(ele);
    }

    public void DataSet(int idx, T val)
    {
        IntPtr pos = eina_inlist_nth_inlist(Handle, (uint)idx);
        if (pos == IntPtr.Zero)
            throw new IndexOutOfRangeException();
        if (OwnContent)
            NativeFree<T>(InternalDataGet(pos));
        IntPtr ele = ManagedToNativeAlloc(val);
        InternalDataSet(pos, ele);
    }

    public T this[int idx]
    {
        get
        {
            return Nth(idx);
        }
        set
        {
            DataSet(idx, value);
        }
    }

    public T LastDataGet()
    {
        IntPtr ele = eina_inlist_last_data_get_custom_export_mono(Handle);
        return NativeToManaged<T>(ele);
    }

    public void Reverse()
    {
        Handle = eina_inlist_reverse(Handle);
    }

    public void Shuffle()
    {
        Handle = eina_inlist_shuffle(Handle, IntPtr.Zero);
    }

    public T[] ToArray()
    {
        var managed = new T[Count()];
        int i = 0;
        for(IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr), ++i)
        {
            managed[i] = NativeToManaged<T>(InternalDataGet(curr));
        }
        return managed;
    }

    public void AppendArray(T[] values)
    {
        foreach (T v in values)
            Append(v);
    }

    public IEnumerator<T> GetEnumerator()
    {
        for(IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr))
        {
            yield return NativeToManaged<T>(InternalDataGet(curr));
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

}