1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.ListG; 26 27 private import glib.Str; 28 private import glib.c.functions; 29 public import glib.c.types; 30 private import gobject.ObjectG; 31 public import gtkc.glibtypes; 32 33 34 /** 35 * The #GList struct is used for each element in a doubly-linked list. 36 */ 37 public class ListG 38 { 39 /** the main Gtk struct */ 40 protected GList* gList; 41 protected bool ownedRef; 42 43 /** Get the main Gtk struct */ 44 public GList* getListGStruct(bool transferOwnership = false) 45 { 46 if (transferOwnership) 47 ownedRef = false; 48 return gList; 49 } 50 51 /** the main Gtk struct as a void* */ 52 protected void* getStruct() 53 { 54 return cast(void*)gList; 55 } 56 57 /** 58 * Sets our main struct and passes it to the parent class. 59 */ 60 public this (GList* gList, bool ownedRef = false) 61 { 62 this.gList = gList; 63 this.ownedRef = ownedRef; 64 } 65 66 /** */ 67 @property void* data() 68 { 69 return gList.data; 70 } 71 72 /** 73 * get the next element 74 * Returns: the next element, or NULL if there are no more elements. 75 */ 76 @property ListG next() 77 { 78 if ( gList.next is null ) 79 { 80 return null; 81 } 82 83 return new ListG(gList.next); 84 } 85 86 /** 87 * get the previous element 88 * Returns: the previous element, or NULL if there are no more elements. 89 */ 90 @property ListG previous() 91 { 92 if ( gList.prev is null ) 93 { 94 return null; 95 } 96 97 return new ListG(gList.prev); 98 } 99 100 /** 101 * Turn the list into a D array of the desiered type. 102 * Type T wraps should match the type of the data. 103 */ 104 public T[] toArray(T, TC = getCType!T)() 105 if ( is(T == class) ) 106 { 107 T[] arr = new T[length()]; 108 ListG list = this; 109 size_t count; 110 111 while(list !is null && count < arr.length) 112 { 113 arr[count] = ObjectG.getDObject!(T)(cast(TC)list.data); 114 list = list.next(); 115 count++; 116 } 117 118 return arr; 119 } 120 121 /** Ditto */ 122 public T[] toArray(T)() 123 if ( is ( T == string ) ) 124 { 125 T[] arr = new T[length()]; 126 ListG list = this; 127 size_t count; 128 129 while(list !is null && count < arr.length) 130 { 131 arr[count] = Str.toString(cast(char*)list.data); 132 list = list.next(); 133 count++; 134 } 135 136 return arr; 137 } 138 139 private template getCType(T) 140 { 141 static if ( is(T == class) ) 142 alias getCType = typeof(T.tupleof[0]); 143 else 144 alias getCType = void*; 145 } 146 147 unittest 148 { 149 import gobject.Value; 150 151 auto list = new ListG(null); 152 list = list.append(new Value(0).getValueStruct()); 153 list = list.append(new Value(1).getValueStruct()); 154 auto arr = list.toArray!Value(); 155 156 assert(arr[0].getInt() == 0); 157 assert(arr[1].getInt() == 1); 158 159 list = new ListG(null); 160 list = list.append(cast(void*)"test\0".ptr); 161 list = list.append(cast(void*)"test2\0".ptr); 162 163 assert(["test", "test2"] == list.toArray!string()); 164 } 165 166 /** 167 */ 168 169 /** 170 * Allocates space for one #GList element. It is called by 171 * g_list_append(), g_list_prepend(), g_list_insert() and 172 * g_list_insert_sorted() and so is rarely used on its own. 173 * 174 * Returns: a pointer to the newly-allocated #GList element 175 */ 176 public static ListG alloc() 177 { 178 auto p = g_list_alloc(); 179 180 if(p is null) 181 { 182 return null; 183 } 184 185 return new ListG(cast(GList*) p); 186 } 187 188 /** 189 * Adds a new element on to the end of the list. 190 * 191 * Note that the return value is the new start of the list, 192 * if @list was empty; make sure you store the new value. 193 * 194 * g_list_append() has to traverse the entire list to find the end, 195 * which is inefficient when adding multiple elements. A common idiom 196 * to avoid the inefficiency is to use g_list_prepend() and reverse 197 * the list with g_list_reverse() when all elements have been added. 198 * 199 * |[<!-- language="C" --> 200 * // Notice that these are initialized to the empty list. 201 * GList *string_list = NULL, *number_list = NULL; 202 * 203 * // This is a list of strings. 204 * string_list = g_list_append (string_list, "first"); 205 * string_list = g_list_append (string_list, "second"); 206 * 207 * // This is a list of integers. 208 * number_list = g_list_append (number_list, GINT_TO_POINTER (27)); 209 * number_list = g_list_append (number_list, GINT_TO_POINTER (14)); 210 * ]| 211 * 212 * Params: 213 * data = the data for the new element 214 * 215 * Returns: either @list or the new start of the #GList if @list was %NULL 216 */ 217 public ListG append(void* data) 218 { 219 auto p = g_list_append(gList, data); 220 221 if(p is null) 222 { 223 return null; 224 } 225 226 return new ListG(cast(GList*) p); 227 } 228 229 /** 230 * Adds the second #GList onto the end of the first #GList. 231 * Note that the elements of the second #GList are not copied. 232 * They are used directly. 233 * 234 * This function is for example used to move an element in the list. 235 * The following example moves an element to the top of the list: 236 * |[<!-- language="C" --> 237 * list = g_list_remove_link (list, llink); 238 * list = g_list_concat (llink, list); 239 * ]| 240 * 241 * Params: 242 * list2 = the #GList to add to the end of the first #GList, 243 * this must point to the top of the list 244 * 245 * Returns: the start of the new #GList, which equals @list1 if not %NULL 246 */ 247 public ListG concat(ListG list2) 248 { 249 auto p = g_list_concat(gList, (list2 is null) ? null : list2.getListGStruct()); 250 251 if(p is null) 252 { 253 return null; 254 } 255 256 return new ListG(cast(GList*) p); 257 } 258 259 /** 260 * Copies a #GList. 261 * 262 * Note that this is a "shallow" copy. If the list elements 263 * consist of pointers to data, the pointers are copied but 264 * the actual data is not. See g_list_copy_deep() if you need 265 * to copy the data as well. 266 * 267 * Returns: the start of the new list that holds the same data as @list 268 */ 269 public ListG copy() 270 { 271 auto p = g_list_copy(gList); 272 273 if(p is null) 274 { 275 return null; 276 } 277 278 return new ListG(cast(GList*) p); 279 } 280 281 /** 282 * Makes a full (deep) copy of a #GList. 283 * 284 * In contrast with g_list_copy(), this function uses @func to make 285 * a copy of each list element, in addition to copying the list 286 * container itself. 287 * 288 * @func, as a #GCopyFunc, takes two arguments, the data to be copied 289 * and a @user_data pointer. It's safe to pass %NULL as user_data, 290 * if the copy function takes only one argument. 291 * 292 * For instance, if @list holds a list of GObjects, you can do: 293 * |[<!-- language="C" --> 294 * another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL); 295 * ]| 296 * 297 * And, to entirely free the new list, you could do: 298 * |[<!-- language="C" --> 299 * g_list_free_full (another_list, g_object_unref); 300 * ]| 301 * 302 * Params: 303 * func = a copy function used to copy every element in the list 304 * userData = user data passed to the copy function @func, or %NULL 305 * 306 * Returns: the start of the new list that holds a full copy of @list, 307 * use g_list_free_full() to free it 308 * 309 * Since: 2.34 310 */ 311 public ListG copyDeep(GCopyFunc func, void* userData) 312 { 313 auto p = g_list_copy_deep(gList, func, userData); 314 315 if(p is null) 316 { 317 return null; 318 } 319 320 return new ListG(cast(GList*) p); 321 } 322 323 /** 324 * Removes the node link_ from the list and frees it. 325 * Compare this to g_list_remove_link() which removes the node 326 * without freeing it. 327 * 328 * Params: 329 * link = node to delete from @list 330 * 331 * Returns: the (possibly changed) start of the #GList 332 */ 333 public ListG deleteLink(ListG link) 334 { 335 auto p = g_list_delete_link(gList, (link is null) ? null : link.getListGStruct()); 336 337 if(p is null) 338 { 339 return null; 340 } 341 342 return new ListG(cast(GList*) p); 343 } 344 345 /** 346 * Finds the element in a #GList which contains the given data. 347 * 348 * Params: 349 * data = the element data to find 350 * 351 * Returns: the found #GList element, or %NULL if it is not found 352 */ 353 public ListG find(void* data) 354 { 355 auto p = g_list_find(gList, data); 356 357 if(p is null) 358 { 359 return null; 360 } 361 362 return new ListG(cast(GList*) p); 363 } 364 365 /** 366 * Finds an element in a #GList, using a supplied function to 367 * find the desired element. It iterates over the list, calling 368 * the given function which should return 0 when the desired 369 * element is found. The function takes two #gconstpointer arguments, 370 * the #GList element's data as the first argument and the 371 * given user data. 372 * 373 * Params: 374 * data = user data passed to the function 375 * func = the function to call for each element. 376 * It should return 0 when the desired element is found 377 * 378 * Returns: the found #GList element, or %NULL if it is not found 379 */ 380 public ListG findCustom(void* data, GCompareFunc func) 381 { 382 auto p = g_list_find_custom(gList, data, func); 383 384 if(p is null) 385 { 386 return null; 387 } 388 389 return new ListG(cast(GList*) p); 390 } 391 392 /** 393 * Gets the first element in a #GList. 394 * 395 * Returns: the first element in the #GList, 396 * or %NULL if the #GList has no elements 397 */ 398 public ListG first() 399 { 400 auto p = g_list_first(gList); 401 402 if(p is null) 403 { 404 return null; 405 } 406 407 return new ListG(cast(GList*) p); 408 } 409 410 /** 411 * Calls a function for each element of a #GList. 412 * 413 * Params: 414 * func = the function to call with each element's data 415 * userData = user data to pass to the function 416 */ 417 public void foreac(GFunc func, void* userData) 418 { 419 g_list_foreach(gList, func, userData); 420 } 421 422 /** 423 * Frees all of the memory used by a #GList. 424 * The freed elements are returned to the slice allocator. 425 * 426 * If list elements contain dynamically-allocated memory, you should 427 * either use g_list_free_full() or free them manually first. 428 */ 429 public void free() 430 { 431 g_list_free(gList); 432 } 433 434 /** 435 * Frees one #GList element, but does not update links from the next and 436 * previous elements in the list, so you should not call this function on an 437 * element that is currently part of a list. 438 * 439 * It is usually used after g_list_remove_link(). 440 */ 441 public void free1() 442 { 443 g_list_free_1(gList); 444 } 445 446 /** 447 * Convenience method, which frees all the memory used by a #GList, 448 * and calls @free_func on every element's data. 449 * 450 * Params: 451 * freeFunc = the function to be called to free each element's data 452 * 453 * Since: 2.28 454 */ 455 public void freeFull(GDestroyNotify freeFunc) 456 { 457 g_list_free_full(gList, freeFunc); 458 } 459 460 /** 461 * Gets the position of the element containing 462 * the given data (starting from 0). 463 * 464 * Params: 465 * data = the data to find 466 * 467 * Returns: the index of the element containing the data, 468 * or -1 if the data is not found 469 */ 470 public int index(void* data) 471 { 472 return g_list_index(gList, data); 473 } 474 475 /** 476 * Inserts a new element into the list at the given position. 477 * 478 * Params: 479 * data = the data for the new element 480 * position = the position to insert the element. If this is 481 * negative, or is larger than the number of elements in the 482 * list, the new element is added on to the end of the list. 483 * 484 * Returns: the (possibly changed) start of the #GList 485 */ 486 public ListG insert(void* data, int position) 487 { 488 auto p = g_list_insert(gList, data, position); 489 490 if(p is null) 491 { 492 return null; 493 } 494 495 return new ListG(cast(GList*) p); 496 } 497 498 /** 499 * Inserts a new element into the list before the given position. 500 * 501 * Params: 502 * sibling = the list element before which the new element 503 * is inserted or %NULL to insert at the end of the list 504 * data = the data for the new element 505 * 506 * Returns: the (possibly changed) start of the #GList 507 */ 508 public ListG insertBefore(ListG sibling, void* data) 509 { 510 auto p = g_list_insert_before(gList, (sibling is null) ? null : sibling.getListGStruct(), data); 511 512 if(p is null) 513 { 514 return null; 515 } 516 517 return new ListG(cast(GList*) p); 518 } 519 520 /** 521 * Inserts a new element into the list, using the given comparison 522 * function to determine its position. 523 * 524 * If you are adding many new elements to a list, and the number of 525 * new elements is much larger than the length of the list, use 526 * g_list_prepend() to add the new items and sort the list afterwards 527 * with g_list_sort(). 528 * 529 * Params: 530 * data = the data for the new element 531 * func = the function to compare elements in the list. It should 532 * return a number > 0 if the first parameter comes after the 533 * second parameter in the sort order. 534 * 535 * Returns: the (possibly changed) start of the #GList 536 */ 537 public ListG insertSorted(void* data, GCompareFunc func) 538 { 539 auto p = g_list_insert_sorted(gList, data, func); 540 541 if(p is null) 542 { 543 return null; 544 } 545 546 return new ListG(cast(GList*) p); 547 } 548 549 /** 550 * Inserts a new element into the list, using the given comparison 551 * function to determine its position. 552 * 553 * If you are adding many new elements to a list, and the number of 554 * new elements is much larger than the length of the list, use 555 * g_list_prepend() to add the new items and sort the list afterwards 556 * with g_list_sort(). 557 * 558 * Params: 559 * data = the data for the new element 560 * func = the function to compare elements in the list. It should 561 * return a number > 0 if the first parameter comes after the 562 * second parameter in the sort order. 563 * userData = user data to pass to comparison function 564 * 565 * Returns: the (possibly changed) start of the #GList 566 * 567 * Since: 2.10 568 */ 569 public ListG insertSortedWithData(void* data, GCompareDataFunc func, void* userData) 570 { 571 auto p = g_list_insert_sorted_with_data(gList, data, func, userData); 572 573 if(p is null) 574 { 575 return null; 576 } 577 578 return new ListG(cast(GList*) p); 579 } 580 581 /** 582 * Gets the last element in a #GList. 583 * 584 * Returns: the last element in the #GList, 585 * or %NULL if the #GList has no elements 586 */ 587 public ListG last() 588 { 589 auto p = g_list_last(gList); 590 591 if(p is null) 592 { 593 return null; 594 } 595 596 return new ListG(cast(GList*) p); 597 } 598 599 /** 600 * Gets the number of elements in a #GList. 601 * 602 * This function iterates over the whole list to count its elements. 603 * Use a #GQueue instead of a GList if you regularly need the number 604 * of items. To check whether the list is non-empty, it is faster to check 605 * @list against %NULL. 606 * 607 * Returns: the number of elements in the #GList 608 */ 609 public uint length() 610 { 611 return g_list_length(gList); 612 } 613 614 /** 615 * Gets the element at the given position in a #GList. 616 * 617 * This iterates over the list until it reaches the @n-th position. If you 618 * intend to iterate over every element, it is better to use a for-loop as 619 * described in the #GList introduction. 620 * 621 * Params: 622 * n = the position of the element, counting from 0 623 * 624 * Returns: the element, or %NULL if the position is off 625 * the end of the #GList 626 */ 627 public ListG nth(uint n) 628 { 629 auto p = g_list_nth(gList, n); 630 631 if(p is null) 632 { 633 return null; 634 } 635 636 return new ListG(cast(GList*) p); 637 } 638 639 /** 640 * Gets the data of the element at the given position. 641 * 642 * This iterates over the list until it reaches the @n-th position. If you 643 * intend to iterate over every element, it is better to use a for-loop as 644 * described in the #GList introduction. 645 * 646 * Params: 647 * n = the position of the element 648 * 649 * Returns: the element's data, or %NULL if the position 650 * is off the end of the #GList 651 */ 652 public void* nthData(uint n) 653 { 654 return g_list_nth_data(gList, n); 655 } 656 657 /** 658 * Gets the element @n places before @list. 659 * 660 * Params: 661 * n = the position of the element, counting from 0 662 * 663 * Returns: the element, or %NULL if the position is 664 * off the end of the #GList 665 */ 666 public ListG nthPrev(uint n) 667 { 668 auto p = g_list_nth_prev(gList, n); 669 670 if(p is null) 671 { 672 return null; 673 } 674 675 return new ListG(cast(GList*) p); 676 } 677 678 /** 679 * Gets the position of the given element 680 * in the #GList (starting from 0). 681 * 682 * Params: 683 * llink = an element in the #GList 684 * 685 * Returns: the position of the element in the #GList, 686 * or -1 if the element is not found 687 */ 688 public int position(ListG llink) 689 { 690 return g_list_position(gList, (llink is null) ? null : llink.getListGStruct()); 691 } 692 693 /** 694 * Prepends a new element on to the start of the list. 695 * 696 * Note that the return value is the new start of the list, 697 * which will have changed, so make sure you store the new value. 698 * 699 * |[<!-- language="C" --> 700 * // Notice that it is initialized to the empty list. 701 * GList *list = NULL; 702 * 703 * list = g_list_prepend (list, "last"); 704 * list = g_list_prepend (list, "first"); 705 * ]| 706 * 707 * Do not use this function to prepend a new element to a different 708 * element than the start of the list. Use g_list_insert_before() instead. 709 * 710 * Params: 711 * data = the data for the new element 712 * 713 * Returns: a pointer to the newly prepended element, which is the new 714 * start of the #GList 715 */ 716 public ListG prepend(void* data) 717 { 718 auto p = g_list_prepend(gList, data); 719 720 if(p is null) 721 { 722 return null; 723 } 724 725 return new ListG(cast(GList*) p); 726 } 727 728 /** 729 * Removes an element from a #GList. 730 * If two elements contain the same data, only the first is removed. 731 * If none of the elements contain the data, the #GList is unchanged. 732 * 733 * Params: 734 * data = the data of the element to remove 735 * 736 * Returns: the (possibly changed) start of the #GList 737 */ 738 public ListG remove(void* data) 739 { 740 auto p = g_list_remove(gList, data); 741 742 if(p is null) 743 { 744 return null; 745 } 746 747 return new ListG(cast(GList*) p); 748 } 749 750 /** 751 * Removes all list nodes with data equal to @data. 752 * Returns the new head of the list. Contrast with 753 * g_list_remove() which removes only the first node 754 * matching the given data. 755 * 756 * Params: 757 * data = data to remove 758 * 759 * Returns: the (possibly changed) start of the #GList 760 */ 761 public ListG removeAll(void* data) 762 { 763 auto p = g_list_remove_all(gList, data); 764 765 if(p is null) 766 { 767 return null; 768 } 769 770 return new ListG(cast(GList*) p); 771 } 772 773 /** 774 * Removes an element from a #GList, without freeing the element. 775 * The removed element's prev and next links are set to %NULL, so 776 * that it becomes a self-contained list with one element. 777 * 778 * This function is for example used to move an element in the list 779 * (see the example for g_list_concat()) or to remove an element in 780 * the list before freeing its data: 781 * |[<!-- language="C" --> 782 * list = g_list_remove_link (list, llink); 783 * free_some_data_that_may_access_the_list_again (llink->data); 784 * g_list_free (llink); 785 * ]| 786 * 787 * Params: 788 * llink = an element in the #GList 789 * 790 * Returns: the (possibly changed) start of the #GList 791 */ 792 public ListG removeLink(ListG llink) 793 { 794 auto p = g_list_remove_link(gList, (llink is null) ? null : llink.getListGStruct()); 795 796 if(p is null) 797 { 798 return null; 799 } 800 801 return new ListG(cast(GList*) p); 802 } 803 804 /** 805 * Reverses a #GList. 806 * It simply switches the next and prev pointers of each element. 807 * 808 * Returns: the start of the reversed #GList 809 */ 810 public ListG reverse() 811 { 812 auto p = g_list_reverse(gList); 813 814 if(p is null) 815 { 816 return null; 817 } 818 819 return new ListG(cast(GList*) p); 820 } 821 822 /** 823 * Sorts a #GList using the given comparison function. The algorithm 824 * used is a stable sort. 825 * 826 * Params: 827 * compareFunc = the comparison function used to sort the #GList. 828 * This function is passed the data from 2 elements of the #GList 829 * and should return 0 if they are equal, a negative value if the 830 * first element comes before the second, or a positive value if 831 * the first element comes after the second. 832 * 833 * Returns: the (possibly changed) start of the #GList 834 */ 835 public ListG sort(GCompareFunc compareFunc) 836 { 837 auto p = g_list_sort(gList, compareFunc); 838 839 if(p is null) 840 { 841 return null; 842 } 843 844 return new ListG(cast(GList*) p); 845 } 846 847 /** 848 * Like g_list_sort(), but the comparison function accepts 849 * a user data argument. 850 * 851 * Params: 852 * compareFunc = comparison function 853 * userData = user data to pass to comparison function 854 * 855 * Returns: the (possibly changed) start of the #GList 856 */ 857 public ListG sortWithData(GCompareDataFunc compareFunc, void* userData) 858 { 859 auto p = g_list_sort_with_data(gList, compareFunc, userData); 860 861 if(p is null) 862 { 863 return null; 864 } 865 866 return new ListG(cast(GList*) p); 867 } 868 }