forked from enlightenment/efl
efl_mono: Initial support for Accessors.
Summary: Plain conversion to IEnumerable, which is the base of LINQ Depends on D6189 Reviewers: felipealmeida, vitor.sousa Reviewed By: vitor.sousa Subscribers: cedric, #committers, zmike Tags: #efl Differential Revision: https://phab.enlightenment.org/D6190
This commit is contained in:
parent
2bbaada66e
commit
cfafd01bbe
|
@ -23,6 +23,7 @@ efl_eina_mono_files = \
|
|||
bindings/mono/eina_mono/eina_error.cs \
|
||||
bindings/mono/eina_mono/eina_value.cs \
|
||||
bindings/mono/eina_mono/eina_promises.cs \
|
||||
bindings/mono/eina_mono/eina_accessor.cs \
|
||||
bindings/mono/eina_mono/eina_strbuf.cs
|
||||
|
||||
efl_eldbus_mono_files = \
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using static eina.TraitFunctions;
|
||||
|
||||
using static eina.AccessorNativeFunctions;
|
||||
|
||||
namespace eina {
|
||||
|
||||
internal class AccessorNativeFunctions
|
||||
{
|
||||
[DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
|
||||
eina_accessor_data_get(IntPtr accessor, uint position, IntPtr data);
|
||||
[DllImport(efl.Libs.Eina)] public static extern void
|
||||
eina_accessor_free(IntPtr accessor);
|
||||
}
|
||||
|
||||
/// <summary>Accessors provide an uniform way of accessing Eina containers, similar to C++ STL's and C# IEnumerable.</summary>
|
||||
public class Accessor<T> : IEnumerable<T>, IDisposable
|
||||
{
|
||||
/// <summary>Pointer to the native accessor.</summary>
|
||||
public IntPtr Handle { get; private set; } = IntPtr.Zero;
|
||||
|
||||
/// <summary>Who is in charge of releasing the resources wrapped by this instance.</summary>
|
||||
private Ownership Ownership { get; set; }
|
||||
|
||||
/// <summary>Create a new accessor wrapping the given pointer.</summary>
|
||||
public Accessor(IntPtr handle, Ownership owner=Ownership.Managed)
|
||||
{
|
||||
Handle = handle;
|
||||
Ownership = owner;
|
||||
}
|
||||
|
||||
/// <summary>Release the native resources held by this instance.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (Ownership == Ownership.Managed && Handle != IntPtr.Zero)
|
||||
{
|
||||
eina_accessor_free(Handle);
|
||||
Handle = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
~Accessor()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public virtual T Convert(IntPtr data)
|
||||
{
|
||||
return NativeToManaged<T>(data);
|
||||
}
|
||||
|
||||
/// <summary>Returns an enumerator that iterates throught this accessor.</summary>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
if (Handle == IntPtr.Zero)
|
||||
throw new ObjectDisposedException(base.GetType().Name);
|
||||
IntPtr tmp = MemoryNative.Alloc(Marshal.SizeOf(typeof(IntPtr)));
|
||||
uint position = 0;
|
||||
|
||||
try
|
||||
{
|
||||
while(eina_accessor_data_get(Handle, position, tmp))
|
||||
{
|
||||
IntPtr data = (IntPtr)Marshal.PtrToStructure(tmp, typeof(IntPtr));
|
||||
yield return Convert(data);
|
||||
position += 1;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
MemoryNative.Free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public class AccessorInList<T> : Accessor<T>
|
||||
{
|
||||
public AccessorInList(IntPtr handle, Ownership own): base(handle, own) {}
|
||||
public override T Convert(IntPtr data)
|
||||
{
|
||||
return NativeToManagedInlistNode<T>(data);
|
||||
}
|
||||
}
|
||||
|
||||
public class AccessorInArray<T> : Accessor<T>
|
||||
{
|
||||
public AccessorInArray(IntPtr handle, Ownership own): base(handle, own) {}
|
||||
public override T Convert(IntPtr data)
|
||||
{
|
||||
return NativeToManagedInplace<T>(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -24,6 +24,8 @@ public static class ArrayNativeFunctions
|
|||
|
||||
[DllImport(efl.Libs.Eina)] public static extern IntPtr
|
||||
eina_array_iterator_new(IntPtr array);
|
||||
[DllImport(efl.Libs.Eina)] public static extern IntPtr
|
||||
eina_array_accessor_new(IntPtr array);
|
||||
|
||||
[DllImport(efl.Libs.CustomExports)] public static extern void
|
||||
eina_array_clean_custom_export_mono(IntPtr array);
|
||||
|
@ -288,6 +290,12 @@ public class Array<T> : IEnumerable<T>, IDisposable
|
|||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary> Gets an Accessor for this Array.</summary>
|
||||
public eina.Accessor<T> GetAccessor()
|
||||
{
|
||||
return new eina.Accessor<T>(eina_array_accessor_new(Handle), Ownership.Managed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -321,6 +321,12 @@ public class Inarray<T> : IEnumerable<T>, IDisposable
|
|||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary> Gets an Accessor for this Array.</summary>
|
||||
public eina.Accessor<T> GetAccessor()
|
||||
{
|
||||
return new eina.AccessorInArray<T>(eina_inarray_accessor_new(Handle), Ownership.Managed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -313,6 +313,12 @@ public class Inlist<T> : IEnumerable<T>, IDisposable
|
|||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary> Gets an Accessor for this List.</summary>
|
||||
public eina.Accessor<T> GetAccessor()
|
||||
{
|
||||
return new eina.AccessorInList<T>(eina_inlist_accessor_new(Handle), Ownership.Managed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -349,6 +349,12 @@ public class List<T> : IEnumerable<T>, IDisposable
|
|||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary> Gets an Accessor for this List.</summary>
|
||||
public eina.Accessor<T> GetAccessor()
|
||||
{
|
||||
return new eina.Accessor<T>(eina_list_accessor_new(Handle), Ownership.Managed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4254,4 +4254,75 @@ class TestEinaIterator
|
|||
}
|
||||
} // < TestEinaIterator
|
||||
|
||||
|
||||
class TestEinaAccessor
|
||||
{
|
||||
public static void basic_accessor_list()
|
||||
{
|
||||
var lst = new eina.List<int>();
|
||||
lst.Append(1);
|
||||
lst.Append(2);
|
||||
lst.Append(3);
|
||||
lst.Append(4);
|
||||
|
||||
eina.Accessor<int> accessor = lst.GetAccessor();
|
||||
|
||||
var zipped = accessor.Zip(lst, (first, second) => new Tuple<int, int>(first, second));
|
||||
|
||||
foreach(Tuple<int, int> pair in zipped)
|
||||
{
|
||||
Test.AssertEquals(pair.Item1, pair.Item2);
|
||||
}
|
||||
}
|
||||
|
||||
public static void basic_accessor_array()
|
||||
{
|
||||
var arr = new eina.Array<string>();
|
||||
arr.Append(base_seq_str);
|
||||
|
||||
eina.Accessor<string> accessor = arr.GetAccessor();
|
||||
|
||||
var zipped = accessor.Zip(arr, (first, second) => new Tuple<string, string>(first, second));
|
||||
|
||||
foreach(Tuple<string, string> pair in zipped)
|
||||
{
|
||||
Test.AssertEquals(pair.Item1, pair.Item2);
|
||||
}
|
||||
}
|
||||
|
||||
public static void basic_accessor_inlist()
|
||||
{
|
||||
var lst = new eina.Inlist<int>();
|
||||
lst.Append(1);
|
||||
lst.Append(2);
|
||||
lst.Append(3);
|
||||
lst.Append(4);
|
||||
|
||||
eina.Accessor<int> accessor = lst.GetAccessor();
|
||||
|
||||
var zipped = accessor.Zip(lst, (first, second) => new Tuple<int, int>(first, second));
|
||||
|
||||
foreach(Tuple<int, int> pair in zipped)
|
||||
{
|
||||
Test.AssertEquals(pair.Item1, pair.Item2);
|
||||
}
|
||||
}
|
||||
|
||||
public static void basic_accessor_inarray()
|
||||
{
|
||||
var arr = new eina.Inarray<int>();
|
||||
arr.Append(base_seq_int);
|
||||
|
||||
eina.Accessor<int> accessor = arr.GetAccessor();
|
||||
|
||||
var zipped = accessor.Zip(arr, (first, second) => new Tuple<int, int>(first, second));
|
||||
|
||||
foreach(Tuple<int, int> pair in zipped)
|
||||
{
|
||||
Test.AssertEquals(pair.Item1, pair.Item2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue