summaryrefslogtreecommitdiff
path: root/src/bindings
diff options
context:
space:
mode:
authorBruno da Silva Belo <bruno.belo@expertisesolutions.com.br>2019-12-11 18:47:45 -0300
committerLauro Moura <lauromoura@expertisesolutions.com.br>2019-12-11 18:58:20 -0300
commit8fabc422b6565be81b94b802f0a8d3eff737e633 (patch)
tree9a866e8aabb9df719199d96e96f1dccafb89a960 /src/bindings
parent42f56d0a0a77414db708dbb8b2d08ccc4aacab6f (diff)
c#: Implement IList<T> to Eina.List.
Summary: Container can have three configuration over `Own` and `OwnContent`: `Own = true` and `OwnContent = true`; `Own = true` and `OwnContent = false`; `Own = false`and `OwnContent = false; If someone try to instanciate the container with `Own = false` and `OwnContent = true`, a exception raises. There is two Ownerships' behaviours in c#, where `IsReadOnly` is responsible and `IsReadOnly = !OwnContent`: Full Ownership: User can use modify/Add/Remove operations over the container, this is implemented with `OwnContent = true`. No Ownership: User **cannot** use modify/Add/Remove operations, this is implemented with `OwnContent = false`. For the memory, `Own` frees the node, while `OwnContent` frees the data portion. ref T8487 Reviewers: lauromoura, felipealmeida, YOhoho, segfaultxavi, jptiz Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Maniphest Tasks: T8487 Differential Revision: https://phab.enlightenment.org/D10742
Diffstat (limited to 'src/bindings')
-rw-r--r--src/bindings/mono/eina_mono/eina_list.cs364
1 files changed, 257 insertions, 107 deletions
diff --git a/src/bindings/mono/eina_mono/eina_list.cs b/src/bindings/mono/eina_mono/eina_list.cs
index 70f2d91665..d14dd26359 100644
--- a/src/bindings/mono/eina_mono/eina_list.cs
+++ b/src/bindings/mono/eina_mono/eina_list.cs
@@ -124,19 +124,22 @@ public static class ListNativeFunctions
124/// <summary>Native wrapper around a linked list of items. 124/// <summary>Native wrapper around a linked list of items.
125/// <para>Since EFL 1.23.</para> 125/// <para>Since EFL 1.23.</para>
126/// </summary> 126/// </summary>
127public class List<T> : IEnumerable<T>, IDisposable 127public class List<T> : IList<T>, IEnumerable<T>, IDisposable
128{ 128{
129 129
130 [EditorBrowsable(EditorBrowsableState.Never)] 130 [EditorBrowsable(EditorBrowsableState.Never)]
131 public IntPtr Handle {get;set;} = IntPtr.Zero; 131 public IntPtr Handle {get; set; } = IntPtr.Zero;
132
132 /// <summary>Whether this managed list owns the native one. 133 /// <summary>Whether this managed list owns the native one.
133 /// <para>Since EFL 1.23.</para> 134 /// <para>Since EFL 1.23.</para>
134 /// </summary> 135 /// </summary>
135 public bool Own {get;set;} 136 internal bool Own { get; set; }
137
136 /// <summary>Whether the native list wrapped owns the content it points to. 138 /// <summary>Whether the native list wrapped owns the content it points to.
137 /// <para>Since EFL 1.23.</para> 139 /// <para>Since EFL 1.23.</para>
138 ///</summary> 140 ///</summary>
139 public bool OwnContent {get;set;} 141 internal bool OwnContent { get; set; }
142
140 143
141 /// <summary>Delegate for comparing two elements of this list. 144 /// <summary>Delegate for comparing two elements of this list.
142 /// <para>Since EFL 1.23.</para> 145 /// <para>Since EFL 1.23.</para>
@@ -146,15 +149,16 @@ public class List<T> : IEnumerable<T>, IDisposable
146 /// <returns>-1, 0 or 1 for respectively smaller, equal or larger.</returns> 149 /// <returns>-1, 0 or 1 for respectively smaller, equal or larger.</returns>
147 public delegate int Compare(T a, T b); 150 public delegate int Compare(T a, T b);
148 151
149 /// <summary>The number of elements on this list. 152 public bool IsReadOnly { get => !OwnContent; }
150 /// <para>Since EFL 1.23.</para> 153
154 /// <summary>The number of elements in this list.
155 /// <para>Since EFL 1.24.</para>
151 /// </summary> 156 /// </summary>
152 public int Length 157 public int Count
153 { 158 {
154 get { return Count(); } 159 get => (int)eina_list_count_custom_export_mono(Handle);
155 } 160 }
156 161
157
158 private void InitNew() 162 private void InitNew()
159 { 163 {
160 Handle = IntPtr.Zero; 164 Handle = IntPtr.Zero;
@@ -163,38 +167,85 @@ public class List<T> : IEnumerable<T>, IDisposable
163 } 167 }
164 168
165 private IntPtr InternalLast() 169 private IntPtr InternalLast()
166 { 170 => eina_list_last_custom_export_mono(Handle);
167 return eina_list_last_custom_export_mono(Handle);
168 }
169 171
170 private static IntPtr InternalNext(IntPtr list) 172 private static IntPtr InternalNext(IntPtr list)
173 => eina_list_next_custom_export_mono(list);
174
175 private static IntPtr InternalPrev(IntPtr list)
176 => eina_list_prev_custom_export_mono(list);
177
178 private static IntPtr InternalDataGet(IntPtr list)
179 => eina_list_data_get_custom_export_mono(list);
180
181 private static IntPtr InternalDataSet(IntPtr list, IntPtr data)
182 => eina_list_data_set_custom_export_mono(list, data);
183
184 private IntPtr GetNative(int idx, Func<IntPtr, uint, IntPtr> f)
171 { 185 {
172 return eina_list_next_custom_export_mono(list); 186 if (!(0 <= idx && idx < Count))
187 {
188 throw new ArgumentOutOfRangeException();
189 }
190
191 var ele = f(Handle, (uint)idx);
192 if (ele == IntPtr.Zero)
193 {
194 throw new ArgumentOutOfRangeException();
195 }
196
197 return ele;
173 } 198 }
174 199
175 private static IntPtr InternalPrev(IntPtr list) 200 private IntPtr GetNativeDataPointer(int idx)
201 => GetNative(idx, eina_list_nth);
202
203 private IntPtr GetNativePointer(int idx)
204 => GetNative(idx, eina_list_nth_list);
205
206 private void RequireWritable()
176 { 207 {
177 return eina_list_prev_custom_export_mono(list); 208 if (IsReadOnly)
209 {
210 throw new NotSupportedException("Cannot modify read-only container.");
211 }
178 } 212 }
179 213
180 private static IntPtr InternalDataGet(IntPtr list) 214 private void CheckOwnerships()
181 { 215 {
182 return eina_list_data_get_custom_export_mono(list); 216 if ((Own == false) && (OwnContent == true))
217 {
218 throw new InvalidOperationException(nameof(Own) + "/" + nameof(OwnContent));
219 }
183 } 220 }
184 221
185 private static IntPtr InternalDataSet(IntPtr list, IntPtr data) 222 private void DeleteData(IntPtr ele)
186 { 223 {
187 return eina_list_data_set_custom_export_mono(list, data); 224 if (OwnContent)
225 {
226 NativeFree<T>(InternalDataGet(ele));
227 }
188 } 228 }
189 229
230 private U LoopingThrough<U>(T val, Func<int, U> f1, Func<U> f2)
231 {
232 int i = 0;
233 IntPtr cur = Handle;
234 for (; cur != IntPtr.Zero; ++i, cur = InternalNext(cur))
235 {
236 if (NativeToManaged<T>(InternalDataGet(cur)).Equals(val))
237 {
238 return f1(i);
239 }
240 }
241
242 return f2();
243 }
190 244
191 /// <summary>Creates a new empty list. 245 /// <summary>Creates a new empty list.
192 /// <para>Since EFL 1.23.</para> 246 /// <para>Since EFL 1.23.</para>
193 /// </summary> 247 /// </summary>
194 public List() 248 public List() => InitNew();
195 {
196 InitNew();
197 }
198 249
199 /// <summary>Creates a new list wrapping the given handle.</summary> 250 /// <summary>Creates a new list wrapping the given handle.</summary>
200 [EditorBrowsable(EditorBrowsableState.Never)] 251 [EditorBrowsable(EditorBrowsableState.Never)]
@@ -203,6 +254,7 @@ public class List<T> : IEnumerable<T>, IDisposable
203 Handle = handle; 254 Handle = handle;
204 Own = own; 255 Own = own;
205 OwnContent = own; 256 OwnContent = own;
257 CheckOwnerships();
206 } 258 }
207 259
208 /// <summary>Creates a new list wrapping the given handle.</summary> 260 /// <summary>Creates a new list wrapping the given handle.</summary>
@@ -212,15 +264,13 @@ public class List<T> : IEnumerable<T>, IDisposable
212 Handle = handle; 264 Handle = handle;
213 Own = own; 265 Own = own;
214 OwnContent = ownContent; 266 OwnContent = ownContent;
267 CheckOwnerships();
215 } 268 }
216 269
217 /// <summary>Finalizes this list. 270 /// <summary>Finalizes this list.
218 /// <para>Since EFL 1.23.</para> 271 /// <para>Since EFL 1.23.</para>
219 /// </summary> 272 /// </summary>
220 ~List() 273 ~List() => Dispose(false);
221 {
222 Dispose(false);
223 }
224 274
225 /// <summary>Disposes of this list. 275 /// <summary>Disposes of this list.
226 /// <para>Since EFL 1.23.</para> 276 /// <para>Since EFL 1.23.</para>
@@ -236,12 +286,14 @@ public class List<T> : IEnumerable<T>, IDisposable
236 return; 286 return;
237 } 287 }
238 288
239 if (Own && OwnContent) 289 for (IntPtr curr = h; curr != IntPtr.Zero; curr = InternalNext(curr))
240 { 290 {
241 for (IntPtr curr = h; curr != IntPtr.Zero; curr = InternalNext(curr)) 291 if (!OwnContent)
242 { 292 {
243 NativeFree<T>(InternalDataGet(curr)); 293 break;
244 } 294 }
295
296 DeleteData(curr);
245 } 297 }
246 298
247 if (Own) 299 if (Own)
@@ -269,30 +321,17 @@ public class List<T> : IEnumerable<T>, IDisposable
269 /// <summary>Disposes of this list. 321 /// <summary>Disposes of this list.
270 /// <para>Since EFL 1.23.</para> 322 /// <para>Since EFL 1.23.</para>
271 /// </summary> 323 /// </summary>
272 public void Free() 324 public void Free() => Dispose();
273 {
274 Dispose();
275 }
276
277 /// <summary>Relinquishes of the native list.
278 /// <para>Since EFL 1.23.</para>
279 /// </summary>
280 /// <returns>The previously wrapped native list handle.</returns>
281 public IntPtr Release()
282 {
283 IntPtr h = Handle;
284 Handle = IntPtr.Zero;
285 return h;
286 }
287 325
288 /// <summary>Sets whether this wrapper should own the native list or not. 326 /// <summary>Sets whether this wrapper should own the native list or not.
289 /// <para>Since EFL 1.23.</para> 327 /// <para>Since EFL 1.23.</para>
290 /// </summary> 328 /// </summary>
291 /// <param name="ownAll">If the hash own for all ownerships.</param> 329 /// <param name="ownAll">If the hash own for all ownerships.</param>
292 public void SetOwnership(bool ownAll) 330 internal void SetOwnership(bool ownAll)
293 { 331 {
294 Own = ownAll; 332 Own = ownAll;
295 OwnContent = ownAll; 333 OwnContent = ownAll;
334 CheckOwnerships();
296 } 335 }
297 336
298 /// <summary>Sets whether this wrapper should own the native list and 337 /// <summary>Sets whether this wrapper should own the native list and
@@ -301,19 +340,11 @@ public class List<T> : IEnumerable<T>, IDisposable
301 /// </summary> 340 /// </summary>
302 /// <param name="own">If own the object.</param> 341 /// <param name="own">If own the object.</param>
303 /// <param name="ownContent">If own the content's object.</param> 342 /// <param name="ownContent">If own the content's object.</param>
304 public void SetOwnership(bool own, bool ownContent) 343 internal void SetOwnership(bool own, bool ownContent)
305 { 344 {
306 Own = own; 345 Own = own;
307 OwnContent = ownContent; 346 OwnContent = ownContent;
308 } 347 CheckOwnerships();
309
310 /// <summary>Returns the number of elements in this list.
311 /// <para>Since EFL 1.23.</para>
312 /// </summary>
313 /// <returns>The number of elements.</returns>
314 public int Count()
315 {
316 return (int)eina_list_count_custom_export_mono(Handle);
317 } 348 }
318 349
319 /// <summary>Appends <c>val</c> to the list. 350 /// <summary>Appends <c>val</c> to the list.
@@ -322,8 +353,9 @@ public class List<T> : IEnumerable<T>, IDisposable
322 /// <param name="val">The item to be appended.</param> 353 /// <param name="val">The item to be appended.</param>
323 public void Append(T val) 354 public void Append(T val)
324 { 355 {
325 IntPtr ele = ManagedToNativeAlloc(val); 356 RequireWritable();
326 Handle = eina_list_append(Handle, ele); 357
358 Handle = eina_list_append(Handle, ManagedToNativeAlloc(val));
327 } 359 }
328 360
329 /// <summary>Prepends <c>val</c> to the list. 361 /// <summary>Prepends <c>val</c> to the list.
@@ -332,8 +364,9 @@ public class List<T> : IEnumerable<T>, IDisposable
332 /// <param name="val">The item to be prepended.</param> 364 /// <param name="val">The item to be prepended.</param>
333 public void Prepend(T val) 365 public void Prepend(T val)
334 { 366 {
335 IntPtr ele = ManagedToNativeAlloc(val); 367 RequireWritable();
336 Handle = eina_list_prepend(Handle, ele); 368
369 Handle = eina_list_prepend(Handle, ManagedToNativeAlloc(val));
337 } 370 }
338 371
339 /// <summary>Inserts <c>val</c> in the list in a sorted manner. 372 /// <summary>Inserts <c>val</c> in the list in a sorted manner.
@@ -343,8 +376,11 @@ public class List<T> : IEnumerable<T>, IDisposable
343 /// <param name="val">The item to be inserted.</param> 376 /// <param name="val">The item to be inserted.</param>
344 public void SortedInsert(T val) 377 public void SortedInsert(T val)
345 { 378 {
346 IntPtr ele = ManagedToNativeAlloc(val); 379 RequireWritable();
347 Handle = eina_list_sorted_insert(Handle, EinaCompareCb<T>(), ele); 380
381 Handle = eina_list_sorted_insert(Handle,
382 EinaCompareCb<T>(),
383 ManagedToNativeAlloc(val));
348 } 384 }
349 385
350 /// <summary>Inserts <c>val</c> in the list in a sorted manner with the 386 /// <summary>Inserts <c>val</c> in the list in a sorted manner with the
@@ -356,8 +392,11 @@ public class List<T> : IEnumerable<T>, IDisposable
356 /// <param name="val">The item to be inserted.</param> 392 /// <param name="val">The item to be inserted.</param>
357 public void SortedInsert(Compare compareCb, T val) 393 public void SortedInsert(Compare compareCb, T val)
358 { 394 {
359 IntPtr ele = ManagedToNativeAlloc(val); 395 RequireWritable();
360 Handle = eina_list_sorted_insert(Handle, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)), ele); 396
397 Handle = eina_list_sorted_insert(Handle,
398 Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)),
399 ManagedToNativeAlloc(val));
361 } 400 }
362 401
363 /// <summary>Sorts <c>limit</c> elements in this list inplace. 402 /// <summary>Sorts <c>limit</c> elements in this list inplace.
@@ -366,6 +405,8 @@ public class List<T> : IEnumerable<T>, IDisposable
366 /// <param name="limit">The max number of elements to be sorted.</param> 405 /// <param name="limit">The max number of elements to be sorted.</param>
367 public void Sort(int limit = 0) 406 public void Sort(int limit = 0)
368 { 407 {
408 RequireWritable();
409
369 Handle = eina_list_sort(Handle, (uint)limit, EinaCompareCb<T>()); 410 Handle = eina_list_sort(Handle, (uint)limit, EinaCompareCb<T>());
370 } 411 }
371 412
@@ -375,7 +416,10 @@ public class List<T> : IEnumerable<T>, IDisposable
375 /// <param name="compareCb">The function to compare two elements of the list.</param> 416 /// <param name="compareCb">The function to compare two elements of the list.</param>
376 public void Sort(Compare compareCb) 417 public void Sort(Compare compareCb)
377 { 418 {
378 Handle = eina_list_sort(Handle, 0, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb))); 419 RequireWritable();
420
421 Handle = eina_list_sort(Handle, (uint)0,
422 Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)));
379 } 423 }
380 424
381 /// <summary>Sorts <c>limit</c> elements in this list inplace. 425 /// <summary>Sorts <c>limit</c> elements in this list inplace.
@@ -385,15 +429,16 @@ public class List<T> : IEnumerable<T>, IDisposable
385 /// <param name="compareCb">The function to compare two elements of the list.</param> 429 /// <param name="compareCb">The function to compare two elements of the list.</param>
386 public void Sort(int limit, Compare compareCb) 430 public void Sort(int limit, Compare compareCb)
387 { 431 {
388 Handle = eina_list_sort(Handle, (uint)limit, Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb))); 432 RequireWritable();
433
434 Handle = eina_list_sort(Handle, (uint)limit,
435 Marshal.GetFunctionPointerForDelegate(GetNativeCompareCb(compareCb)));
389 } 436 }
390 437
391 private Eina.Callbacks.EinaCompareCb GetNativeCompareCb(Compare managedCb) 438 private Eina.Callbacks.EinaCompareCb GetNativeCompareCb(Compare managedCb)
392 { 439 => (IntPtr a, IntPtr b)
393 return (IntPtr a, IntPtr b) => { 440 => managedCb(NativeToManaged<T>(a), NativeToManaged<T>(b));
394 return managedCb(NativeToManaged<T>(a), NativeToManaged<T>(b)); 441
395 };
396 }
397 442
398 /// <summary>Returns the <c>n</c>th element of this list. Due to marshalling details, the returned element 443 /// <summary>Returns the <c>n</c>th element of this list. Due to marshalling details, the returned element
399 /// may be a different C# object from the one you used to append. 444 /// may be a different C# object from the one you used to append.
@@ -401,33 +446,20 @@ public class List<T> : IEnumerable<T>, IDisposable
401 /// </summary> 446 /// </summary>
402 /// <param name="n">The 0-based index to be retrieved.</param> 447 /// <param name="n">The 0-based index to be retrieved.</param>
403 /// <returns>The value in the specified element.</returns> 448 /// <returns>The value in the specified element.</returns>
404 public T Nth(int n) 449 public T Nth(int n) => NativeToManaged<T>(GetNativeDataPointer(n));
405 {
406 // TODO: check bounds ???
407 IntPtr ele = eina_list_nth(Handle, (uint)n);
408 return NativeToManaged<T>(ele);
409 }
410 450
411 /// <summary>Sets the data at the <c>idx</c> position. 451 /// <summary>Sets the data at the <c>idx</c> position.
412 /// <para>Since EFL 1.23.</para> 452 /// <para>Since EFL 1.23.</para>
413 /// </summary> 453 /// </summary>
414 /// <param name="idx">The 0-based index to be set.</param> 454 /// <param name="idx">The 0-based index to be set.</param>
415 /// <param name="val">The value to be inserted.</param> 455 /// <param name="val">The value to be inserted.</param>
416 public void DataSet(int idx, T val) 456 internal void DataSet(int idx, T val)
417 { 457 {
418 IntPtr pos = eina_list_nth_list(Handle, (uint)idx); 458 RequireWritable();
419 if (pos == IntPtr.Zero)
420 {
421 throw new IndexOutOfRangeException();
422 }
423 459
424 if (OwnContent) 460 IntPtr pos = GetNativePointer(idx);
425 { 461 DeleteData(pos);
426 NativeFree<T>(InternalDataGet(pos)); 462 InternalDataSet(pos, ManagedToNativeAlloc(val));
427 }
428
429 IntPtr ele = ManagedToNativeAlloc(val);
430 InternalDataSet(pos, ele);
431 } 463 }
432 464
433 /// <summary>Accessor for the data at the <c>idx</c> position. 465 /// <summary>Accessor for the data at the <c>idx</c> position.
@@ -451,10 +483,7 @@ public class List<T> : IEnumerable<T>, IDisposable
451 /// </summary> 483 /// </summary>
452 /// <returns>The value contained in the last list position.</returns> 484 /// <returns>The value contained in the last list position.</returns>
453 public T LastDataGet() 485 public T LastDataGet()
454 { 486 => NativeToManaged<T>(eina_list_last_data_get_custom_export_mono(Handle));
455 IntPtr ele = eina_list_last_data_get_custom_export_mono(Handle);
456 return NativeToManaged<T>(ele);
457 }
458 487
459 /// <summary>Reverses this list in place. 488 /// <summary>Reverses this list in place.
460 /// <para>Since EFL 1.23.</para> 489 /// <para>Since EFL 1.23.</para>
@@ -462,6 +491,8 @@ public class List<T> : IEnumerable<T>, IDisposable
462 /// <returns>A reference to this object.</returns> 491 /// <returns>A reference to this object.</returns>
463 public List<T> Reverse() 492 public List<T> Reverse()
464 { 493 {
494 RequireWritable();
495
465 Handle = eina_list_reverse(Handle); 496 Handle = eina_list_reverse(Handle);
466 return this; 497 return this;
467 } 498 }
@@ -471,6 +502,8 @@ public class List<T> : IEnumerable<T>, IDisposable
471 /// </summary> 502 /// </summary>
472 public void Shuffle() 503 public void Shuffle()
473 { 504 {
505 RequireWritable();
506
474 Handle = eina_list_shuffle(Handle, IntPtr.Zero); 507 Handle = eina_list_shuffle(Handle, IntPtr.Zero);
475 } 508 }
476 509
@@ -480,9 +513,10 @@ public class List<T> : IEnumerable<T>, IDisposable
480 /// <returns>A managed array of the elements.</returns> 513 /// <returns>A managed array of the elements.</returns>
481 public T[] ToArray() 514 public T[] ToArray()
482 { 515 {
483 var managed = new T[Count()]; 516 var managed = new T[Count];
484 int i = 0; 517 int i = 0;
485 for (IntPtr curr = Handle; curr != IntPtr.Zero; curr = InternalNext(curr), ++i) 518 for (IntPtr curr = Handle; curr != IntPtr.Zero;
519 curr = InternalNext(curr), ++i)
486 { 520 {
487 managed[i] = NativeToManaged<T>(InternalDataGet(curr)); 521 managed[i] = NativeToManaged<T>(InternalDataGet(curr));
488 } 522 }
@@ -494,8 +528,10 @@ public class List<T> : IEnumerable<T>, IDisposable
494 /// <para>Since EFL 1.23.</para> 528 /// <para>Since EFL 1.23.</para>
495 /// </summary> 529 /// </summary>
496 /// <param name="values">The values to be appended.</param> 530 /// <param name="values">The values to be appended.</param>
497 public void AppendArray(T[] values) 531 public void Append(T[] values)
498 { 532 {
533 RequireWritable();
534
499 foreach (T v in values) 535 foreach (T v in values)
500 { 536 {
501 Append(v); 537 Append(v);
@@ -508,18 +544,14 @@ public class List<T> : IEnumerable<T>, IDisposable
508 /// </summary> 544 /// </summary>
509 /// <returns>The iterator.</returns> 545 /// <returns>The iterator.</returns>
510 public Eina.Iterator<T> GetIterator() 546 public Eina.Iterator<T> GetIterator()
511 { 547 => new Eina.Iterator<T>(eina_list_iterator_new(Handle), true);
512 return new Eina.Iterator<T>(eina_list_iterator_new(Handle), true);
513 }
514 548
515 /// <summary>Gets an iterator that iterates this list in reverse order. 549 /// <summary>Gets an iterator that iterates this list in reverse order.
516 /// <para>Since EFL 1.23.</para> 550 /// <para>Since EFL 1.23.</para>
517 /// </summary> 551 /// </summary>
518 /// <returns>The iterator.</returns> 552 /// <returns>The iterator.</returns>
519 public Eina.Iterator<T> GetReversedIterator() 553 public Eina.Iterator<T> GetReversedIterator()
520 { 554 => new Eina.Iterator<T>(eina_list_iterator_reversed_new(Handle), true);
521 return new Eina.Iterator<T>(eina_list_iterator_reversed_new(Handle), true);
522 }
523 555
524 /// <summary>Gets an enumerator into this list. 556 /// <summary>Gets an enumerator into this list.
525 /// <para>Since EFL 1.23.</para> 557 /// <para>Since EFL 1.23.</para>
@@ -538,18 +570,136 @@ public class List<T> : IEnumerable<T>, IDisposable
538 /// </summary> 570 /// </summary>
539 /// <returns>The enumerator.</returns> 571 /// <returns>The enumerator.</returns>
540 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 572 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
541 { 573 => this.GetEnumerator();
542 return this.GetEnumerator();
543 }
544 574
545 /// <summary> Gets an Accessor for this List. 575 /// <summary> Gets an Accessor for this List.
546 /// <para>Since EFL 1.23.</para> 576 /// <para>Since EFL 1.23.</para>
547 /// </summary> 577 /// </summary>
548 /// <returns>The accessor.</returns> 578 /// <returns>The accessor.</returns>
549 public Eina.Accessor<T> GetAccessor() 579 public Eina.Accessor<T> GetAccessor()
580 => new Eina.Accessor<T>(eina_list_accessor_new(Handle),
581 Ownership.Managed);
582
583 /// <summary>
584 /// Removes the first occurrence of a specific object.
585 /// <para>Since EFL 1.24.</para>
586 /// </summary>
587 /// <param name="val">The object to remove.</param>
588 public bool Remove(T val)
550 { 589 {
551 return new Eina.Accessor<T>(eina_list_accessor_new(Handle), Ownership.Managed); 590 RequireWritable();
591
592 var prev_count = Count;
593 var deleted = LoopingThrough(val,
594 (i) =>
595 {
596 RemoveAt(i);
597 return true;
598 },
599 () => false);
600
601 return deleted && (prev_count - 1 == Count);
552 } 602 }
603
604 /// <summary>
605 /// Adds an item.
606 /// <para>Since EFL 1.24.</para>
607 /// </summary>
608 /// <param name="val">The object to add.</param>
609 public void Add(T val) => Append(val);
610
611 /// <summary>
612 /// Removes all items.
613 /// <para>Since EFL 1.24.</para>
614 /// </summary>
615 public void Clear()
616 {
617 RequireWritable();
618
619 for (; Handle != IntPtr.Zero;)
620 {
621 Handle = eina_list_remove_list(Handle, Handle);
622 }
623 }
624
625 /// <summary>
626 /// Determines whether the <see cref="List{T}" /> contains a specific value.
627 /// <para>Since EFL 1.24.</para>
628 /// </summary>
629 /// <param name="val">The object to locate.</param>
630 public bool Contains(T val)
631 => LoopingThrough(val, (i) => true, () => false);
632
633 /// <summary>
634 /// Determines the index of a specific item.
635 /// <para>Since EFL 1.24.</para>
636 /// </summary>
637 /// <param name="val">The object to locate.</param>
638 public int IndexOf(T val)
639 => LoopingThrough(val, (i) => i, () => -1);
640
641 /// <summary>
642 /// Inserts an item to the <see cref="List{T}" /> at the specified index.
643 /// <para>Since EFL 1.24.</para>
644 /// </summary>
645 /// <param name="idx">The zero-based index at which item should be inserted.</param>
646 /// <param name="val">The object to insert.</param>
647 public void Insert(int idx, T val)
648 {
649 RequireWritable();
650
651 if (idx == 0)
652 {
653 Prepend(val);
654 return;
655 }
656
657 if (idx == Count)
658 {
659 Append(val);
660 return;
661 }
662
663 if (idx < 0)
664 {
665 throw new ArgumentOutOfRangeException(nameof(idx), $"{nameof(idx)} cannot be negative.");
666 }
667
668 if (Count < idx)
669 {
670 throw new ArgumentOutOfRangeException(nameof(idx), $"{nameof(idx)} cannot be greater than {nameof(Count)} + 1.");
671 }
672
673 Handle = eina_list_prepend_relative_list(Handle, ManagedToNativeAlloc(val),
674 GetNativePointer(idx));
675 }
676
677 /// <summary>
678 /// Removes the <see cref="List{T}" /> item at the specified index.
679 /// <para>Since EFL 1.24.</para>
680 /// </summary>
681 /// <param name="idx">The zero-based index of the item to remove.</param>
682 public void RemoveAt(int idx)
683 {
684 RequireWritable();
685
686 var ele = GetNativePointer(idx);
687 DeleteData(ele);
688 Handle = eina_list_remove_list(Handle, ele);
689 }
690
691 /// <summary>
692 /// Copies the elements of the <see cref="List{T}" /> to an
693 /// <see cref="Array" />, starting at a particular <see cref="Array" /> index.
694 /// <para>Since EFL 1.24.</para>
695 /// </summary>
696 /// <param name="array">The one-dimensional <see cref="Array" /> that is the
697 /// destination of the elements copied from <see cref="List{T}" />.
698 /// The <see cref="Array" /> must have zero-based indexing.</param>
699 /// <param name="arrayIndex">The zero-based index in array at which copying
700 /// begins.</param>
701 public void CopyTo(T[] array, int arrayIndex)
702 => ToArray().CopyTo(array, arrayIndex);
553} 703}
554 704
555} 705}