summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousasilva@gmail.com>2017-06-06 13:21:26 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2017-07-13 18:25:44 -0300
commit715f0fd87b203e67e355e1560c1fa6e43e1b8569 (patch)
tree2f682d3fd7891863c9e4b9c85f6cc34bfd7bb758
parent1cab32dc8b2e2339cb7ddc063004dfafa7dd9ed3 (diff)
eina_mono: WIP 'In'Containers
-rw-r--r--src/bindings/mono/eina_mono/eina_container_common.cs38
-rw-r--r--src/bindings/mono/eina_mono/eina_inarray.cs289
-rw-r--r--src/bindings/mono/eina_mono/eina_inlist.cs307
3 files changed, 634 insertions, 0 deletions
diff --git a/src/bindings/mono/eina_mono/eina_container_common.cs b/src/bindings/mono/eina_mono/eina_container_common.cs
index c317b1876a..67ee4d6748 100644
--- a/src/bindings/mono/eina_mono/eina_container_common.cs
+++ b/src/bindings/mono/eina_mono/eina_container_common.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
4 4
5using eina.Callbacks; 5using eina.Callbacks;
6using static eina.HashNativeFunctions; 6using static eina.HashNativeFunctions;
7using static eina.InarrayNativeFunctions;
7using static eina.NativeCustomExportFunctions; 8using static eina.NativeCustomExportFunctions;
8 9
9namespace eina { 10namespace eina {
@@ -37,6 +38,22 @@ public struct ConvertWrapper<T>
37 public T Val {get;set;} 38 public T Val {get;set;}
38} 39}
39 40
41[StructLayout(LayoutKind.Sequential)]
42public struct InlistMem
43{
44 public IntPtr next {get;set;}
45 public IntPtr prev {get;set;}
46 public IntPtr last {get;set;}
47}
48
49[StructLayout(LayoutKind.Sequential)]
50public struct InlistNode<T>
51{
52 public InlistMem __in_list {get;set;}
53 public T Val {get;set;}
54}
55
56
40public interface IBaseElementTraits<T> 57public interface IBaseElementTraits<T>
41{ 58{
42 IntPtr ManagedToNativeAlloc(T man); 59 IntPtr ManagedToNativeAlloc(T man);
@@ -47,6 +64,7 @@ public interface IBaseElementTraits<T>
47 IntPtr EinaCompareCb(); 64 IntPtr EinaCompareCb();
48 IntPtr EinaFreeCb(); 65 IntPtr EinaFreeCb();
49 IntPtr EinaHashNew(); 66 IntPtr EinaHashNew();
67 IntPtr EinaInarrayNew(uint step);
50} 68}
51 69
52public class StringElementTraits<T> : IBaseElementTraits<T> 70public class StringElementTraits<T> : IBaseElementTraits<T>
@@ -94,6 +112,11 @@ public class StringElementTraits<T> : IBaseElementTraits<T>
94 { 112 {
95 return eina_hash_string_superfast_new(IntPtr.Zero); 113 return eina_hash_string_superfast_new(IntPtr.Zero);
96 } 114 }
115
116 public IntPtr EinaInarrayNew(uint step)
117 {
118 return eina_inarray_new(Marshal.SizeOf<IntPtr>(), step);
119 }
97} 120}
98 121
99public class EflObjectElementTraits<T> : IBaseElementTraits<T> 122public class EflObjectElementTraits<T> : IBaseElementTraits<T>
@@ -157,6 +180,11 @@ public class EflObjectElementTraits<T> : IBaseElementTraits<T>
157 { 180 {
158 return eina_hash_pointer_new(IntPtr.Zero); 181 return eina_hash_pointer_new(IntPtr.Zero);
159 } 182 }
183
184 public IntPtr EinaInarrayNew(uint step)
185 {
186 return eina_inarray_new(Marshal.SizeOf<IntPtr>(), step);
187 }
160} 188}
161 189
162public abstract class PrimitiveElementTraits<T> 190public abstract class PrimitiveElementTraits<T>
@@ -206,6 +234,11 @@ public abstract class PrimitiveElementTraits<T>
206 { 234 {
207 return efl_mono_native_free_addr_get(); 235 return efl_mono_native_free_addr_get();
208 } 236 }
237
238 public IntPtr EinaInarrayNew(uint step)
239 {
240 return eina_inarray_new(Marshal.SizeOf<T>(), step);
241 }
209} 242}
210 243
211public class Primitive32ElementTraits<T> : PrimitiveElementTraits<T>, IBaseElementTraits<T> 244public class Primitive32ElementTraits<T> : PrimitiveElementTraits<T>, IBaseElementTraits<T>
@@ -382,6 +415,11 @@ public static class TraitFunctions
382 { 415 {
383 return GetTypeTraits<TKey>().EinaHashNew(); 416 return GetTypeTraits<TKey>().EinaHashNew();
384 } 417 }
418
419 public static IntPtr EinaInarrayNew<T>(uint step)
420 {
421 return GetTypeTraits<T>().EinaInarrayNew(step);
422 }
385} 423}
386 424
387} 425}
diff --git a/src/bindings/mono/eina_mono/eina_inarray.cs b/src/bindings/mono/eina_mono/eina_inarray.cs
new file mode 100644
index 0000000000..e92f4afbba
--- /dev/null
+++ b/src/bindings/mono/eina_mono/eina_inarray.cs
@@ -0,0 +1,289 @@
1using System;
2using System.Runtime.InteropServices;
3using System.Collections.Generic;
4
5using static eina.TraitFunctions;
6using static eina.InarrayNativeFunctions;
7
8namespace eina {
9
10public static class InarrayNativeFunctions
11{
12 [DllImport("eina")] public static extern IntPtr
13 eina_inarray_new(uint member_size, uint step);
14 [DllImport("eina")] public static extern void
15 eina_inarray_free(IntPtr array);
16 [DllImport("eina")] public static extern void
17 eina_inarray_step_set(IntPtr array, uint sizeof_eina_inarray, uint member_size, uint step);
18 [DllImport("eina")] public static extern void
19 eina_inarray_flush(IntPtr array);
20 [DllImport("eina")] public static extern int
21 eina_inarray_push(IntPtr array, IntPtr data);
22 [DllImport("eina")] public static extern IntPtr
23 eina_inarray_grow(IntPtr array, uint size);
24 [DllImport("eina")] public static extern int
25 eina_inarray_insert(IntPtr array, IntPtr data, IntPtr compare);
26 [DllImport("eina")] public static extern int
27 eina_inarray_insert_sorted(IntPtr array, IntPtr data, IntPtr compare);
28 [DllImport("eina")] public static extern int
29 eina_inarray_remove(IntPtr array, IntPtr data);
30 [DllImport("eina")] public static extern IntPtr
31 eina_inarray_pop(IntPtr array);
32 [DllImport("eina")] public static extern IntPtr
33 eina_inarray_nth(IntPtr array, uint position);
34 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
35 eina_inarray_insert_at(IntPtr array, uint position, IntPtr data);
36 [DllImport("eina")] public static extern IntPtr
37 eina_inarray_alloc_at(IntPtr array, uint position, uint member_count);
38 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
39 eina_inarray_replace_at(IntPtr array, uint position, IntPtr data);
40 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
41 eina_inarray_remove_at(IntPtr array, uint position);
42 [DllImport("eina")] public static extern void
43 eina_inarray_reverse(IntPtr array);
44 [DllImport("eina")] public static extern void
45 eina_inarray_sort(IntPtr array, IntPtr compare);
46 [DllImport("eina")] public static extern int
47 eina_inarray_search(IntPtr array, IntPtr data, IntPtr compare);
48 [DllImport("eina")] public static extern int
49 eina_inarray_search_sorted(IntPtr array, IntPtr data, IntPtr compare);
50 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
51 eina_inarray_foreach(IntPtr array, IntPtr function, IntPtr user_data);
52 [DllImport("eina")] public static extern int
53 eina_inarray_foreach_remove(IntPtr array, IntPtr match, IntPtr user_data);
54 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
55 eina_inarray_resize(IntPtr array, uint new_size);
56 [DllImport("eina")] public static extern uint
57 eina_inarray_count(IntPtr array);
58
59 [DllImport("eina")] public static extern IntPtr
60 eina_inarray_iterator_new(IntPtr array);
61 [DllImport("eina")] public static extern IntPtr
62 eina_inarray_iterator_reversed_new(IntPtr array);
63 [DllImport("eina")] public static extern IntPtr
64 eina_inarray_accessor_new(IntPtr array);
65}
66
67public class Inarray<T> : IEnumerable<T>, IDisposable
68{
69 public static uint DefaultStep = 0;
70
71 public IntPtr Handle {get;set;} = IntPtr.Zero;
72 public bool Own {get;set;}
73 public bool OwnContent {get;set;}
74
75 public int Length
76 {
77 get { return Count(); }
78 }
79
80
81 private void InitNew(uint step)
82 {
83 Handle = EinaInarrayNew<T>(step);
84 Own = true;
85 OwnContent = true;
86 if (Handle == IntPtr.Zero)
87 throw new SEHException("Could not alloc inarray");
88 }
89
90 public Inarray()
91 {
92 InitNew(DefaultStep);
93 }
94
95 public Inarray(uint step)
96 {
97 InitNew(step);
98 }
99
100 public Inarray(IntPtr handle, bool own)
101 {
102 Handle = handle;
103 Own = own;
104 OwnContent = own;
105 }
106
107 public Inarray(IntPtr handle, bool own, bool ownContent)
108 {
109 Handle = handle;
110 Own = own;
111 OwnContent = ownContent;
112 }
113
114 ~Inarray()
115 {
116 Dispose(false);
117 }
118
119 protected virtual void Dispose(bool disposing)
120 {
121 IntPtr h = Handle;
122 Handle = IntPtr.Zero;
123 if (h == IntPtr.Zero)
124 return;
125
126 if (OwnContent)
127 {
128 uint len = eina_inarray_count(h);
129 for(uint i = 0; i < len; ++i)
130 {
131 NativeFreeInplace<T>(eina_inarray_nth(h, i));
132 }
133 }
134
135 if (Own)
136 eina_inarray_free(h);
137 }
138
139 public void Dispose()
140 {
141 Dispose(true);
142 GC.SuppressFinalize(this);
143 }
144
145 public void Free()
146 {
147 Dispose();
148 }
149
150 public IntPtr Release()
151 {
152 IntPtr h = Handle;
153 Handle = IntPtr.Zero;
154 return h;
155 }
156
157 private void FreeElementsIfOwned()
158 {
159 if (OwnContent)
160 {
161 int len = Length;
162 for (int i = 0; i < len; ++i)
163 {
164 NativeFreeInplace<T>(eina_inarray_nth(h, i));
165 }
166 }
167 }
168
169 public void Clean()
170 {
171 FreeElementsIfOwned();
172 eina_inarray_clean_custom_export_mono(Handle);
173 }
174
175 public void Flush()
176 {
177 FreeElementsIfOwned();
178 eina_inarray_flush(Handle);
179 }
180
181 public int Count()
182 {
183 return (int) eina_inarray_count_custom_export_mono(Handle);
184 }
185
186 public void SetOwnership(bool ownAll)
187 {
188 Own = ownAll;
189 OwnContent = ownAll;
190 }
191
192 public void SetOwnership(bool own, bool ownContent)
193 {
194 Own = own;
195 OwnContent = ownContent;
196 }
197
198 public bool Push(T val)
199 {
200 IntPtr ele = ManagedToNativeAlloc(val);
201 var r = InternalPush(ele);
202 if (!r)
203 NativeFree<T>(ele);
204 return r;
205 }
206
207// TODO ???
208// public void Add(T val)
209// {
210// if (!Push(val))
211// throw;
212// }
213
214 public T Pop()
215 {
216 IntPtr ele = InternalPop();
217 var r = NativeToManaged<T>(ele);
218 if (OwnContent && ele != IntPtr.Zero)
219 NativeFree<T>(ele);
220 return r;
221 }
222
223 public T DataGet(int idx)
224 {
225 IntPtr ele = InternalDataGet(idx);
226 return NativeToManaged<T>(ele);
227 }
228
229 public T At(int idx)
230 {
231 return DataGet(idx);
232 }
233
234 public void DataSet(int idx, T val)
235 {
236 IntPtr ele = InternalDataGet(idx); // TODO: check bondaries ??
237 if (OwnContent && ele != IntPtr.Zero)
238 NativeFree<T>(ele);
239 ele = ManagedToNativeAlloc(val);
240 InternalDataSet(idx, ele);
241 }
242
243 public T this[int idx]
244 {
245 get
246 {
247 return DataGet(idx);
248 }
249 set
250 {
251 DataSet(idx, value);
252 }
253 }
254
255 public T[] ToArray()
256 {
257 int len = Length;
258 var managed = new T[len];
259 for(int i = 0; i < len; ++i)
260 {
261 managed[i] = DataGet(i);
262 }
263 return managed;
264 }
265
266 public bool Append(T[] values)
267 {
268 foreach(T v in values)
269 if (!Push(v))
270 return false;
271 return true;
272 }
273
274 public IEnumerator<T> GetEnumerator()
275 {
276 int len = Length;
277 for(int i = 0; i < len; ++i)
278 {
279 yield return DataGet(i);
280 }
281 }
282
283 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
284 {
285 return this.GetEnumerator();
286 }
287}
288
289}
diff --git a/src/bindings/mono/eina_mono/eina_inlist.cs b/src/bindings/mono/eina_mono/eina_inlist.cs
new file mode 100644
index 0000000000..7e1cb60f3d
--- /dev/null
+++ b/src/bindings/mono/eina_mono/eina_inlist.cs
@@ -0,0 +1,307 @@
1using System;
2using System.Runtime.InteropServices;
3using System.Collections.Generic;
4
5using static eina.TraitFunctions;
6using static eina.InlistNativeFunctions;
7using eina.Callbacks;
8
9namespace eina {
10
11public static class InlistNativeFunctions
12{
13 [DllImport("eina")] public static extern IntPtr
14 eina_inlist_append(IntPtr in_list, IntPtr in_item);
15
16 [DllImport("eina")] public static extern IntPtr
17 eina_inlist_prepend(IntPtr in_list, IntPtr in_item);
18
19 [DllImport("eina")] public static extern IntPtr
20 eina_inlist_append_relative(IntPtr in_list, IntPtr in_item, IntPtr in_relative);
21
22 [DllImport("eina")] public static extern IntPtr
23 eina_inlist_prepend_relative(IntPtr in_list, IntPtr in_item, IntPtr in_relative);
24
25 [DllImport("eina")] public static extern IntPtr
26 eina_inlist_remove(IntPtr in_list, IntPtr in_item);
27
28 [DllImport("eina")] public static extern IntPtr
29 eina_inlist_find(IntPtr in_list, IntPtr in_item);
30
31 [DllImport("eina")] public static extern IntPtr
32 eina_inlist_promote(IntPtr list, IntPtr item);
33
34 [DllImport("eina")] public static extern IntPtr
35 eina_inlist_demote(IntPtr list, IntPtr item);
36
37 [DllImport("eina")] public static extern uint
38 eina_inlist_count(IntPtr list);
39
40 [DllImport("eina")] public static extern IntPtr
41 eina_inlist_iterator_new(IntPtr in_list);
42
43 [DllImport("eina")] public static extern IntPtr
44 eina_inlist_accessor_new(IntPtr in_list);
45
46 [DllImport("eina")] public static extern IntPtr
47 eina_inlist_sorted_insert(IntPtr list, IntPtr item, IntPtr func);
48
49 [DllImport("eina")] public static extern IntPtr
50 eina_inlist_sorted_state_new(void);
51
52 [DllImport("eina")] public static extern int
53 eina_inlist_sorted_state_init(IntPtr state, IntPtr list);
54
55 [DllImport("eina")] public static extern void
56 eina_inlist_sorted_state_free(IntPtr state);
57
58 [DllImport("eina")] public static extern IntPtr
59 eina_inlist_sorted_state_insert(IntPtr list, IntPtr item, IntPtr func, IntPtr state);
60
61 [DllImport("eina")] public static extern IntPtr
62 eina_inlist_sort(IntPtr head, IntPtr func);
63
64// static inline IntPtr eina_inlist_first(IntPtr list);
65// static inline IntPtr eina_inlist_last(IntPtr list);
66}
67
68public class Inlist<T> : IEnumerable<T>, IDisposable
69{
70 public IntPtr Handle {get;set;} = IntPtr.Zero;
71 public bool Own {get;set;}
72 public bool OwnContent {get;set;}
73
74 public int Length
75 {
76 get { return Count(); }
77 }
78
79
80 private void InitNew()
81 {
82 Handle = IntPtr.Zero;
83 Own = true;
84 OwnContent = true;
85 }
86
87 private IntPtr InternalLast()
88 {
89 return eina_inlist_last_custom_export_mono(Handle);
90 }
91
92 private static IntPtr InternalNext(IntPtr inlist)
93 {
94 return eina_inlist_next_custom_export_mono(inlist);
95 }
96
97 private static IntPtr InternalPrev(IntPtr inlist)
98 {
99 return eina_inlist_prev_custom_export_mono(inlist);
100 }
101
102 private static IntPtr InternalDataGet(IntPtr inlist)
103 {
104 return eina_inlist_data_get_custom_export_mono(inlist);
105 }
106
107 private static IntPtr InternalDataSet(IntPtr inlist, IntPtr data)
108 {
109 return eina_inlist_data_set_custom_export_mono(inlist, data);
110 }
111
112
113 public Inlist()
114 {
115 InitNew();
116 }
117
118 public Inlist(IntPtr handle, bool own)
119 {
120 Handle = handle;
121 Own = own;
122 OwnContent = own;
123 }
124
125 public Inlist(IntPtr handle, bool own, bool ownContent)
126 {
127 Handle = handle;
128 Own = own;
129 OwnContent = ownContent;
130 }
131
132 ~Inlist()
133 {
134 Dispose(false);
135 }
136
137 protected virtual void Dispose(bool disposing)
138 {
139 IntPtr h = Handle;
140 Handle = IntPtr.Zero;
141 if (h == IntPtr.Zero)
142 return;
143
144 if (OwnContent)
145 {
146 for(IntPtr curr = h; curr != IntPtr.Zero; curr = InternalNext(curr))
147 {
148 NativeFree<T>(InternalDataGet(curr));
149 }
150 }
151
152 if (Own)
153 eina_inlist_free(h);
154 }
155
156 public void Dispose()
157 {
158 Dispose(true);
159 GC.SuppressFinalize(this);
160 }
161
162 public void Free()
163 {
164 Dispose();
165 }
166
167 public IntPtr Release()
168 {
169 IntPtr h = Handle;
170 Handle = IntPtr.Zero;
171 return h;
172 }
173
174 public void SetOwnership(bool ownAll)
175 {
176 Own = ownAll;
177 OwnContent = ownAll;
178 }
179
180 public void SetOwnership(bool own, bool ownContent)
181 {
182 Own = own;
183 OwnContent = ownContent;
184 }
185
186 public int Count()
187 {
188 return (int) eina_inlist_count_custom_export_mono(Handle);
189 }
190
191 public void Append(T val)
192 {
193 IntPtr ele = ManagedToNativeAlloc(val);
194 Handle = eina_inlist_append(Handle, ele);
195 }
196
197 public void Prepend(T val)
198 {
199 IntPtr ele = ManagedToNativeAlloc(val);
200 Handle = eina_inlist_prepend(Handle, ele);
201 }
202
203 public void SortedInsert(T val)
204 {
205 IntPtr ele = ManagedToNativeAlloc(val);
206 Handle = eina_inlist_sorted_insert(Handle, EinaCompareCb<T>(), ele);
207 }
208
209 public void SortedInsert(Eina_Compare_Cb compareCb, T val)
210 {
211 IntPtr ele = ManagedToNativeAlloc(val);
212 Handle = eina_inlist_sorted_insert(Handle, Marshal.GetFunctionPointerForDelegate(compareCb), ele);
213 }
214
215 public void Sort(int limit = 0)
216 {
217 Handle = eina_inlist_sort(Handle, (uint)limit, EinaCompareCb<T>());
218 }
219
220 public void Sort(Eina_Compare_Cb compareCb)
221 {
222 Handle = eina_inlist_sort(Handle, 0, Marshal.GetFunctionPointerForDelegate(compareCb));
223 }
224
225 public void Sort(int limit, Eina_Compare_Cb compareCb)
226 {
227 Handle = eina_inlist_sort(Handle, (uint)limit, Marshal.GetFunctionPointerForDelegate(compareCb));
228 }
229
230 public T Nth(int n)
231 {
232 // TODO: check bounds ???
233 IntPtr ele = eina_inlist_nth(Handle, (uint)n);
234 return NativeToManaged<T>(ele);
235 }
236
237 public void DataSet(int idx, T val)
238 {
239 IntPtr pos = eina_inlist_nth_inlist(Handle, (uint)idx);
240 if (pos == IntPtr.Zero)
241 throw new IndexOutOfRangeException();
242 if (OwnContent)
243 NativeFree<T>(InternalDataGet(pos));
244 IntPtr ele = ManagedToNativeAlloc(val);
245 InternalDataSet(pos, ele);
246 }
247
248 public T this[int idx]
249 {
250 get
251 {
252 return Nth(idx);
253 }
254 set
255 {
256 DataSet(idx, value);
257 }
258 }
259
260 public T LastDataGet()
261 {
262 IntPtr ele = eina_inlist_last_data_get_custom_export_mono(Handle);
263 return NativeToManaged<T>(ele);
264 }
265
266 public void Reverse()
267 {
268 Handle = eina_inlist_reverse(Handle);
269 }
270
271 public void Shuffle()
272 {
273 Handle = eina_inlist_shuffle(Handle, IntPtr.Zero);
274 }
275
276 public T[] ToArray()
277 {
278 var managed = new T[Count()];
279 int i = 0;
280 for(IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr), ++i)
281 {
282 managed[i] = NativeToManaged<T>(InternalDataGet(curr));
283 }
284 return managed;
285 }
286
287 public void AppendArray(T[] values)
288 {
289 foreach (T v in values)
290 Append(v);
291 }
292
293 public IEnumerator<T> GetEnumerator()
294 {
295 for(IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr))
296 {
297 yield return NativeToManaged<T>(InternalDataGet(curr));
298 }
299 }
300
301 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
302 {
303 return this.GetEnumerator();
304 }
305}
306
307}