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.ListSG; 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 #GSList struct is used for each element in the singly-linked 36 * list. 37 */ 38 public class ListSG 39 { 40 /** the main Gtk struct */ 41 protected GSList* gSList; 42 protected bool ownedRef; 43 44 /** Get the main Gtk struct */ 45 public GSList* getListSGStruct(bool transferOwnership = false) 46 { 47 if (transferOwnership) 48 ownedRef = false; 49 return gSList; 50 } 51 52 /** the main Gtk struct as a void* */ 53 protected void* getStruct() 54 { 55 return cast(void*)gSList; 56 } 57 58 /** 59 * Sets our main struct and passes it to the parent class. 60 */ 61 public this (GSList* gSList, bool ownedRef = false) 62 { 63 this.gSList = gSList; 64 this.ownedRef = ownedRef; 65 } 66 67 /** */ 68 @property void* data() 69 { 70 return gSList.data; 71 } 72 73 /** 74 * get the next element 75 * Returns: the next element, or NULL if there are no more elements. 76 */ 77 @property ListSG next() 78 { 79 if ( gSList.next is null ) 80 { 81 return null; 82 } 83 84 return new ListSG(gSList.next); 85 } 86 87 /** 88 * Turn the list into a D array of the desiered type. 89 * Type T wraps should match the type of the data. 90 */ 91 public T[] toArray(T, TC = getCType!T)() 92 if ( is(T == class) ) 93 { 94 T[] arr = new T[length()]; 95 ListSG list = this; 96 size_t count; 97 98 while(list !is null && count < arr.length) 99 { 100 arr[count] = ObjectG.getDObject!(T)(cast(TC)list.data); 101 list = list.next(); 102 count++; 103 } 104 105 return arr; 106 } 107 108 /** Ditto */ 109 public T[] toArray(T)() 110 if ( is ( T == string ) ) 111 { 112 T[] arr = new T[length()]; 113 ListSG list = this; 114 size_t count; 115 116 while(list !is null && count < arr.length) 117 { 118 arr[count] = Str.toString(cast(char*)list.data); 119 list = list.next(); 120 count++; 121 } 122 123 return arr; 124 } 125 126 private template getCType(T) 127 { 128 static if ( is(T == class) ) 129 alias getCType = typeof(T.tupleof[0]); 130 else 131 alias getCType = void*; 132 } 133 134 unittest 135 { 136 import gobject.Value; 137 138 auto list = new ListSG(null); 139 list = list.append(new Value(0).getValueStruct()); 140 list = list.append(new Value(1).getValueStruct()); 141 auto arr = list.toArray!Value(); 142 143 assert(arr[0].getInt() == 0); 144 assert(arr[1].getInt() == 1); 145 146 list = new ListSG(null); 147 list = list.append(cast(void*)"test\0".ptr); 148 list = list.append(cast(void*)"test2\0".ptr); 149 150 assert(["test", "test2"] == list.toArray!string()); 151 } 152 153 /** 154 */ 155 156 /** 157 * Allocates space for one #GSList element. It is called by the 158 * g_slist_append(), g_slist_prepend(), g_slist_insert() and 159 * g_slist_insert_sorted() functions and so is rarely used on its own. 160 * 161 * Returns: a pointer to the newly-allocated #GSList element. 162 */ 163 public static ListSG alloc() 164 { 165 auto __p = g_slist_alloc(); 166 167 if(__p is null) 168 { 169 return null; 170 } 171 172 return new ListSG(cast(GSList*) __p); 173 } 174 175 /** 176 * Adds a new element on to the end of the list. 177 * 178 * The return value is the new start of the list, which may 179 * have changed, so make sure you store the new value. 180 * 181 * Note that g_slist_append() has to traverse the entire list 182 * to find the end, which is inefficient when adding multiple 183 * elements. A common idiom to avoid the inefficiency is to prepend 184 * the elements and reverse the list when all elements have been added. 185 * 186 * |[<!-- language="C" --> 187 * // Notice that these are initialized to the empty list. 188 * GSList *list = NULL, *number_list = NULL; 189 * 190 * // This is a list of strings. 191 * list = g_slist_append (list, "first"); 192 * list = g_slist_append (list, "second"); 193 * 194 * // This is a list of integers. 195 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27)); 196 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14)); 197 * ]| 198 * 199 * Params: 200 * data = the data for the new element 201 * 202 * Returns: the new start of the #GSList 203 */ 204 public ListSG append(void* data) 205 { 206 auto __p = g_slist_append(gSList, data); 207 208 if(__p is null) 209 { 210 return null; 211 } 212 213 return new ListSG(cast(GSList*) __p); 214 } 215 216 /** 217 * Adds the second #GSList onto the end of the first #GSList. 218 * Note that the elements of the second #GSList are not copied. 219 * They are used directly. 220 * 221 * Params: 222 * list2 = the #GSList to add to the end of the first #GSList 223 * 224 * Returns: the start of the new #GSList 225 */ 226 public ListSG concat(ListSG list2) 227 { 228 auto __p = g_slist_concat(gSList, (list2 is null) ? null : list2.getListSGStruct()); 229 230 if(__p is null) 231 { 232 return null; 233 } 234 235 return new ListSG(cast(GSList*) __p); 236 } 237 238 /** 239 * Copies a #GSList. 240 * 241 * Note that this is a "shallow" copy. If the list elements 242 * consist of pointers to data, the pointers are copied but 243 * the actual data isn't. See g_slist_copy_deep() if you need 244 * to copy the data as well. 245 * 246 * Returns: a copy of @list 247 */ 248 public ListSG copy() 249 { 250 auto __p = g_slist_copy(gSList); 251 252 if(__p is null) 253 { 254 return null; 255 } 256 257 return new ListSG(cast(GSList*) __p); 258 } 259 260 /** 261 * Makes a full (deep) copy of a #GSList. 262 * 263 * In contrast with g_slist_copy(), this function uses @func to make a copy of 264 * each list element, in addition to copying the list container itself. 265 * 266 * @func, as a #GCopyFunc, takes two arguments, the data to be copied 267 * and a @user_data pointer. On common processor architectures, it's safe to 268 * pass %NULL as @user_data if the copy function takes only one argument. You 269 * may get compiler warnings from this though if compiling with GCC’s 270 * `-Wcast-function-type` warning. 271 * 272 * For instance, if @list holds a list of GObjects, you can do: 273 * |[<!-- language="C" --> 274 * another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL); 275 * ]| 276 * 277 * And, to entirely free the new list, you could do: 278 * |[<!-- language="C" --> 279 * g_slist_free_full (another_list, g_object_unref); 280 * ]| 281 * 282 * Params: 283 * func = a copy function used to copy every element in the list 284 * userData = user data passed to the copy function @func, or #NULL 285 * 286 * Returns: a full copy of @list, use g_slist_free_full() to free it 287 * 288 * Since: 2.34 289 */ 290 public ListSG copyDeep(GCopyFunc func, void* userData) 291 { 292 auto __p = g_slist_copy_deep(gSList, func, userData); 293 294 if(__p is null) 295 { 296 return null; 297 } 298 299 return new ListSG(cast(GSList*) __p); 300 } 301 302 /** 303 * Removes the node link_ from the list and frees it. 304 * Compare this to g_slist_remove_link() which removes the node 305 * without freeing it. 306 * 307 * Removing arbitrary nodes from a singly-linked list requires time 308 * that is proportional to the length of the list (ie. O(n)). If you 309 * find yourself using g_slist_delete_link() frequently, you should 310 * consider a different data structure, such as the doubly-linked 311 * #GList. 312 * 313 * Params: 314 * link = node to delete 315 * 316 * Returns: the new head of @list 317 */ 318 public ListSG deleteLink(ListSG link) 319 { 320 auto __p = g_slist_delete_link(gSList, (link is null) ? null : link.getListSGStruct()); 321 322 if(__p is null) 323 { 324 return null; 325 } 326 327 return new ListSG(cast(GSList*) __p); 328 } 329 330 /** 331 * Finds the element in a #GSList which 332 * contains the given data. 333 * 334 * Params: 335 * data = the element data to find 336 * 337 * Returns: the found #GSList element, 338 * or %NULL if it is not found 339 */ 340 public ListSG find(void* data) 341 { 342 auto __p = g_slist_find(gSList, data); 343 344 if(__p is null) 345 { 346 return null; 347 } 348 349 return new ListSG(cast(GSList*) __p); 350 } 351 352 /** 353 * Finds an element in a #GSList, using a supplied function to 354 * find the desired element. It iterates over the list, calling 355 * the given function which should return 0 when the desired 356 * element is found. The function takes two #gconstpointer arguments, 357 * the #GSList element's data as the first argument and the 358 * given user data. 359 * 360 * Params: 361 * data = user data passed to the function 362 * func = the function to call for each element. 363 * It should return 0 when the desired element is found 364 * 365 * Returns: the found #GSList element, or %NULL if it is not found 366 */ 367 public ListSG findCustom(void* data, GCompareFunc func) 368 { 369 auto __p = g_slist_find_custom(gSList, data, func); 370 371 if(__p is null) 372 { 373 return null; 374 } 375 376 return new ListSG(cast(GSList*) __p); 377 } 378 379 alias foreac = foreach_; 380 /** 381 * Calls a function for each element of a #GSList. 382 * 383 * It is safe for @func to remove the element from @list, but it must 384 * not modify any part of the list after that element. 385 * 386 * Params: 387 * func = the function to call with each element's data 388 * userData = user data to pass to the function 389 */ 390 public void foreach_(GFunc func, void* userData) 391 { 392 g_slist_foreach(gSList, func, userData); 393 } 394 395 /** 396 * Frees all of the memory used by a #GSList. 397 * The freed elements are returned to the slice allocator. 398 * 399 * If list elements contain dynamically-allocated memory, 400 * you should either use g_slist_free_full() or free them manually 401 * first. 402 * 403 * It can be combined with g_steal_pointer() to ensure the list head pointer 404 * is not left dangling: 405 * |[<!-- language="C" --> 406 * GSList *list_of_borrowed_things = …; /<!-- -->* (transfer container) *<!-- -->/ 407 * g_slist_free (g_steal_pointer (&list_of_borrowed_things)); 408 * ]| 409 */ 410 public void free() 411 { 412 g_slist_free(gSList); 413 } 414 415 /** 416 * Frees one #GSList element. 417 * It is usually used after g_slist_remove_link(). 418 */ 419 public void free1() 420 { 421 g_slist_free_1(gSList); 422 } 423 424 /** 425 * Convenience method, which frees all the memory used by a #GSList, and 426 * calls the specified destroy function on every element's data. 427 * 428 * @free_func must not modify the list (eg, by removing the freed 429 * element from it). 430 * 431 * It can be combined with g_steal_pointer() to ensure the list head pointer 432 * is not left dangling — this also has the nice property that the head pointer 433 * is cleared before any of the list elements are freed, to prevent double frees 434 * from @free_func: 435 * |[<!-- language="C" --> 436 * GSList *list_of_owned_things = …; /<!-- -->* (transfer full) (element-type GObject) *<!-- -->/ 437 * g_slist_free_full (g_steal_pointer (&list_of_owned_things), g_object_unref); 438 * ]| 439 * 440 * Params: 441 * freeFunc = the function to be called to free each element's data 442 * 443 * Since: 2.28 444 */ 445 public void freeFull(GDestroyNotify freeFunc) 446 { 447 g_slist_free_full(gSList, freeFunc); 448 } 449 450 /** 451 * Gets the position of the element containing 452 * the given data (starting from 0). 453 * 454 * Params: 455 * data = the data to find 456 * 457 * Returns: the index of the element containing the data, 458 * or -1 if the data is not found 459 */ 460 public int index(void* data) 461 { 462 return g_slist_index(gSList, data); 463 } 464 465 /** 466 * Inserts a new element into the list at the given position. 467 * 468 * Params: 469 * data = the data for the new element 470 * position = the position to insert the element. 471 * If this is negative, or is larger than the number 472 * of elements in the list, the new element is added on 473 * to the end of the list. 474 * 475 * Returns: the new start of the #GSList 476 */ 477 public ListSG insert(void* data, int position) 478 { 479 auto __p = g_slist_insert(gSList, data, position); 480 481 if(__p is null) 482 { 483 return null; 484 } 485 486 return new ListSG(cast(GSList*) __p); 487 } 488 489 /** 490 * Inserts a node before @sibling containing @data. 491 * 492 * Params: 493 * sibling = node to insert @data before 494 * data = data to put in the newly-inserted node 495 * 496 * Returns: the new head of the list. 497 */ 498 public ListSG insertBefore(ListSG sibling, void* data) 499 { 500 auto __p = g_slist_insert_before(gSList, (sibling is null) ? null : sibling.getListSGStruct(), data); 501 502 if(__p is null) 503 { 504 return null; 505 } 506 507 return new ListSG(cast(GSList*) __p); 508 } 509 510 /** 511 * Inserts a new element into the list, using the given 512 * comparison function to determine its position. 513 * 514 * Params: 515 * data = the data for the new element 516 * func = the function to compare elements in the list. 517 * It should return a number > 0 if the first parameter 518 * comes after the second parameter in the sort order. 519 * 520 * Returns: the new start of the #GSList 521 */ 522 public ListSG insertSorted(void* data, GCompareFunc func) 523 { 524 auto __p = g_slist_insert_sorted(gSList, data, func); 525 526 if(__p is null) 527 { 528 return null; 529 } 530 531 return new ListSG(cast(GSList*) __p); 532 } 533 534 /** 535 * Inserts a new element into the list, using the given 536 * comparison function to determine its position. 537 * 538 * Params: 539 * data = the data for the new element 540 * func = the function to compare elements in the list. 541 * It should return a number > 0 if the first parameter 542 * comes after the second parameter in the sort order. 543 * userData = data to pass to comparison function 544 * 545 * Returns: the new start of the #GSList 546 * 547 * Since: 2.10 548 */ 549 public ListSG insertSortedWithData(void* data, GCompareDataFunc func, void* userData) 550 { 551 auto __p = g_slist_insert_sorted_with_data(gSList, data, func, userData); 552 553 if(__p is null) 554 { 555 return null; 556 } 557 558 return new ListSG(cast(GSList*) __p); 559 } 560 561 /** 562 * Gets the last element in a #GSList. 563 * 564 * This function iterates over the whole list. 565 * 566 * Returns: the last element in the #GSList, 567 * or %NULL if the #GSList has no elements 568 */ 569 public ListSG last() 570 { 571 auto __p = g_slist_last(gSList); 572 573 if(__p is null) 574 { 575 return null; 576 } 577 578 return new ListSG(cast(GSList*) __p); 579 } 580 581 /** 582 * Gets the number of elements in a #GSList. 583 * 584 * This function iterates over the whole list to 585 * count its elements. To check whether the list is non-empty, it is faster to 586 * check @list against %NULL. 587 * 588 * Returns: the number of elements in the #GSList 589 */ 590 public uint length() 591 { 592 return g_slist_length(gSList); 593 } 594 595 /** 596 * Gets the element at the given position in a #GSList. 597 * 598 * Params: 599 * n = the position of the element, counting from 0 600 * 601 * Returns: the element, or %NULL if the position is off 602 * the end of the #GSList 603 */ 604 public ListSG nth(uint n) 605 { 606 auto __p = g_slist_nth(gSList, n); 607 608 if(__p is null) 609 { 610 return null; 611 } 612 613 return new ListSG(cast(GSList*) __p); 614 } 615 616 /** 617 * Gets the data of the element at the given position. 618 * 619 * Params: 620 * n = the position of the element 621 * 622 * Returns: the element's data, or %NULL if the position 623 * is off the end of the #GSList 624 */ 625 public void* nthData(uint n) 626 { 627 return g_slist_nth_data(gSList, n); 628 } 629 630 /** 631 * Gets the position of the given element 632 * in the #GSList (starting from 0). 633 * 634 * Params: 635 * llink = an element in the #GSList 636 * 637 * Returns: the position of the element in the #GSList, 638 * or -1 if the element is not found 639 */ 640 public int position(ListSG llink) 641 { 642 return g_slist_position(gSList, (llink is null) ? null : llink.getListSGStruct()); 643 } 644 645 /** 646 * Adds a new element on to the start of the list. 647 * 648 * The return value is the new start of the list, which 649 * may have changed, so make sure you store the new value. 650 * 651 * |[<!-- language="C" --> 652 * // Notice that it is initialized to the empty list. 653 * GSList *list = NULL; 654 * list = g_slist_prepend (list, "last"); 655 * list = g_slist_prepend (list, "first"); 656 * ]| 657 * 658 * Params: 659 * data = the data for the new element 660 * 661 * Returns: the new start of the #GSList 662 */ 663 public ListSG prepend(void* data) 664 { 665 auto __p = g_slist_prepend(gSList, data); 666 667 if(__p is null) 668 { 669 return null; 670 } 671 672 return new ListSG(cast(GSList*) __p); 673 } 674 675 /** 676 * Removes an element from a #GSList. 677 * If two elements contain the same data, only the first is removed. 678 * If none of the elements contain the data, the #GSList is unchanged. 679 * 680 * Params: 681 * data = the data of the element to remove 682 * 683 * Returns: the new start of the #GSList 684 */ 685 public ListSG remove(void* data) 686 { 687 auto __p = g_slist_remove(gSList, data); 688 689 if(__p is null) 690 { 691 return null; 692 } 693 694 return new ListSG(cast(GSList*) __p); 695 } 696 697 /** 698 * Removes all list nodes with data equal to @data. 699 * Returns the new head of the list. Contrast with 700 * g_slist_remove() which removes only the first node 701 * matching the given data. 702 * 703 * Params: 704 * data = data to remove 705 * 706 * Returns: new head of @list 707 */ 708 public ListSG removeAll(void* data) 709 { 710 auto __p = g_slist_remove_all(gSList, data); 711 712 if(__p is null) 713 { 714 return null; 715 } 716 717 return new ListSG(cast(GSList*) __p); 718 } 719 720 /** 721 * Removes an element from a #GSList, without 722 * freeing the element. The removed element's next 723 * link is set to %NULL, so that it becomes a 724 * self-contained list with one element. 725 * 726 * Removing arbitrary nodes from a singly-linked list 727 * requires time that is proportional to the length of the list 728 * (ie. O(n)). If you find yourself using g_slist_remove_link() 729 * frequently, you should consider a different data structure, 730 * such as the doubly-linked #GList. 731 * 732 * Params: 733 * link = an element in the #GSList 734 * 735 * Returns: the new start of the #GSList, without the element 736 */ 737 public ListSG removeLink(ListSG link) 738 { 739 auto __p = g_slist_remove_link(gSList, (link is null) ? null : link.getListSGStruct()); 740 741 if(__p is null) 742 { 743 return null; 744 } 745 746 return new ListSG(cast(GSList*) __p); 747 } 748 749 /** 750 * Reverses a #GSList. 751 * 752 * Returns: the start of the reversed #GSList 753 */ 754 public ListSG reverse() 755 { 756 auto __p = g_slist_reverse(gSList); 757 758 if(__p is null) 759 { 760 return null; 761 } 762 763 return new ListSG(cast(GSList*) __p); 764 } 765 766 /** 767 * Sorts a #GSList using the given comparison function. The algorithm 768 * used is a stable sort. 769 * 770 * Params: 771 * compareFunc = the comparison function used to sort the #GSList. 772 * This function is passed the data from 2 elements of the #GSList 773 * and should return 0 if they are equal, a negative value if the 774 * first element comes before the second, or a positive value if 775 * the first element comes after the second. 776 * 777 * Returns: the start of the sorted #GSList 778 */ 779 public ListSG sort(GCompareFunc compareFunc) 780 { 781 auto __p = g_slist_sort(gSList, compareFunc); 782 783 if(__p is null) 784 { 785 return null; 786 } 787 788 return new ListSG(cast(GSList*) __p); 789 } 790 791 /** 792 * Like g_slist_sort(), but the sort function accepts a user data argument. 793 * 794 * Params: 795 * compareFunc = comparison function 796 * userData = data to pass to comparison function 797 * 798 * Returns: new head of the list 799 */ 800 public ListSG sortWithData(GCompareDataFunc compareFunc, void* userData) 801 { 802 auto __p = g_slist_sort_with_data(gSList, compareFunc, userData); 803 804 if(__p is null) 805 { 806 return null; 807 } 808 809 return new ListSG(cast(GSList*) __p); 810 } 811 812 /** 813 * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy. 814 * 815 * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing. 816 * 817 * Params: 818 * slistPtr = a #GSList return location 819 * destroy = the function to pass to g_slist_free_full() or %NULL to not free elements 820 * 821 * Since: 2.64 822 */ 823 public static void clearSlist(out ListSG slistPtr, GDestroyNotify destroy) 824 { 825 GSList* outslistPtr = null; 826 827 g_clear_slist(&outslistPtr, destroy); 828 829 slistPtr = new ListSG(outslistPtr); 830 } 831 }