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.PtrArray; 26 27 private import glib.ConstructionException; 28 private import glib.c.functions; 29 public import glib.c.types; 30 public import gtkc.glibtypes; 31 32 33 /** 34 * Contains the public fields of a pointer array. 35 */ 36 public class PtrArray 37 { 38 /** the main Gtk struct */ 39 protected GPtrArray* gPtrArray; 40 protected bool ownedRef; 41 42 /** Get the main Gtk struct */ 43 public GPtrArray* getPtrArrayStruct(bool transferOwnership = false) 44 { 45 if (transferOwnership) 46 ownedRef = false; 47 return gPtrArray; 48 } 49 50 /** the main Gtk struct as a void* */ 51 protected void* getStruct() 52 { 53 return cast(void*)gPtrArray; 54 } 55 56 /** 57 * Sets our main struct and passes it to the parent class. 58 */ 59 public this (GPtrArray* gPtrArray, bool ownedRef = false) 60 { 61 this.gPtrArray = gPtrArray; 62 this.ownedRef = ownedRef; 63 } 64 65 /** 66 * Number of pointers in the array 67 */ 68 public uint len() pure 69 { 70 return gPtrArray.len; 71 } 72 73 /** 74 * Returns the pointer at the given index of the pointer array. 75 * 76 * This does not perform bounds checking on the given index, so 77 * you are responsible for checking it against the array length. 78 */ 79 public void* index(uint idx) 80 { 81 return (gPtrArray.pdata)[idx]; 82 } 83 84 /** 85 */ 86 87 /** 88 * Adds a pointer to the end of the pointer array. The array will grow 89 * in size automatically if necessary. 90 * 91 * Params: 92 * data = the pointer to add 93 */ 94 public void add(void* data) 95 { 96 g_ptr_array_add(gPtrArray, data); 97 } 98 99 /** 100 * Makes a full (deep) copy of a #GPtrArray. 101 * 102 * @func, as a #GCopyFunc, takes two arguments, the data to be copied 103 * and a @user_data pointer. On common processor architectures, it's safe to 104 * pass %NULL as @user_data if the copy function takes only one argument. You 105 * may get compiler warnings from this though if compiling with GCC’s 106 * `-Wcast-function-type` warning. 107 * 108 * If @func is %NULL, then only the pointers (and not what they are 109 * pointing to) are copied to the new #GPtrArray. 110 * 111 * The copy of @array will have the same #GDestroyNotify for its elements as 112 * @array. 113 * 114 * Params: 115 * func = a copy function used to copy every element in the array 116 * userData = user data passed to the copy function @func, or %NULL 117 * 118 * Returns: a deep copy of the initial #GPtrArray. 119 * 120 * Since: 2.62 121 */ 122 public PtrArray copy(GCopyFunc func, void* userData) 123 { 124 auto __p = g_ptr_array_copy(gPtrArray, func, userData); 125 126 if(__p is null) 127 { 128 return null; 129 } 130 131 return new PtrArray(cast(GPtrArray*) __p, true); 132 } 133 134 /** 135 * Adds all pointers of @array to the end of the array @array_to_extend. 136 * The array will grow in size automatically if needed. @array_to_extend is 137 * modified in-place. 138 * 139 * @func, as a #GCopyFunc, takes two arguments, the data to be copied 140 * and a @user_data pointer. On common processor architectures, it's safe to 141 * pass %NULL as @user_data if the copy function takes only one argument. You 142 * may get compiler warnings from this though if compiling with GCC’s 143 * `-Wcast-function-type` warning. 144 * 145 * If @func is %NULL, then only the pointers (and not what they are 146 * pointing to) are copied to the new #GPtrArray. 147 * 148 * Params: 149 * array = a #GPtrArray to add to the end of @array_to_extend. 150 * func = a copy function used to copy every element in the array 151 * userData = user data passed to the copy function @func, or %NULL 152 * 153 * Since: 2.62 154 */ 155 public void extend(PtrArray array, GCopyFunc func, void* userData) 156 { 157 g_ptr_array_extend(gPtrArray, (array is null) ? null : array.getPtrArrayStruct(), func, userData); 158 } 159 160 /** 161 * Adds all the pointers in @array to the end of @array_to_extend, transferring 162 * ownership of each element from @array to @array_to_extend and modifying 163 * @array_to_extend in-place. @array is then freed. 164 * 165 * As with g_ptr_array_free(), @array will be destroyed if its reference count 166 * is 1. If its reference count is higher, it will be decremented and the 167 * length of @array set to zero. 168 * 169 * Params: 170 * array = a #GPtrArray to add to the end of 171 * @array_to_extend. 172 * 173 * Since: 2.62 174 */ 175 public void extendAndSteal(PtrArray array) 176 { 177 g_ptr_array_extend_and_steal(gPtrArray, (array is null) ? null : array.getPtrArrayStruct()); 178 } 179 180 /** 181 * Checks whether @needle exists in @haystack. If the element is found, %TRUE is 182 * returned and the element’s index is returned in @index_ (if non-%NULL). 183 * Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists 184 * multiple times in @haystack, the index of the first instance is returned. 185 * 186 * This does pointer comparisons only. If you want to use more complex equality 187 * checks, such as string comparisons, use g_ptr_array_find_with_equal_func(). 188 * 189 * Params: 190 * needle = pointer to look for 191 * index = return location for the index of 192 * the element, if found 193 * 194 * Returns: %TRUE if @needle is one of the elements of @haystack 195 * 196 * Since: 2.54 197 */ 198 public bool find(void* needle, out uint index) 199 { 200 return g_ptr_array_find(gPtrArray, needle, &index) != 0; 201 } 202 203 /** 204 * Checks whether @needle exists in @haystack, using the given @equal_func. 205 * If the element is found, %TRUE is returned and the element’s index is 206 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_ 207 * is undefined. If @needle exists multiple times in @haystack, the index of 208 * the first instance is returned. 209 * 210 * @equal_func is called with the element from the array as its first parameter, 211 * and @needle as its second parameter. If @equal_func is %NULL, pointer 212 * equality is used. 213 * 214 * Params: 215 * needle = pointer to look for 216 * equalFunc = the function to call for each element, which should 217 * return %TRUE when the desired element is found; or %NULL to use pointer 218 * equality 219 * index = return location for the index of 220 * the element, if found 221 * 222 * Returns: %TRUE if @needle is one of the elements of @haystack 223 * 224 * Since: 2.54 225 */ 226 public bool findWithEqualFunc(void* needle, GEqualFunc equalFunc, out uint index) 227 { 228 return g_ptr_array_find_with_equal_func(gPtrArray, needle, equalFunc, &index) != 0; 229 } 230 231 alias foreac = foreach_; 232 /** 233 * Calls a function for each element of a #GPtrArray. @func must not 234 * add elements to or remove elements from the array. 235 * 236 * Params: 237 * func = the function to call for each array element 238 * userData = user data to pass to the function 239 * 240 * Since: 2.4 241 */ 242 public void foreach_(GFunc func, void* userData) 243 { 244 g_ptr_array_foreach(gPtrArray, func, userData); 245 } 246 247 /** 248 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE 249 * it frees the memory block holding the elements as well. Pass %FALSE 250 * if you want to free the #GPtrArray wrapper but preserve the 251 * underlying array for use elsewhere. If the reference count of @array 252 * is greater than one, the #GPtrArray wrapper is preserved but the 253 * size of @array will be set to zero. 254 * 255 * If array contents point to dynamically-allocated memory, they should 256 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify 257 * function has been set for @array. 258 * 259 * This function is not thread-safe. If using a #GPtrArray from multiple 260 * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref() 261 * functions. 262 * 263 * Params: 264 * freeSeg = if %TRUE the actual pointer array is freed as well 265 * 266 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL. 267 * The pointer array should be freed using g_free(). 268 */ 269 public void** free(bool freeSeg) 270 { 271 return g_ptr_array_free(gPtrArray, freeSeg); 272 } 273 274 /** 275 * Inserts an element into the pointer array at the given index. The 276 * array will grow in size automatically if necessary. 277 * 278 * Params: 279 * index = the index to place the new element at, or -1 to append 280 * data = the pointer to add. 281 * 282 * Since: 2.40 283 */ 284 public void insert(int index, void* data) 285 { 286 g_ptr_array_insert(gPtrArray, index, data); 287 } 288 289 /** 290 * Creates a new #GPtrArray with a reference count of 1. 291 * 292 * Returns: the new #GPtrArray 293 * 294 * Throws: ConstructionException GTK+ fails to create the object. 295 */ 296 public this() 297 { 298 auto __p = g_ptr_array_new(); 299 300 if(__p is null) 301 { 302 throw new ConstructionException("null returned by new"); 303 } 304 305 this(cast(GPtrArray*) __p); 306 } 307 308 /** 309 * Creates a new #GPtrArray with @reserved_size pointers preallocated 310 * and a reference count of 1. This avoids frequent reallocation, if 311 * you are going to add many pointers to the array. Note however that 312 * the size of the array is still 0. It also set @element_free_func 313 * for freeing each element when the array is destroyed either via 314 * g_ptr_array_unref(), when g_ptr_array_free() is called with 315 * @free_segment set to %TRUE or when removing elements. 316 * 317 * Params: 318 * reservedSize = number of pointers preallocated 319 * elementFreeFunc = A function to free elements with 320 * destroy @array or %NULL 321 * 322 * Returns: A new #GPtrArray 323 * 324 * Since: 2.30 325 * 326 * Throws: ConstructionException GTK+ fails to create the object. 327 */ 328 public this(uint reservedSize, GDestroyNotify elementFreeFunc) 329 { 330 auto __p = g_ptr_array_new_full(reservedSize, elementFreeFunc); 331 332 if(__p is null) 333 { 334 throw new ConstructionException("null returned by new_full"); 335 } 336 337 this(cast(GPtrArray*) __p); 338 } 339 340 /** 341 * Creates a new #GPtrArray with a reference count of 1 and use 342 * @element_free_func for freeing each element when the array is destroyed 343 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with 344 * @free_segment set to %TRUE or when removing elements. 345 * 346 * Params: 347 * elementFreeFunc = A function to free elements with 348 * destroy @array or %NULL 349 * 350 * Returns: A new #GPtrArray 351 * 352 * Since: 2.22 353 * 354 * Throws: ConstructionException GTK+ fails to create the object. 355 */ 356 public this(GDestroyNotify elementFreeFunc) 357 { 358 auto __p = g_ptr_array_new_with_free_func(elementFreeFunc); 359 360 if(__p is null) 361 { 362 throw new ConstructionException("null returned by new_with_free_func"); 363 } 364 365 this(cast(GPtrArray*) __p); 366 } 367 368 alias doref = ref_; 369 /** 370 * Atomically increments the reference count of @array by one. 371 * This function is thread-safe and may be called from any thread. 372 * 373 * Returns: The passed in #GPtrArray 374 * 375 * Since: 2.22 376 */ 377 public PtrArray ref_() 378 { 379 auto __p = g_ptr_array_ref(gPtrArray); 380 381 if(__p is null) 382 { 383 return null; 384 } 385 386 return new PtrArray(cast(GPtrArray*) __p); 387 } 388 389 /** 390 * Removes the first occurrence of the given pointer from the pointer 391 * array. The following elements are moved down one place. If @array 392 * has a non-%NULL #GDestroyNotify function it is called for the 393 * removed element. 394 * 395 * It returns %TRUE if the pointer was removed, or %FALSE if the 396 * pointer was not found. 397 * 398 * Params: 399 * data = the pointer to remove 400 * 401 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer 402 * is not found in the array 403 */ 404 public bool remove(void* data) 405 { 406 return g_ptr_array_remove(gPtrArray, data) != 0; 407 } 408 409 /** 410 * Removes the first occurrence of the given pointer from the pointer 411 * array. The last element in the array is used to fill in the space, 412 * so this function does not preserve the order of the array. But it 413 * is faster than g_ptr_array_remove(). If @array has a non-%NULL 414 * #GDestroyNotify function it is called for the removed element. 415 * 416 * It returns %TRUE if the pointer was removed, or %FALSE if the 417 * pointer was not found. 418 * 419 * Params: 420 * data = the pointer to remove 421 * 422 * Returns: %TRUE if the pointer was found in the array 423 */ 424 public bool removeFast(void* data) 425 { 426 return g_ptr_array_remove_fast(gPtrArray, data) != 0; 427 } 428 429 /** 430 * Removes the pointer at the given index from the pointer array. 431 * The following elements are moved down one place. If @array has 432 * a non-%NULL #GDestroyNotify function it is called for the removed 433 * element. If so, the return value from this function will potentially point 434 * to freed memory (depending on the #GDestroyNotify implementation). 435 * 436 * Params: 437 * index = the index of the pointer to remove 438 * 439 * Returns: the pointer which was removed 440 */ 441 public void* removeIndex(uint index) 442 { 443 return g_ptr_array_remove_index(gPtrArray, index); 444 } 445 446 /** 447 * Removes the pointer at the given index from the pointer array. 448 * The last element in the array is used to fill in the space, so 449 * this function does not preserve the order of the array. But it 450 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL 451 * #GDestroyNotify function it is called for the removed element. If so, the 452 * return value from this function will potentially point to freed memory 453 * (depending on the #GDestroyNotify implementation). 454 * 455 * Params: 456 * index = the index of the pointer to remove 457 * 458 * Returns: the pointer which was removed 459 */ 460 public void* removeIndexFast(uint index) 461 { 462 return g_ptr_array_remove_index_fast(gPtrArray, index); 463 } 464 465 /** 466 * Removes the given number of pointers starting at the given index 467 * from a #GPtrArray. The following elements are moved to close the 468 * gap. If @array has a non-%NULL #GDestroyNotify function it is 469 * called for the removed elements. 470 * 471 * Params: 472 * index = the index of the first pointer to remove 473 * length = the number of pointers to remove 474 * 475 * Returns: the @array 476 * 477 * Since: 2.4 478 */ 479 public PtrArray removeRange(uint index, uint length) 480 { 481 auto __p = g_ptr_array_remove_range(gPtrArray, index, length); 482 483 if(__p is null) 484 { 485 return null; 486 } 487 488 return new PtrArray(cast(GPtrArray*) __p); 489 } 490 491 /** 492 * Sets a function for freeing each element when @array is destroyed 493 * either via g_ptr_array_unref(), when g_ptr_array_free() is called 494 * with @free_segment set to %TRUE or when removing elements. 495 * 496 * Params: 497 * elementFreeFunc = A function to free elements with 498 * destroy @array or %NULL 499 * 500 * Since: 2.22 501 */ 502 public void setFreeFunc(GDestroyNotify elementFreeFunc) 503 { 504 g_ptr_array_set_free_func(gPtrArray, elementFreeFunc); 505 } 506 507 /** 508 * Sets the size of the array. When making the array larger, 509 * newly-added elements will be set to %NULL. When making it smaller, 510 * if @array has a non-%NULL #GDestroyNotify function then it will be 511 * called for the removed elements. 512 * 513 * Params: 514 * length = the new length of the pointer array 515 */ 516 public void setSize(int length) 517 { 518 g_ptr_array_set_size(gPtrArray, length); 519 } 520 521 /** 522 * Creates a new #GPtrArray with @reserved_size pointers preallocated 523 * and a reference count of 1. This avoids frequent reallocation, if 524 * you are going to add many pointers to the array. Note however that 525 * the size of the array is still 0. 526 * 527 * Params: 528 * reservedSize = number of pointers preallocated 529 * 530 * Returns: the new #GPtrArray 531 */ 532 public static PtrArray sizedNew(uint reservedSize) 533 { 534 auto __p = g_ptr_array_sized_new(reservedSize); 535 536 if(__p is null) 537 { 538 return null; 539 } 540 541 return new PtrArray(cast(GPtrArray*) __p); 542 } 543 544 /** 545 * Sorts the array, using @compare_func which should be a qsort()-style 546 * comparison function (returns less than zero for first arg is less 547 * than second arg, zero for equal, greater than zero if irst arg is 548 * greater than second arg). 549 * 550 * Note that the comparison function for g_ptr_array_sort() doesn't 551 * take the pointers from the array as arguments, it takes pointers to 552 * the pointers in the array. Here is a full example of usage: 553 * 554 * |[<!-- language="C" --> 555 * typedef struct 556 * { 557 * gchar *name; 558 * gint size; 559 * } FileListEntry; 560 * 561 * static gint 562 * sort_filelist (gconstpointer a, gconstpointer b) 563 * { 564 * const FileListEntry *entry1 = *((FileListEntry **) a); 565 * const FileListEntry *entry2 = *((FileListEntry **) b); 566 * 567 * return g_ascii_strcasecmp (entry1->name, entry2->name); 568 * } 569 * 570 * … 571 * g_autoptr (GPtrArray) file_list = NULL; 572 * 573 * // initialize file_list array and load with many FileListEntry entries 574 * ... 575 * // now sort it with 576 * g_ptr_array_sort (file_list, sort_filelist); 577 * ]| 578 * 579 * This is guaranteed to be a stable sort since version 2.32. 580 * 581 * Params: 582 * compareFunc = comparison function 583 */ 584 public void sort(GCompareFunc compareFunc) 585 { 586 g_ptr_array_sort(gPtrArray, compareFunc); 587 } 588 589 /** 590 * Like g_ptr_array_sort(), but the comparison function has an extra 591 * user data argument. 592 * 593 * Note that the comparison function for g_ptr_array_sort_with_data() 594 * doesn't take the pointers from the array as arguments, it takes 595 * pointers to the pointers in the array. Here is a full example of use: 596 * 597 * |[<!-- language="C" --> 598 * typedef enum { SORT_NAME, SORT_SIZE } SortMode; 599 * 600 * typedef struct 601 * { 602 * gchar *name; 603 * gint size; 604 * } FileListEntry; 605 * 606 * static gint 607 * sort_filelist (gconstpointer a, gconstpointer b, gpointer user_data) 608 * { 609 * gint order; 610 * const SortMode sort_mode = GPOINTER_TO_INT (user_data); 611 * const FileListEntry *entry1 = *((FileListEntry **) a); 612 * const FileListEntry *entry2 = *((FileListEntry **) b); 613 * 614 * switch (sort_mode) 615 * { 616 * case SORT_NAME: 617 * order = g_ascii_strcasecmp (entry1->name, entry2->name); 618 * break; 619 * case SORT_SIZE: 620 * order = entry1->size - entry2->size; 621 * break; 622 * default: 623 * order = 0; 624 * break; 625 * } 626 * return order; 627 * } 628 * 629 * ... 630 * g_autoptr (GPtrArray) file_list = NULL; 631 * SortMode sort_mode; 632 * 633 * // initialize file_list array and load with many FileListEntry entries 634 * ... 635 * // now sort it with 636 * sort_mode = SORT_NAME; 637 * g_ptr_array_sort_with_data (file_list, 638 * sort_filelist, 639 * GINT_TO_POINTER (sort_mode)); 640 * ]| 641 * 642 * This is guaranteed to be a stable sort since version 2.32. 643 * 644 * Params: 645 * compareFunc = comparison function 646 * userData = data to pass to @compare_func 647 */ 648 public void sortWithData(GCompareDataFunc compareFunc, void* userData) 649 { 650 g_ptr_array_sort_with_data(gPtrArray, compareFunc, userData); 651 } 652 653 /** 654 * Frees the data in the array and resets the size to zero, while 655 * the underlying array is preserved for use elsewhere and returned 656 * to the caller. 657 * 658 * Even if set, the #GDestroyNotify function will never be called 659 * on the current contents of the array and the caller is 660 * responsible for freeing the array elements. 661 * 662 * An example of use: 663 * |[<!-- language="C" --> 664 * g_autoptr(GPtrArray) chunk_buffer = g_ptr_array_new_with_free_func (g_bytes_unref); 665 * 666 * // Some part of your application appends a number of chunks to the pointer array. 667 * g_ptr_array_add (chunk_buffer, g_bytes_new_static ("hello", 5)); 668 * g_ptr_array_add (chunk_buffer, g_bytes_new_static ("world", 5)); 669 * 670 * … 671 * 672 * // Periodically, the chunks need to be sent as an array-and-length to some 673 * // other part of the program. 674 * GBytes **chunks; 675 * gsize n_chunks; 676 * 677 * chunks = g_ptr_array_steal (chunk_buffer, &n_chunks); 678 * for (gsize i = 0; i < n_chunks; i++) 679 * { 680 * // Do something with each chunk here, and then free them, since 681 * // g_ptr_array_steal() transfers ownership of all the elements and the 682 * // array to the caller. 683 * … 684 * 685 * g_bytes_unref (chunks[i]); 686 * } 687 * 688 * g_free (chunks); 689 * 690 * // After calling g_ptr_array_steal(), the pointer array can be reused for the 691 * // next set of chunks. 692 * g_assert (chunk_buffer->len == 0); 693 * ]| 694 * 695 * Params: 696 * len = pointer to retrieve the number of 697 * elements of the original array 698 * 699 * Returns: the element data, which should be 700 * freed using g_free(). 701 * 702 * Since: 2.64 703 */ 704 public void** steal(out size_t len) 705 { 706 return g_ptr_array_steal(gPtrArray, &len); 707 } 708 709 /** 710 * Removes the pointer at the given index from the pointer array. 711 * The following elements are moved down one place. The #GDestroyNotify for 712 * @array is *not* called on the removed element; ownership is transferred to 713 * the caller of this function. 714 * 715 * Params: 716 * index = the index of the pointer to steal 717 * 718 * Returns: the pointer which was removed 719 * 720 * Since: 2.58 721 */ 722 public void* stealIndex(uint index) 723 { 724 return g_ptr_array_steal_index(gPtrArray, index); 725 } 726 727 /** 728 * Removes the pointer at the given index from the pointer array. 729 * The last element in the array is used to fill in the space, so 730 * this function does not preserve the order of the array. But it 731 * is faster than g_ptr_array_steal_index(). The #GDestroyNotify for @array is 732 * *not* called on the removed element; ownership is transferred to the caller 733 * of this function. 734 * 735 * Params: 736 * index = the index of the pointer to steal 737 * 738 * Returns: the pointer which was removed 739 * 740 * Since: 2.58 741 */ 742 public void* stealIndexFast(uint index) 743 { 744 return g_ptr_array_steal_index_fast(gPtrArray, index); 745 } 746 747 /** 748 * Atomically decrements the reference count of @array by one. If the 749 * reference count drops to 0, the effect is the same as calling 750 * g_ptr_array_free() with @free_segment set to %TRUE. This function 751 * is thread-safe and may be called from any thread. 752 * 753 * Since: 2.22 754 */ 755 public void unref() 756 { 757 g_ptr_array_unref(gPtrArray); 758 } 759 }