1 #pragma warning disable 1591
4 using System.Runtime.InteropServices;
5 using System.Collections.Generic;
7 using static Eina.TraitFunctions;
8 using static Eina.ArrayNativeFunctions;
12 public static class ArrayNativeFunctions
14 [DllImport(efl.Libs.Eina)] public static extern IntPtr
15 eina_array_new(uint step);
16 [DllImport(efl.Libs.Eina)] public static extern void
17 eina_array_free(IntPtr array);
18 [DllImport(efl.Libs.Eina)] public static extern void
19 eina_array_flush(IntPtr array);
20 [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
21 eina_array_remove(IntPtr array, IntPtr keep, IntPtr gdata);
22 [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
23 eina_array_push(IntPtr array, IntPtr data);
25 [DllImport(efl.Libs.Eina)] public static extern IntPtr
26 eina_array_iterator_new(IntPtr array);
27 [DllImport(efl.Libs.Eina)] public static extern IntPtr
28 eina_array_accessor_new(IntPtr array);
30 [DllImport(efl.Libs.CustomExports)] public static extern void
31 eina_array_clean_custom_export_mono(IntPtr array);
32 [DllImport(efl.Libs.CustomExports)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
33 eina_array_push_custom_export_mono(IntPtr array, IntPtr data);
34 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
35 eina_array_pop_custom_export_mono(IntPtr array);
36 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
37 eina_array_data_get_custom_export_mono(IntPtr array, uint idx);
38 [DllImport(efl.Libs.CustomExports)] public static extern void
39 eina_array_data_set_custom_export_mono(IntPtr array, uint idx, IntPtr data);
40 [DllImport(efl.Libs.CustomExports)] public static extern uint
41 eina_array_count_custom_export_mono(IntPtr array);
43 [DllImport(efl.Libs.CustomExports)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
44 eina_array_foreach_custom_export_mono(IntPtr array, IntPtr cb, IntPtr fdata);
47 public class Array<T> : IEnumerable<T>, IDisposable
49 public static uint DefaultStep = 32;
51 public IntPtr Handle {get;set;} = IntPtr.Zero;
52 public bool Own {get;set;}
53 public bool OwnContent {get;set;}
57 get { return Count(); }
61 private void InitNew(uint step)
63 Handle = eina_array_new(step);
66 if (Handle == IntPtr.Zero)
67 throw new SEHException("Could not alloc array");
70 internal bool InternalPush(IntPtr ele)
72 return eina_array_push_custom_export_mono(Handle, ele);
75 internal IntPtr InternalPop()
77 return eina_array_pop_custom_export_mono(Handle);
80 internal IntPtr InternalDataGet(int idx)
82 return eina_array_data_get_custom_export_mono(Handle, (uint)idx); // TODO: Check bounds ???
85 internal void InternalDataSet(int idx, IntPtr ele)
87 eina_array_data_set_custom_export_mono(Handle, (uint)idx, ele); // TODO: Check bounds ???
95 public Array(uint step)
100 public Array(IntPtr handle, bool own)
107 public Array(IntPtr handle, bool own, bool ownContent)
111 OwnContent = ownContent;
119 protected virtual void Dispose(bool disposing)
122 Handle = IntPtr.Zero;
123 if (h == IntPtr.Zero)
128 int len = (int)eina_array_count_custom_export_mono(h);
129 for(int i = 0; i < len; ++i)
131 NativeFree<T>(eina_array_data_get_custom_export_mono(h, (uint)i));
139 public void Dispose()
142 GC.SuppressFinalize(this);
150 public IntPtr Release()
153 Handle = IntPtr.Zero;
157 private void FreeElementsIfOwned()
162 for(int i = 0; i < len; ++i)
164 NativeFree<T>(InternalDataGet(i));
171 FreeElementsIfOwned();
172 eina_array_clean_custom_export_mono(Handle);
177 FreeElementsIfOwned();
178 eina_array_flush(Handle);
183 return (int) eina_array_count_custom_export_mono(Handle);
186 public void SetOwnership(bool ownAll)
192 public void SetOwnership(bool own, bool ownContent)
195 OwnContent = ownContent;
198 public bool Push(T val)
200 IntPtr ele = ManagedToNativeAlloc(val);
201 var r = InternalPush(ele);
208 // public void Add(T val)
216 IntPtr ele = InternalPop();
217 var r = NativeToManaged<T>(ele);
218 if (OwnContent && ele != IntPtr.Zero)
223 public T DataGet(int idx)
225 IntPtr ele = InternalDataGet(idx);
226 return NativeToManaged<T>(ele);
234 public void DataSet(int idx, T val)
236 IntPtr ele = InternalDataGet(idx); // TODO: check bondaries ??
237 if (OwnContent && ele != IntPtr.Zero)
239 ele = ManagedToNativeAlloc(val);
240 InternalDataSet(idx, ele);
243 public T this[int idx]
258 var managed = new T[len];
259 for(int i = 0; i < len; ++i)
261 managed[i] = DataGet(i);
266 public bool Append(T[] values)
268 foreach(T v in values)
275 public Eina.Iterator<T> GetIterator()
277 return new Eina.Iterator<T>(eina_array_iterator_new(Handle), true, false);
280 public IEnumerator<T> GetEnumerator()
283 for(int i = 0; i < len; ++i)
285 yield return DataGet(i);
289 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
291 return this.GetEnumerator();
294 /// <summary> Gets an Accessor for this Array.</summary>
295 public Eina.Accessor<T> GetAccessor()
297 return new Eina.Accessor<T>(eina_array_accessor_new(Handle), Ownership.Managed);