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