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