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