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.ArrayG; 26 27 private import glib.ConstructionException; 28 private import glib.Str; 29 private import glib.c.functions; 30 public import glib.c.types; 31 public import gtkc.glibtypes; 32 33 34 /** 35 * Contains the public fields of a GArray. 36 */ 37 public class ArrayG 38 { 39 /** the main Gtk struct */ 40 protected GArray* gArray; 41 protected bool ownedRef; 42 43 /** Get the main Gtk struct */ 44 public GArray* getArrayGStruct(bool transferOwnership = false) 45 { 46 if (transferOwnership) 47 ownedRef = false; 48 return gArray; 49 } 50 51 /** the main Gtk struct as a void* */ 52 protected void* getStruct() 53 { 54 return cast(void*)gArray; 55 } 56 57 /** 58 * Sets our main struct and passes it to the parent class. 59 */ 60 public this (GArray* gArray, bool ownedRef = false) 61 { 62 this.gArray = gArray; 63 this.ownedRef = ownedRef; 64 } 65 66 67 /** 68 * Adds @len elements onto the end of the array. 69 * 70 * Params: 71 * data = a pointer to the elements to append to the end of the array 72 * len = the number of elements to append 73 * 74 * Returns: the #GArray 75 */ 76 public ArrayG appendVals(void* data, uint len) 77 { 78 auto __p = g_array_append_vals(gArray, data, len); 79 80 if(__p is null) 81 { 82 return null; 83 } 84 85 return new ArrayG(cast(GArray*) __p); 86 } 87 88 /** 89 * Checks whether @target exists in @array by performing a binary 90 * search based on the given comparison function @compare_func which 91 * get pointers to items as arguments. If the element is found, %TRUE 92 * is returned and the element’s index is returned in @out_match_index 93 * (if non-%NULL). Otherwise, %FALSE is returned and @out_match_index 94 * is undefined. If @target exists multiple times in @array, the index 95 * of the first instance is returned. This search is using a binary 96 * search, so the @array must absolutely be sorted to return a correct 97 * result (if not, the function may produce false-negative). 98 * 99 * This example defines a comparison function and search an element in a #GArray: 100 * |[<!-- language="C" --> 101 * static gint* 102 * cmpint (gconstpointer a, gconstpointer b) 103 * { 104 * const gint *_a = a; 105 * const gint *_b = b; 106 * 107 * return *_a - *_b; 108 * } 109 * ... 110 * gint i = 424242; 111 * guint matched_index; 112 * gboolean result = g_array_binary_search (garray, &i, cmpint, &matched_index); 113 * ... 114 * ]| 115 * 116 * Params: 117 * target = a pointer to the item to look up. 118 * compareFunc = A #GCompareFunc used to locate @target. 119 * outMatchIndex = return location 120 * for the index of the element, if found. 121 * 122 * Returns: %TRUE if @target is one of the elements of @array, %FALSE otherwise. 123 * 124 * Since: 2.62 125 */ 126 public bool binarySearch(void* target, GCompareFunc compareFunc, out uint outMatchIndex) 127 { 128 return g_array_binary_search(gArray, target, compareFunc, &outMatchIndex) != 0; 129 } 130 131 /** 132 * Create a shallow copy of a #GArray. If the array elements consist of 133 * pointers to data, the pointers are copied but the actual data is not. 134 * 135 * Returns: A copy of @array. 136 * 137 * Since: 2.62 138 */ 139 public ArrayG copy() 140 { 141 auto __p = g_array_copy(gArray); 142 143 if(__p is null) 144 { 145 return null; 146 } 147 148 return new ArrayG(cast(GArray*) __p); 149 } 150 151 /** 152 * Frees the memory allocated for the #GArray. If @free_segment is 153 * %TRUE it frees the memory block holding the elements as well. Pass 154 * %FALSE if you want to free the #GArray wrapper but preserve the 155 * underlying array for use elsewhere. If the reference count of 156 * @array is greater than one, the #GArray wrapper is preserved but 157 * the size of @array will be set to zero. 158 * 159 * If array contents point to dynamically-allocated memory, they should 160 * be freed separately if @free_seg is %TRUE and no @clear_func 161 * function has been set for @array. 162 * 163 * This function is not thread-safe. If using a #GArray from multiple 164 * threads, use only the atomic g_array_ref() and g_array_unref() 165 * functions. 166 * 167 * Params: 168 * freeSegment = if %TRUE the actual element data is freed as well 169 * 170 * Returns: the element data if @free_segment is %FALSE, otherwise 171 * %NULL. The element data should be freed using g_free(). 172 */ 173 public string free(bool freeSegment) 174 { 175 auto retStr = g_array_free(gArray, freeSegment); 176 177 scope(exit) Str.freeString(retStr); 178 return Str.toString(retStr); 179 } 180 181 /** 182 * Gets the size of the elements in @array. 183 * 184 * Returns: Size of each element, in bytes 185 * 186 * Since: 2.22 187 */ 188 public uint getElementSize() 189 { 190 return g_array_get_element_size(gArray); 191 } 192 193 /** 194 * Inserts @len elements into a #GArray at the given index. 195 * 196 * If @index_ is greater than the array’s current length, the array is expanded. 197 * The elements between the old end of the array and the newly inserted elements 198 * will be initialised to zero if the array was configured to clear elements; 199 * otherwise their values will be undefined. 200 * 201 * @data may be %NULL if (and only if) @len is zero. If @len is zero, this 202 * function is a no-op. 203 * 204 * Params: 205 * index = the index to place the elements at 206 * data = a pointer to the elements to insert 207 * len = the number of elements to insert 208 * 209 * Returns: the #GArray 210 */ 211 public ArrayG insertVals(uint index, void* data, uint len) 212 { 213 auto __p = g_array_insert_vals(gArray, index, data, len); 214 215 if(__p is null) 216 { 217 return null; 218 } 219 220 return new ArrayG(cast(GArray*) __p); 221 } 222 223 /** 224 * Creates a new #GArray with a reference count of 1. 225 * 226 * Params: 227 * zeroTerminated = %TRUE if the array should have an extra element at 228 * the end which is set to 0 229 * clear = %TRUE if #GArray elements should be automatically cleared 230 * to 0 when they are allocated 231 * elementSize = the size of each element in bytes 232 * 233 * Returns: the new #GArray 234 * 235 * Throws: ConstructionException GTK+ fails to create the object. 236 */ 237 public this(bool zeroTerminated, bool clear, uint elementSize) 238 { 239 auto __p = g_array_new(zeroTerminated, clear, elementSize); 240 241 if(__p is null) 242 { 243 throw new ConstructionException("null returned by new"); 244 } 245 246 this(cast(GArray*) __p); 247 } 248 249 /** 250 * Adds @len elements onto the start of the array. 251 * 252 * @data may be %NULL if (and only if) @len is zero. If @len is zero, this 253 * function is a no-op. 254 * 255 * This operation is slower than g_array_append_vals() since the 256 * existing elements in the array have to be moved to make space for 257 * the new elements. 258 * 259 * Params: 260 * data = a pointer to the elements to prepend to the start of the array 261 * len = the number of elements to prepend, which may be zero 262 * 263 * Returns: the #GArray 264 */ 265 public ArrayG prependVals(void* data, uint len) 266 { 267 auto __p = g_array_prepend_vals(gArray, data, len); 268 269 if(__p is null) 270 { 271 return null; 272 } 273 274 return new ArrayG(cast(GArray*) __p); 275 } 276 277 alias doref = ref_; 278 /** 279 * Atomically increments the reference count of @array by one. 280 * This function is thread-safe and may be called from any thread. 281 * 282 * Returns: The passed in #GArray 283 * 284 * Since: 2.22 285 */ 286 public ArrayG ref_() 287 { 288 auto __p = g_array_ref(gArray); 289 290 if(__p is null) 291 { 292 return null; 293 } 294 295 return new ArrayG(cast(GArray*) __p); 296 } 297 298 /** 299 * Removes the element at the given index from a #GArray. The following 300 * elements are moved down one place. 301 * 302 * Params: 303 * index = the index of the element to remove 304 * 305 * Returns: the #GArray 306 */ 307 public ArrayG removeIndex(uint index) 308 { 309 auto __p = g_array_remove_index(gArray, index); 310 311 if(__p is null) 312 { 313 return null; 314 } 315 316 return new ArrayG(cast(GArray*) __p); 317 } 318 319 /** 320 * Removes the element at the given index from a #GArray. The last 321 * element in the array is used to fill in the space, so this function 322 * does not preserve the order of the #GArray. But it is faster than 323 * g_array_remove_index(). 324 * 325 * Params: 326 * index = the index of the element to remove 327 * 328 * Returns: the #GArray 329 */ 330 public ArrayG removeIndexFast(uint index) 331 { 332 auto __p = g_array_remove_index_fast(gArray, index); 333 334 if(__p is null) 335 { 336 return null; 337 } 338 339 return new ArrayG(cast(GArray*) __p); 340 } 341 342 /** 343 * Removes the given number of elements starting at the given index 344 * from a #GArray. The following elements are moved to close the gap. 345 * 346 * Params: 347 * index = the index of the first element to remove 348 * length = the number of elements to remove 349 * 350 * Returns: the #GArray 351 * 352 * Since: 2.4 353 */ 354 public ArrayG removeRange(uint index, uint length) 355 { 356 auto __p = g_array_remove_range(gArray, index, length); 357 358 if(__p is null) 359 { 360 return null; 361 } 362 363 return new ArrayG(cast(GArray*) __p); 364 } 365 366 /** 367 * Sets a function to clear an element of @array. 368 * 369 * The @clear_func will be called when an element in the array 370 * data segment is removed and when the array is freed and data 371 * segment is deallocated as well. @clear_func will be passed a 372 * pointer to the element to clear, rather than the element itself. 373 * 374 * Note that in contrast with other uses of #GDestroyNotify 375 * functions, @clear_func is expected to clear the contents of 376 * the array element it is given, but not free the element itself. 377 * 378 * Params: 379 * clearFunc = a function to clear an element of @array 380 * 381 * Since: 2.32 382 */ 383 public void setClearFunc(GDestroyNotify clearFunc) 384 { 385 g_array_set_clear_func(gArray, clearFunc); 386 } 387 388 /** 389 * Sets the size of the array, expanding it if necessary. If the array 390 * was created with @clear_ set to %TRUE, the new elements are set to 0. 391 * 392 * Params: 393 * length = the new size of the #GArray 394 * 395 * Returns: the #GArray 396 */ 397 public ArrayG setSize(uint length) 398 { 399 auto __p = g_array_set_size(gArray, length); 400 401 if(__p is null) 402 { 403 return null; 404 } 405 406 return new ArrayG(cast(GArray*) __p); 407 } 408 409 /** 410 * Creates a new #GArray with @reserved_size elements preallocated and 411 * a reference count of 1. This avoids frequent reallocation, if you 412 * are going to add many elements to the array. Note however that the 413 * size of the array is still 0. 414 * 415 * Params: 416 * zeroTerminated = %TRUE if the array should have an extra element at 417 * the end with all bits cleared 418 * clear = %TRUE if all bits in the array should be cleared to 0 on 419 * allocation 420 * elementSize = size of each element in the array 421 * reservedSize = number of elements preallocated 422 * 423 * Returns: the new #GArray 424 */ 425 public static ArrayG sizedNew(bool zeroTerminated, bool clear, uint elementSize, uint reservedSize) 426 { 427 auto __p = g_array_sized_new(zeroTerminated, clear, elementSize, reservedSize); 428 429 if(__p is null) 430 { 431 return null; 432 } 433 434 return new ArrayG(cast(GArray*) __p); 435 } 436 437 /** 438 * Sorts a #GArray using @compare_func which should be a qsort()-style 439 * comparison function (returns less than zero for first arg is less 440 * than second arg, zero for equal, greater zero if first arg is 441 * greater than second arg). 442 * 443 * This is guaranteed to be a stable sort since version 2.32. 444 * 445 * Params: 446 * compareFunc = comparison function 447 */ 448 public void sort(GCompareFunc compareFunc) 449 { 450 g_array_sort(gArray, compareFunc); 451 } 452 453 /** 454 * Like g_array_sort(), but the comparison function receives an extra 455 * user data argument. 456 * 457 * This is guaranteed to be a stable sort since version 2.32. 458 * 459 * There used to be a comment here about making the sort stable by 460 * using the addresses of the elements in the comparison function. 461 * This did not actually work, so any such code should be removed. 462 * 463 * Params: 464 * compareFunc = comparison function 465 * userData = data to pass to @compare_func 466 */ 467 public void sortWithData(GCompareDataFunc compareFunc, void* userData) 468 { 469 g_array_sort_with_data(gArray, compareFunc, userData); 470 } 471 472 /** 473 * Frees the data in the array and resets the size to zero, while 474 * the underlying array is preserved for use elsewhere and returned 475 * to the caller. 476 * 477 * If the array was created with the @zero_terminate property 478 * set to %TRUE, the returned data is zero terminated too. 479 * 480 * If array elements contain dynamically-allocated memory, 481 * the array elements should also be freed by the caller. 482 * 483 * A short example of use: 484 * |[<!-- language="C" --> 485 * ... 486 * gpointer data; 487 * gsize data_len; 488 * data = g_array_steal (some_array, &data_len); 489 * ... 490 * ]| 491 * 492 * Params: 493 * len = pointer to retrieve the number of 494 * elements of the original array 495 * 496 * Returns: the element data, which should be 497 * freed using g_free(). 498 * 499 * Since: 2.64 500 */ 501 public void* steal(out size_t len) 502 { 503 return g_array_steal(gArray, &len); 504 } 505 506 /** 507 * Atomically decrements the reference count of @array by one. If the 508 * reference count drops to 0, all memory allocated by the array is 509 * released. This function is thread-safe and may be called from any 510 * thread. 511 * 512 * Since: 2.22 513 */ 514 public void unref() 515 { 516 g_array_unref(gArray); 517 } 518 }