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