diff options
author | Vitor Sousa <vitorsousasilva@gmail.com> | 2017-06-26 17:39:43 -0300 |
---|---|---|
committer | Vitor Sousa <vitorsousasilva@gmail.com> | 2017-06-26 17:40:48 -0300 |
commit | 72a1ab32fb031cdf76b281e18d90de1cff83c04b (patch) | |
tree | 2a356c16e65735e6b61c9b548b6d3b906a5591b2 | |
parent | 356cfc5a48b1d670a516dcedb200aff4876bb46a (diff) | |
download | efl-72a1ab32fb031cdf76b281e18d90de1cff83c04b.tar.gz |
eina_mono: get eina.Iterator for the Eina containers (plus IEnumerable implementation for eina.Hash)
-rw-r--r-- | src/bindings/mono/eina_mono/eina_array.cs | 9 | ||||
-rw-r--r-- | src/bindings/mono/eina_mono/eina_hash.cs | 60 | ||||
-rw-r--r-- | src/bindings/mono/eina_mono/eina_inarray.cs | 10 | ||||
-rw-r--r-- | src/bindings/mono/eina_mono/eina_inlist.cs | 6 | ||||
-rw-r--r-- | src/bindings/mono/eina_mono/eina_list.cs | 11 |
5 files changed, 82 insertions, 14 deletions
diff --git a/src/bindings/mono/eina_mono/eina_array.cs b/src/bindings/mono/eina_mono/eina_array.cs index a6166e04f8..4fff37d09e 100644 --- a/src/bindings/mono/eina_mono/eina_array.cs +++ b/src/bindings/mono/eina_mono/eina_array.cs @@ -20,6 +20,9 @@ public static class ArrayNativeFunctions [DllImport("eina")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool eina_array_push(IntPtr array, IntPtr data); + [DllImport("eina")] public static extern IntPtr + eina_array_iterator_new(IntPtr array); + [DllImport("eflcustomexportsmono")] public static extern void eina_array_free_generic_custom_export_mono(IntPtr array); [DllImport("eflcustomexportsmono")] public static extern void @@ -270,6 +273,12 @@ public class Array<T> : IEnumerable<T>, IDisposable return true; } + + public eina.Iterator<T> GetIterator() + { + return new eina.Iterator<T>(eina_array_iterator_new(Handle), true, false); + } + public IEnumerator<T> GetEnumerator() { int len = Length; diff --git a/src/bindings/mono/eina_mono/eina_hash.cs b/src/bindings/mono/eina_mono/eina_hash.cs index 6922ea739c..48b0d4e867 100644 --- a/src/bindings/mono/eina_mono/eina_hash.cs +++ b/src/bindings/mono/eina_mono/eina_hash.cs @@ -3,12 +3,21 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using static eina.TraitFunctions; +using static eina.IteratorNativeFunctions; using static eina.HashNativeFunctions; using eina.Callbacks; namespace eina { +[StructLayout(LayoutKind.Sequential)] +public struct HashTupleNative +{ + public IntPtr key; + public IntPtr data; + public uint key_length; +} + public static class HashNativeFunctions { [DllImport("eina")] public static extern IntPtr @@ -92,6 +101,15 @@ public static class HashNativeFunctions [DllImport("eina")] public static extern IntPtr eina_hash_modify_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data); + [DllImport("eina")] public static extern IntPtr + eina_hash_iterator_key_new(IntPtr hash); + + [DllImport("eina")] public static extern IntPtr + eina_hash_iterator_data_new(IntPtr hash); + + [DllImport("eina")] public static extern IntPtr + eina_hash_iterator_tuple_new(IntPtr hash); + [DllImport("eina")] public static extern void eina_hash_foreach(IntPtr hash, IntPtr func, IntPtr fdata); @@ -107,7 +125,7 @@ public static class HashNativeFunctions eina_hash_superfast(string key, int len); } -public class Hash<TKey, TValue> : IDisposable +public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDisposable { public IntPtr Handle {get; set;} = IntPtr.Zero; public bool Own {get; set;} @@ -342,19 +360,33 @@ public class Hash<TKey, TValue> : IDisposable return eina_hash_population(Handle); } -// TODO: implement when iterator is ready -// public IEnumerator<KeyValuePair<TKey, TValue> > 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(); -// } + public eina.Iterator<TKey> GetKeyIterator() + { + return new eina.Iterator<TKey>(eina_hash_iterator_key_new(Handle), true, false); + } + + public eina.Iterator<TValue> GetValueIterator() + { + return new eina.Iterator<TValue>(eina_hash_iterator_data_new(Handle), true, false); + } + + public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() + { + IntPtr itr = eina_hash_iterator_tuple_new(Handle); + for (IntPtr tuplePtr; !eina_iterator_next(itr, out tuplePtr);) + { + var tuple = Marshal.PtrToStructure<eina.HashTupleNative>(tuplePtr); + var key = NativeToManaged<TKey>(tuple.key); + var val = NativeToManaged<TValue>(tuple.data); + yield return new KeyValuePair<TKey, TValue>(key, val); + } + eina_iterator_free(itr); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } } } diff --git a/src/bindings/mono/eina_mono/eina_inarray.cs b/src/bindings/mono/eina_mono/eina_inarray.cs index ef3acc1d62..966bd9d097 100644 --- a/src/bindings/mono/eina_mono/eina_inarray.cs +++ b/src/bindings/mono/eina_mono/eina_inarray.cs @@ -296,6 +296,16 @@ public class Inarray<T> : IEnumerable<T>, IDisposable return true; } + public eina.Iterator<T> GetIterator() + { + return new eina.Iterator<T>(eina_inarray_iterator_new(Handle), true, false); + } + + public eina.Iterator<T> GetReversedIterator() + { + return new eina.Iterator<T>(eina_inarray_iterator_reversed_new(Handle), true, false); + } + public IEnumerator<T> GetEnumerator() { int len = Length; diff --git a/src/bindings/mono/eina_mono/eina_inlist.cs b/src/bindings/mono/eina_mono/eina_inlist.cs index b95a5f0dc3..ab5468e903 100644 --- a/src/bindings/mono/eina_mono/eina_inlist.cs +++ b/src/bindings/mono/eina_mono/eina_inlist.cs @@ -290,6 +290,12 @@ public class Inlist<T> : IEnumerable<T>, IDisposable Append(v); } + + public eina.Iterator<T> GetIterator() + { + return new eina.Iterator<T>(eina_inlist_iterator_new(Handle), true, false); + } + public IEnumerator<T> GetEnumerator() { for(IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr)) diff --git a/src/bindings/mono/eina_mono/eina_list.cs b/src/bindings/mono/eina_mono/eina_list.cs index 7abd8bb99c..afb10dfe47 100644 --- a/src/bindings/mono/eina_mono/eina_list.cs +++ b/src/bindings/mono/eina_mono/eina_list.cs @@ -324,6 +324,17 @@ public class List<T> : IEnumerable<T>, IDisposable Append(v); } + + public eina.Iterator<T> GetIterator() + { + return new eina.Iterator<T>(eina_list_iterator_new(Handle), true, false); + } + + public eina.Iterator<T> GetReversedIterator() + { + return new eina.Iterator<T>(eina_list_iterator_reversed_new(Handle), true, false); + } + public IEnumerator<T> GetEnumerator() { for(IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr)) |