csharp: Document Eina.List

Summary:
Also added Since 1.23 info

Depends on D10253o

ref T8293

Test Plan: Check docs

Reviewers: segfaultxavi, brunobelo, felipealmeida

Reviewed By: felipealmeida

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Maniphest Tasks: T8293

Differential Revision: https://phab.enlightenment.org/D10254
This commit is contained in:
Lauro Moura 2019-09-30 23:10:40 -03:00 committed by Felipe Magno de Almeida
parent 74e79b5ae2
commit 465b049d5b
1 changed files with 67 additions and 0 deletions

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using static Eina.TraitFunctions; using static Eina.TraitFunctions;
using static Eina.ListNativeFunctions; using static Eina.ListNativeFunctions;
@ -11,6 +12,7 @@ using Eina.Callbacks;
namespace Eina namespace Eina
{ {
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ListNativeFunctions public static class ListNativeFunctions
{ {
[DllImport(efl.Libs.Eina)] public static extern IntPtr [DllImport(efl.Libs.Eina)] public static extern IntPtr
@ -104,16 +106,22 @@ public static class ListNativeFunctions
eina_list_last_data_get_custom_export_mono(IntPtr list); eina_list_last_data_get_custom_export_mono(IntPtr list);
} }
/// <summary>Native wrapper around a linked list of items. (Since EFL 1.23)</summary>
public class List<T> : IEnumerable<T>, IDisposable public class List<T> : IEnumerable<T>, IDisposable
{ {
[EditorBrowsable(EditorBrowsableState.Never)]
public IntPtr Handle {get;set;} = IntPtr.Zero; public IntPtr Handle {get;set;} = IntPtr.Zero;
/// <summary>Whether this managed list owns the native one.</summary>
public bool Own {get;set;} public bool Own {get;set;}
/// <summary>Whether the native list wrapped owns the content it points to.</summary>
public bool OwnContent {get;set;} public bool OwnContent {get;set;}
/// <summary>Delegate for comparing two elements of this list.</summary> /// <summary>Delegate for comparing two elements of this list.</summary>
/// <returns>-1, 0 or 1 for respectively smaller, equal or larger.</returns> /// <returns>-1, 0 or 1 for respectively smaller, equal or larger.</returns>
public delegate int Compare(T a, T b); public delegate int Compare(T a, T b);
/// <summary>The number of elements on this list.</summary>
public int Length public int Length
{ {
get { return Count(); } get { return Count(); }
@ -153,11 +161,14 @@ public class List<T> : IEnumerable<T>, IDisposable
} }
/// <summary>Creates a new empty list.</summary>
public List() public List()
{ {
InitNew(); InitNew();
} }
/// <summary>Creates a new list wrapping the given handle.</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public List(IntPtr handle, bool own) public List(IntPtr handle, bool own)
{ {
Handle = handle; Handle = handle;
@ -165,6 +176,8 @@ public class List<T> : IEnumerable<T>, IDisposable
OwnContent = own; OwnContent = own;
} }
/// <summary>Creates a new list wrapping the given handle.</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public List(IntPtr handle, bool own, bool ownContent) public List(IntPtr handle, bool own, bool ownContent)
{ {
Handle = handle; Handle = handle;
@ -172,11 +185,15 @@ public class List<T> : IEnumerable<T>, IDisposable
OwnContent = ownContent; OwnContent = ownContent;
} }
/// <summary>Finalizes this list.</summary>
~List() ~List()
{ {
Dispose(false); Dispose(false);
} }
/// <summary>Disposes of this list.</summary>
/// <param name="disposing">Whether this was called from the finalizer (<c>false</c>) or from the
/// <see cref="Dispose()"/> method.</param>
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
IntPtr h = Handle; IntPtr h = Handle;
@ -207,17 +224,21 @@ public class List<T> : IEnumerable<T>, IDisposable
} }
} }
/// <summary>Disposes of this list.</summary>
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
/// <summary>Disposes of this list.</summary>
public void Free() public void Free()
{ {
Dispose(); Dispose();
} }
/// <summary>Relinquishes of the native list.</summary>
/// <returns>The previously wrapped native list handle.</returns>
public IntPtr Release() public IntPtr Release()
{ {
IntPtr h = Handle; IntPtr h = Handle;
@ -225,57 +246,77 @@ public class List<T> : IEnumerable<T>, IDisposable
return h; return h;
} }
/// <summary>Sets whether this wrapper should own the native list or not.</summary>
public void SetOwnership(bool ownAll) public void SetOwnership(bool ownAll)
{ {
Own = ownAll; Own = ownAll;
OwnContent = ownAll; OwnContent = ownAll;
} }
/// <summary>Sets whether this wrapper should own the native list and its content or not.</summary>
public void SetOwnership(bool own, bool ownContent) public void SetOwnership(bool own, bool ownContent)
{ {
Own = own; Own = own;
OwnContent = ownContent; OwnContent = ownContent;
} }
/// <summary>Returns the number of elements in this list.</summary>
public int Count() public int Count()
{ {
return (int)eina_list_count_custom_export_mono(Handle); return (int)eina_list_count_custom_export_mono(Handle);
} }
/// <summary>Appends <c>val</c> to the list.</summary>
/// <param name="val">The item to be appended.</param>
public void Append(T val) public void Append(T val)
{ {
IntPtr ele = ManagedToNativeAlloc(val); IntPtr ele = ManagedToNativeAlloc(val);
Handle = eina_list_append(Handle, ele); Handle = eina_list_append(Handle, ele);
} }
/// <summary>Prepends <c>val</c> to the list.</summary>
/// <param name="val">The item to be prepended.</param>
public void Prepend(T val) public void Prepend(T val)
{ {
IntPtr ele = ManagedToNativeAlloc(val); IntPtr ele = ManagedToNativeAlloc(val);
Handle = eina_list_prepend(Handle, ele); Handle = eina_list_prepend(Handle, ele);
} }
/// <summary>Inserts <c>val</c> in the list in a sorted manner. It presumes the list is already sorted.</summary>
/// <param name="val">The item to be inserted.</param>
public void SortedInsert(T val) public void SortedInsert(T val)
{ {
IntPtr ele = ManagedToNativeAlloc(val); IntPtr ele = ManagedToNativeAlloc(val);
Handle = eina_list_sorted_insert(Handle, EinaCompareCb<T>(), ele); Handle = eina_list_sorted_insert(Handle, EinaCompareCb<T>(), ele);
} }
/// <summary>Inserts <c>val</c> in the list in a sorted manner with the given <c>compareCb</c> for element comparison.
/// It presumes the list is already sorted.</summary>
/// <param name="compareCb">The function to compare two elements of the list.</param>
/// <param name="val">The item to be inserted.</param>
public void SortedInsert(Compare compareCb, T val) public void SortedInsert(Compare compareCb, T val)
{ {
IntPtr ele = ManagedToNativeAlloc(val); IntPtr ele = ManagedToNativeAlloc(val);
Handle = eina_list_sorted_insert(Handle, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)), ele); Handle = eina_list_sorted_insert(Handle, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)), ele);
} }
/// <summary>Sorts <c>limit</c> elements in this list inplace.</summary>
/// <param name="limit">The max number of elements to be sorted.</param>
public void Sort(int limit = 0) public void Sort(int limit = 0)
{ {
Handle = eina_list_sort(Handle, (uint)limit, EinaCompareCb<T>()); Handle = eina_list_sort(Handle, (uint)limit, EinaCompareCb<T>());
} }
/// <summary>Sorts all elements in this list inplace.</summary>
/// <param name="compareCb">The function to compare two elements of the list.</param>
public void Sort(Compare compareCb) public void Sort(Compare compareCb)
{ {
Handle = eina_list_sort(Handle, 0, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb))); Handle = eina_list_sort(Handle, 0, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)));
} }
/// <summary>Sorts <c>limit</c> elements in this list inplace.</summary>
/// <param name="limit">The max number of elements to be sorted.</param>
/// <param name="compareCb">The function to compare two elements of the list.</param>
public void Sort(int limit, Compare compareCb) public void Sort(int limit, Compare compareCb)
{ {
Handle = eina_list_sort(Handle, (uint)limit, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb))); Handle = eina_list_sort(Handle, (uint)limit, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)));
@ -288,6 +329,9 @@ public class List<T> : IEnumerable<T>, IDisposable
}; };
} }
/// <summary>Returns the <c>n</c>th element of this list. Due to marshalling details, the returned element
/// may be a different C# object from the one you used to append.</summary>
/// <param name="n">The 0-based index to be retrieved.</param>
public T Nth(int n) public T Nth(int n)
{ {
// TODO: check bounds ??? // TODO: check bounds ???
@ -295,6 +339,9 @@ public class List<T> : IEnumerable<T>, IDisposable
return NativeToManaged<T>(ele); return NativeToManaged<T>(ele);
} }
/// <summary>Sets the data at the <c>idx</c> position.</summary>
/// <param name="idx">The 0-based index to be set.</param>
/// <param name="val">The value to be inserted.</param>
public void DataSet(int idx, T val) public void DataSet(int idx, T val)
{ {
IntPtr pos = eina_list_nth_list(Handle, (uint)idx); IntPtr pos = eina_list_nth_list(Handle, (uint)idx);
@ -312,6 +359,8 @@ public class List<T> : IEnumerable<T>, IDisposable
InternalDataSet(pos, ele); InternalDataSet(pos, ele);
} }
/// <summary>Accessor for the data at the <c>idx</c> position.</summary>
/// <param name="idx">The 0-based index to be get/set.</param>
public T this[int idx] public T this[int idx]
{ {
get get
@ -324,23 +373,30 @@ public class List<T> : IEnumerable<T>, IDisposable
} }
} }
/// <summary>Returns the data at the last list element.</summary>
/// <returns>The value contained in the last list position.</returns>
public T LastDataGet() public T LastDataGet()
{ {
IntPtr ele = eina_list_last_data_get_custom_export_mono(Handle); IntPtr ele = eina_list_last_data_get_custom_export_mono(Handle);
return NativeToManaged<T>(ele); return NativeToManaged<T>(ele);
} }
/// <summary>Reverses this list in place.</summary>
/// <returns>A reference to this object.</returns>
public List<T> Reverse() public List<T> Reverse()
{ {
Handle = eina_list_reverse(Handle); Handle = eina_list_reverse(Handle);
return this; return this;
} }
/// <summary>Randomly shuffles this list in place.</summary>
public void Shuffle() public void Shuffle()
{ {
Handle = eina_list_shuffle(Handle, IntPtr.Zero); Handle = eina_list_shuffle(Handle, IntPtr.Zero);
} }
/// <summary>Gets a C# array of the elements in this list.</summary>
/// <returns>A managed array of the elements.</returns>
public T[] ToArray() public T[] ToArray()
{ {
var managed = new T[Count()]; var managed = new T[Count()];
@ -353,6 +409,8 @@ public class List<T> : IEnumerable<T>, IDisposable
return managed; return managed;
} }
/// <summary>Appends the given array of elements to this list.</summary>
/// <param name="values">The values to be appended.</param>
public void AppendArray(T[] values) public void AppendArray(T[] values)
{ {
foreach (T v in values) foreach (T v in values)
@ -362,16 +420,22 @@ public class List<T> : IEnumerable<T>, IDisposable
} }
/// <summary>Gets an iterator that iterates this list in normal order.</summary>
/// <returns>The iterator.</returns>
public Eina.Iterator<T> GetIterator() public Eina.Iterator<T> GetIterator()
{ {
return new Eina.Iterator<T>(eina_list_iterator_new(Handle), true); return new Eina.Iterator<T>(eina_list_iterator_new(Handle), true);
} }
/// <summary>Gets an iterator that iterates this list in reverse order.</summary>
/// <returns>The iterator.</returns>
public Eina.Iterator<T> GetReversedIterator() public Eina.Iterator<T> GetReversedIterator()
{ {
return new Eina.Iterator<T>(eina_list_iterator_reversed_new(Handle), true); return new Eina.Iterator<T>(eina_list_iterator_reversed_new(Handle), true);
} }
/// <summary>Gets an enumerator into this list.</summary>
/// <returns>The enumerator.</returns>
public IEnumerator<T> GetEnumerator() public IEnumerator<T> GetEnumerator()
{ {
for (IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr)) for (IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr))
@ -380,12 +444,15 @@ public class List<T> : IEnumerable<T>, IDisposable
} }
} }
/// <summary>Gets an enumerator into this list.</summary>
/// <returns>The enumerator.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ {
return this.GetEnumerator(); return this.GetEnumerator();
} }
/// <summary> Gets an Accessor for this List.</summary> /// <summary> Gets an Accessor for this List.</summary>
/// <returns>The accessor.</returns>
public Eina.Accessor<T> GetAccessor() public Eina.Accessor<T> GetAccessor()
{ {
return new Eina.Accessor<T>(eina_list_accessor_new(Handle), Ownership.Managed); return new Eina.Accessor<T>(eina_list_accessor_new(Handle), Ownership.Managed);