summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousasilva@gmail.com>2017-06-26 17:33:47 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2017-07-13 18:34:55 -0300
commit64f88ce0a4f45aa78e29cf3a7f12cbfaef7f60f7 (patch)
tree920540b0ef485f658dad477991310bd2a116f56d
parentb3d6d544d3a456c84500fd3cfca43dd556da149e (diff)
eina_mono: initial implementation of eina.Iterator binding (missing tests & generation)
-rw-r--r--src/Makefile_Efl_Mono.am1
-rw-r--r--src/bindings/mono/eina_mono/eina_iterator.cs144
2 files changed, 145 insertions, 0 deletions
diff --git a/src/Makefile_Efl_Mono.am b/src/Makefile_Efl_Mono.am
index 5586e4dcad..b3363b442b 100644
--- a/src/Makefile_Efl_Mono.am
+++ b/src/Makefile_Efl_Mono.am
@@ -134,6 +134,7 @@ efl_eina_mono_files = \
134 bindings/mono/eina_mono/eina_list.cs \ 134 bindings/mono/eina_mono/eina_list.cs \
135 bindings/mono/eina_mono/eina_inarray.cs \ 135 bindings/mono/eina_mono/eina_inarray.cs \
136 bindings/mono/eina_mono/eina_inlist.cs \ 136 bindings/mono/eina_mono/eina_inlist.cs \
137 bindings/mono/eina_mono/eina_iterator.cs \
137 bindings/mono/eina_mono/eina_container_common.cs \ 138 bindings/mono/eina_mono/eina_container_common.cs \
138 bindings/mono/eina_mono/eina_common.cs \ 139 bindings/mono/eina_mono/eina_common.cs \
139 bindings/mono/eina_mono/eina_log.cs \ 140 bindings/mono/eina_mono/eina_log.cs \
diff --git a/src/bindings/mono/eina_mono/eina_iterator.cs b/src/bindings/mono/eina_mono/eina_iterator.cs
new file mode 100644
index 0000000000..3f370e69a6
--- /dev/null
+++ b/src/bindings/mono/eina_mono/eina_iterator.cs
@@ -0,0 +1,144 @@
1using System;
2using System.Runtime.InteropServices;
3using System.Collections.Generic;
4
5using static eina.TraitFunctions;
6using static eina.IteratorNativeFunctions;
7
8namespace eina {
9
10public static class IteratorNativeFunctions
11{
12 [DllImport("eina")] public static extern void
13 eina_iterator_free(IntPtr iterator);
14 [DllImport("eina")] public static extern IntPtr
15 eina_iterator_container_get(IntPtr iterator);
16 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
17 eina_iterator_next(IntPtr iterator, out IntPtr data);
18 [DllImport("eina")] public static extern void
19 eina_iterator_foreach(IntPtr iterator, IntPtr callback, IntPtr fdata);
20 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
21 eina_iterator_lock(IntPtr iterator);
22 [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
23 eina_iterator_unlock(IntPtr iterator);
24
25 [DllImport("eina")] public static extern IntPtr
26 eina_carray_iterator_new(IntPtr array);
27}
28
29public class Iterator<T> : IEnumerable<T>, IDisposable
30{
31 public IntPtr Handle {get;set;} = IntPtr.Zero;
32 public bool Own {get;set;} = true;
33 public bool OwnContent {get;set;} = false;
34
35 public Iterator(IntPtr handle, bool own)
36 {
37 Handle = handle;
38 Own = own;
39 OwnContent = own;
40 }
41
42 public Iterator(IntPtr handle, bool own, bool ownContent)
43 {
44 Handle = handle;
45 Own = own;
46 OwnContent = ownContent;
47 }
48
49 ~Iterator()
50 {
51 Dispose(false);
52 }
53
54 protected virtual void Dispose(bool disposing)
55 {
56 var h = Handle;
57 Handle = IntPtr.Zero;
58 if (h == IntPtr.Zero)
59 return;
60
61 if (OwnContent)
62 {
63 for(IntPtr data; eina_iterator_next(h, out data);)
64 {
65 NativeFree<T>(data);
66 }
67 }
68
69 if (Own)
70 eina_iterator_free(h);
71 }
72
73 public void Dispose()
74 {
75 Dispose(true);
76 GC.SuppressFinalize(this);
77 }
78
79 public void Free()
80 {
81 Dispose();
82 }
83
84 public IntPtr Release()
85 {
86 IntPtr h = Handle;
87 Handle = IntPtr.Zero;
88 return h;
89 }
90
91 public void SetOwnership(bool ownAll)
92 {
93 Own = ownAll;
94 OwnContent = ownAll;
95 }
96
97 public void SetOwnership(bool own, bool ownContent)
98 {
99 Own = own;
100 OwnContent = ownContent;
101 }
102
103 public bool Next(out T res)
104 {
105 IntPtr data;
106 if (!eina_iterator_next(Handle, out data))
107 {
108 res = default(T);
109 return false;
110 }
111
112 res = NativeToManaged<T>(data);
113
114 if (OwnContent)
115 NativeFree<T>(data);
116
117 return true;
118 }
119
120 public bool Lock()
121 {
122 return eina_iterator_lock(Handle);
123 }
124
125 public bool Unlock()
126 {
127 return eina_iterator_unlock(Handle);
128 }
129
130 public IEnumerator<T> GetEnumerator()
131 {
132 for(T curr; Next(out curr);)
133 {
134 yield return curr;
135 }
136 }
137
138 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
139 {
140 return this.GetEnumerator();
141 }
142}
143
144}