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 gtkc.glib; 30 public import gtkc.glibtypes; 31 32 33 /** 34 * Contains the public fields of a GArray. 35 */ 36 public class ArrayG 37 { 38 /** the main Gtk struct */ 39 protected GArray* gArray; 40 41 /** Get the main Gtk struct */ 42 public GArray* getArrayGStruct() 43 { 44 return gArray; 45 } 46 47 /** the main Gtk struct as a void* */ 48 protected void* getStruct() 49 { 50 return cast(void*)gArray; 51 } 52 53 /** 54 * Sets our main struct and passes it to the parent class. 55 */ 56 public this (GArray* gArray) 57 { 58 this.gArray = gArray; 59 } 60 61 /** 62 */ 63 64 /** 65 * Adds @len elements onto the end of the array. 66 * 67 * Params: 68 * data = a pointer to the elements to append to the end of the array 69 * len = the number of elements to append 70 * 71 * Return: the #GArray 72 */ 73 public ArrayG appendVals(void* data, uint len) 74 { 75 auto p = g_array_append_vals(gArray, data, len); 76 77 if(p is null) 78 { 79 return null; 80 } 81 82 return new ArrayG(cast(GArray*) p); 83 } 84 85 /** 86 * Frees the memory allocated for the #GArray. If @free_segment is 87 * %TRUE it frees the memory block holding the elements as well and 88 * also each element if @array has a @element_free_func set. Pass 89 * %FALSE if you want to free the #GArray wrapper but preserve the 90 * underlying array for use elsewhere. If the reference count of @array 91 * is greater than one, the #GArray wrapper is preserved but the size 92 * of @array will be set to zero. 93 * 94 * If array elements contain dynamically-allocated memory, they should 95 * be freed separately. 96 * 97 * Params: 98 * freeSegment = if %TRUE the actual element data is freed as well 99 * 100 * Return: the element data if @free_segment is %FALSE, otherwise 101 * %NULL. The element data should be freed using g_free(). 102 */ 103 public string free(bool freeSegment) 104 { 105 return Str.toString(g_array_free(gArray, freeSegment)); 106 } 107 108 /** 109 * Gets the size of the elements in @array. 110 * 111 * Return: Size of each element, in bytes 112 * 113 * Since: 2.22 114 */ 115 public uint getElementSize() 116 { 117 return g_array_get_element_size(gArray); 118 } 119 120 /** 121 * Inserts @len elements into a #GArray at the given index. 122 * 123 * Params: 124 * index = the index to place the elements at 125 * data = a pointer to the elements to insert 126 * len = the number of elements to insert 127 * 128 * Return: the #GArray 129 */ 130 public ArrayG insertVals(uint index, void* data, uint len) 131 { 132 auto p = g_array_insert_vals(gArray, index, data, len); 133 134 if(p is null) 135 { 136 return null; 137 } 138 139 return new ArrayG(cast(GArray*) p); 140 } 141 142 /** 143 * Creates a new #GArray with a reference count of 1. 144 * 145 * Params: 146 * zeroTerminated = %TRUE if the array should have an extra element at 147 * the end which is set to 0 148 * clear = %TRUE if #GArray elements should be automatically cleared 149 * to 0 when they are allocated 150 * elementSize = the size of each element in bytes 151 * 152 * Return: the new #GArray 153 * 154 * Throws: ConstructionException GTK+ fails to create the object. 155 */ 156 public this(bool zeroTerminated, bool clear, uint elementSize) 157 { 158 auto p = g_array_new(zeroTerminated, clear, elementSize); 159 160 if(p is null) 161 { 162 throw new ConstructionException("null returned by new"); 163 } 164 165 this(cast(GArray*) p); 166 } 167 168 /** 169 * Adds @len elements onto the start of the array. 170 * 171 * This operation is slower than g_array_append_vals() since the 172 * existing elements in the array have to be moved to make space for 173 * the new elements. 174 * 175 * Params: 176 * data = a pointer to the elements to prepend to the start of the array 177 * len = the number of elements to prepend 178 * 179 * Return: the #GArray 180 */ 181 public ArrayG prependVals(void* data, uint len) 182 { 183 auto p = g_array_prepend_vals(gArray, data, len); 184 185 if(p is null) 186 { 187 return null; 188 } 189 190 return new ArrayG(cast(GArray*) p); 191 } 192 193 /** 194 * Atomically increments the reference count of @array by one. 195 * This function is MT-safe and may be called from any thread. 196 * 197 * Return: The passed in #GArray 198 * 199 * Since: 2.22 200 */ 201 public ArrayG doref() 202 { 203 auto p = g_array_ref(gArray); 204 205 if(p is null) 206 { 207 return null; 208 } 209 210 return new ArrayG(cast(GArray*) p); 211 } 212 213 /** 214 * Removes the element at the given index from a #GArray. The following 215 * elements are moved down one place. 216 * 217 * Params: 218 * index = the index of the element to remove 219 * 220 * Return: the #GArray 221 */ 222 public ArrayG removeIndex(uint index) 223 { 224 auto p = g_array_remove_index(gArray, index); 225 226 if(p is null) 227 { 228 return null; 229 } 230 231 return new ArrayG(cast(GArray*) p); 232 } 233 234 /** 235 * Removes the element at the given index from a #GArray. The last 236 * element in the array is used to fill in the space, so this function 237 * does not preserve the order of the #GArray. But it is faster than 238 * g_array_remove_index(). 239 * 240 * Params: 241 * index = the index of the element to remove 242 * 243 * Return: the #GArray 244 */ 245 public ArrayG removeIndexFast(uint index) 246 { 247 auto p = g_array_remove_index_fast(gArray, index); 248 249 if(p is null) 250 { 251 return null; 252 } 253 254 return new ArrayG(cast(GArray*) p); 255 } 256 257 /** 258 * Removes the given number of elements starting at the given index 259 * from a #GArray. The following elements are moved to close the gap. 260 * 261 * Params: 262 * index = the index of the first element to remove 263 * length = the number of elements to remove 264 * 265 * Return: the #GArray 266 * 267 * Since: 2.4 268 */ 269 public ArrayG removeRange(uint index, uint length) 270 { 271 auto p = g_array_remove_range(gArray, index, length); 272 273 if(p is null) 274 { 275 return null; 276 } 277 278 return new ArrayG(cast(GArray*) p); 279 } 280 281 /** 282 * Sets a function to clear an element of @array. 283 * 284 * The @clear_func will be called when an element in the array 285 * data segment is removed and when the array is freed and data 286 * segment is deallocated as well. 287 * 288 * Note that in contrast with other uses of #GDestroyNotify 289 * functions, @clear_func is expected to clear the contents of 290 * the array element it is given, but not free the element itself. 291 * 292 * Params: 293 * clearFunc = a function to clear an element of @array 294 * 295 * Since: 2.32 296 */ 297 public void setClearFunc(GDestroyNotify clearFunc) 298 { 299 g_array_set_clear_func(gArray, clearFunc); 300 } 301 302 /** 303 * Sets the size of the array, expanding it if necessary. If the array 304 * was created with @clear_ set to %TRUE, the new elements are set to 0. 305 * 306 * Params: 307 * length = the new size of the #GArray 308 * 309 * Return: the #GArray 310 */ 311 public ArrayG setSize(uint length) 312 { 313 auto p = g_array_set_size(gArray, length); 314 315 if(p is null) 316 { 317 return null; 318 } 319 320 return new ArrayG(cast(GArray*) p); 321 } 322 323 /** 324 * Creates a new #GArray with @reserved_size elements preallocated and 325 * a reference count of 1. This avoids frequent reallocation, if you 326 * are going to add many elements to the array. Note however that the 327 * size of the array is still 0. 328 * 329 * Params: 330 * zeroTerminated = %TRUE if the array should have an extra element at 331 * the end with all bits cleared 332 * clear = %TRUE if all bits in the array should be cleared to 0 on 333 * allocation 334 * elementSize = size of each element in the array 335 * reservedSize = number of elements preallocated 336 * 337 * Return: the new #GArray 338 */ 339 public static ArrayG sizedNew(bool zeroTerminated, bool clear, uint elementSize, uint reservedSize) 340 { 341 auto p = g_array_sized_new(zeroTerminated, clear, elementSize, reservedSize); 342 343 if(p is null) 344 { 345 return null; 346 } 347 348 return new ArrayG(cast(GArray*) p); 349 } 350 351 /** 352 * Sorts a #GArray using @compare_func which should be a qsort()-style 353 * comparison function (returns less than zero for first arg is less 354 * than second arg, zero for equal, greater zero if first arg is 355 * greater than second arg). 356 * 357 * This is guaranteed to be a stable sort since version 2.32. 358 * 359 * Params: 360 * compareFunc = comparison function 361 */ 362 public void sort(GCompareFunc compareFunc) 363 { 364 g_array_sort(gArray, compareFunc); 365 } 366 367 /** 368 * Like g_array_sort(), but the comparison function receives an extra 369 * user data argument. 370 * 371 * This is guaranteed to be a stable sort since version 2.32. 372 * 373 * There used to be a comment here about making the sort stable by 374 * using the addresses of the elements in the comparison function. 375 * This did not actually work, so any such code should be removed. 376 * 377 * Params: 378 * compareFunc = comparison function 379 * userData = data to pass to @compare_func 380 */ 381 public void sortWithData(GCompareDataFunc compareFunc, void* userData) 382 { 383 g_array_sort_with_data(gArray, compareFunc, userData); 384 } 385 386 /** 387 * Atomically decrements the reference count of @array by one. If the 388 * reference count drops to 0, all memory allocated by the array is 389 * released. This function is MT-safe and may be called from any 390 * thread. 391 * 392 * Since: 2.22 393 */ 394 public void unref() 395 { 396 g_array_unref(gArray); 397 } 398 }