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 	/**
67 	 * Adds a pointer to the end of the pointer array. The array will grow
68 	 * in size automatically if necessary.
69 	 *
70 	 * Params:
71 	 *     data = the pointer to add
72 	 */
73 	public void add(void* data)
74 	{
75 		g_ptr_array_add(gPtrArray, data);
76 	}
77 
78 	/**
79 	 * Checks whether @needle exists in @haystack. If the element is found, %TRUE is
80 	 * returned and the element’s index is returned in @index_ (if non-%NULL).
81 	 * Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists
82 	 * multiple times in @haystack, the index of the first instance is returned.
83 	 *
84 	 * This does pointer comparisons only. If you want to use more complex equality
85 	 * checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
86 	 *
87 	 * Params:
88 	 *     needle = pointer to look for
89 	 *     index = return location for the index of
90 	 *         the element, if found
91 	 *
92 	 * Returns: %TRUE if @needle is one of the elements of @haystack
93 	 *
94 	 * Since: 2.54
95 	 */
96 	public bool find(void* needle, out uint index)
97 	{
98 		return g_ptr_array_find(gPtrArray, needle, &index) != 0;
99 	}
100 
101 	/**
102 	 * Checks whether @needle exists in @haystack, using the given @equal_func.
103 	 * If the element is found, %TRUE is returned and the element’s index is
104 	 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
105 	 * is undefined. If @needle exists multiple times in @haystack, the index of
106 	 * the first instance is returned.
107 	 *
108 	 * @equal_func is called with the element from the array as its first parameter,
109 	 * and @needle as its second parameter. If @equal_func is %NULL, pointer
110 	 * equality is used.
111 	 *
112 	 * Params:
113 	 *     needle = pointer to look for
114 	 *     equalFunc = the function to call for each element, which should
115 	 *         return %TRUE when the desired element is found; or %NULL to use pointer
116 	 *         equality
117 	 *     index = return location for the index of
118 	 *         the element, if found
119 	 *
120 	 * Returns: %TRUE if @needle is one of the elements of @haystack
121 	 *
122 	 * Since: 2.54
123 	 */
124 	public bool findWithEqualFunc(void* needle, GEqualFunc equalFunc, out uint index)
125 	{
126 		return g_ptr_array_find_with_equal_func(gPtrArray, needle, equalFunc, &index) != 0;
127 	}
128 
129 	/**
130 	 * Calls a function for each element of a #GPtrArray.
131 	 *
132 	 * Params:
133 	 *     func = the function to call for each array element
134 	 *     userData = user data to pass to the function
135 	 *
136 	 * Since: 2.4
137 	 */
138 	public void foreac(GFunc func, void* userData)
139 	{
140 		g_ptr_array_foreach(gPtrArray, func, userData);
141 	}
142 
143 	/**
144 	 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
145 	 * it frees the memory block holding the elements as well. Pass %FALSE
146 	 * if you want to free the #GPtrArray wrapper but preserve the
147 	 * underlying array for use elsewhere. If the reference count of @array
148 	 * is greater than one, the #GPtrArray wrapper is preserved but the
149 	 * size of @array will be set to zero.
150 	 *
151 	 * If array contents point to dynamically-allocated memory, they should
152 	 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
153 	 * function has been set for @array.
154 	 *
155 	 * This function is not thread-safe. If using a #GPtrArray from multiple
156 	 * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
157 	 * functions.
158 	 *
159 	 * Params:
160 	 *     freeSeg = if %TRUE the actual pointer array is freed as well
161 	 *
162 	 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
163 	 *     The pointer array should be freed using g_free().
164 	 */
165 	public void** free(bool freeSeg)
166 	{
167 		return g_ptr_array_free(gPtrArray, freeSeg);
168 	}
169 
170 	/**
171 	 * Inserts an element into the pointer array at the given index. The
172 	 * array will grow in size automatically if necessary.
173 	 *
174 	 * Params:
175 	 *     index = the index to place the new element at, or -1 to append
176 	 *     data = the pointer to add.
177 	 *
178 	 * Since: 2.40
179 	 */
180 	public void insert(int index, void* data)
181 	{
182 		g_ptr_array_insert(gPtrArray, index, data);
183 	}
184 
185 	/**
186 	 * Creates a new #GPtrArray with a reference count of 1.
187 	 *
188 	 * Returns: the new #GPtrArray
189 	 *
190 	 * Throws: ConstructionException GTK+ fails to create the object.
191 	 */
192 	public this()
193 	{
194 		auto p = g_ptr_array_new();
195 
196 		if(p is null)
197 		{
198 			throw new ConstructionException("null returned by new");
199 		}
200 
201 		this(cast(GPtrArray*) p);
202 	}
203 
204 	/**
205 	 * Creates a new #GPtrArray with @reserved_size pointers preallocated
206 	 * and a reference count of 1. This avoids frequent reallocation, if
207 	 * you are going to add many pointers to the array. Note however that
208 	 * the size of the array is still 0. It also set @element_free_func
209 	 * for freeing each element when the array is destroyed either via
210 	 * g_ptr_array_unref(), when g_ptr_array_free() is called with
211 	 * @free_segment set to %TRUE or when removing elements.
212 	 *
213 	 * Params:
214 	 *     reservedSize = number of pointers preallocated
215 	 *     elementFreeFunc = A function to free elements with
216 	 *         destroy @array or %NULL
217 	 *
218 	 * Returns: A new #GPtrArray
219 	 *
220 	 * Since: 2.30
221 	 *
222 	 * Throws: ConstructionException GTK+ fails to create the object.
223 	 */
224 	public this(uint reservedSize, GDestroyNotify elementFreeFunc)
225 	{
226 		auto p = g_ptr_array_new_full(reservedSize, elementFreeFunc);
227 
228 		if(p is null)
229 		{
230 			throw new ConstructionException("null returned by new_full");
231 		}
232 
233 		this(cast(GPtrArray*) p);
234 	}
235 
236 	/**
237 	 * Creates a new #GPtrArray with a reference count of 1 and use
238 	 * @element_free_func for freeing each element when the array is destroyed
239 	 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
240 	 * @free_segment set to %TRUE or when removing elements.
241 	 *
242 	 * Params:
243 	 *     elementFreeFunc = A function to free elements with
244 	 *         destroy @array or %NULL
245 	 *
246 	 * Returns: A new #GPtrArray
247 	 *
248 	 * Since: 2.22
249 	 *
250 	 * Throws: ConstructionException GTK+ fails to create the object.
251 	 */
252 	public this(GDestroyNotify elementFreeFunc)
253 	{
254 		auto p = g_ptr_array_new_with_free_func(elementFreeFunc);
255 
256 		if(p is null)
257 		{
258 			throw new ConstructionException("null returned by new_with_free_func");
259 		}
260 
261 		this(cast(GPtrArray*) p);
262 	}
263 
264 	/**
265 	 * Atomically increments the reference count of @array by one.
266 	 * This function is thread-safe and may be called from any thread.
267 	 *
268 	 * Returns: The passed in #GPtrArray
269 	 *
270 	 * Since: 2.22
271 	 */
272 	public PtrArray doref()
273 	{
274 		auto p = g_ptr_array_ref(gPtrArray);
275 
276 		if(p is null)
277 		{
278 			return null;
279 		}
280 
281 		return new PtrArray(cast(GPtrArray*) p);
282 	}
283 
284 	/**
285 	 * Removes the first occurrence of the given pointer from the pointer
286 	 * array. The following elements are moved down one place. If @array
287 	 * has a non-%NULL #GDestroyNotify function it is called for the
288 	 * removed element.
289 	 *
290 	 * It returns %TRUE if the pointer was removed, or %FALSE if the
291 	 * pointer was not found.
292 	 *
293 	 * Params:
294 	 *     data = the pointer to remove
295 	 *
296 	 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
297 	 *     is not found in the array
298 	 */
299 	public bool remove(void* data)
300 	{
301 		return g_ptr_array_remove(gPtrArray, data) != 0;
302 	}
303 
304 	/**
305 	 * Removes the first occurrence of the given pointer from the pointer
306 	 * array. The last element in the array is used to fill in the space,
307 	 * so this function does not preserve the order of the array. But it
308 	 * is faster than g_ptr_array_remove(). If @array has a non-%NULL
309 	 * #GDestroyNotify function it is called for the removed element.
310 	 *
311 	 * It returns %TRUE if the pointer was removed, or %FALSE if the
312 	 * pointer was not found.
313 	 *
314 	 * Params:
315 	 *     data = the pointer to remove
316 	 *
317 	 * Returns: %TRUE if the pointer was found in the array
318 	 */
319 	public bool removeFast(void* data)
320 	{
321 		return g_ptr_array_remove_fast(gPtrArray, data) != 0;
322 	}
323 
324 	/**
325 	 * Removes the pointer at the given index from the pointer array.
326 	 * The following elements are moved down one place. If @array has
327 	 * a non-%NULL #GDestroyNotify function it is called for the removed
328 	 * element.
329 	 *
330 	 * Params:
331 	 *     index = the index of the pointer to remove
332 	 *
333 	 * Returns: the pointer which was removed
334 	 */
335 	public void* removeIndex(uint index)
336 	{
337 		return g_ptr_array_remove_index(gPtrArray, index);
338 	}
339 
340 	/**
341 	 * Removes the pointer at the given index from the pointer array.
342 	 * The last element in the array is used to fill in the space, so
343 	 * this function does not preserve the order of the array. But it
344 	 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
345 	 * #GDestroyNotify function it is called for the removed element.
346 	 *
347 	 * Params:
348 	 *     index = the index of the pointer to remove
349 	 *
350 	 * Returns: the pointer which was removed
351 	 */
352 	public void* removeIndexFast(uint index)
353 	{
354 		return g_ptr_array_remove_index_fast(gPtrArray, index);
355 	}
356 
357 	/**
358 	 * Removes the given number of pointers starting at the given index
359 	 * from a #GPtrArray. The following elements are moved to close the
360 	 * gap. If @array has a non-%NULL #GDestroyNotify function it is
361 	 * called for the removed elements.
362 	 *
363 	 * Params:
364 	 *     index = the index of the first pointer to remove
365 	 *     length = the number of pointers to remove
366 	 *
367 	 * Returns: the @array
368 	 *
369 	 * Since: 2.4
370 	 */
371 	public PtrArray removeRange(uint index, uint length)
372 	{
373 		auto p = g_ptr_array_remove_range(gPtrArray, index, length);
374 
375 		if(p is null)
376 		{
377 			return null;
378 		}
379 
380 		return new PtrArray(cast(GPtrArray*) p);
381 	}
382 
383 	/**
384 	 * Sets a function for freeing each element when @array is destroyed
385 	 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
386 	 * with @free_segment set to %TRUE or when removing elements.
387 	 *
388 	 * Params:
389 	 *     elementFreeFunc = A function to free elements with
390 	 *         destroy @array or %NULL
391 	 *
392 	 * Since: 2.22
393 	 */
394 	public void setFreeFunc(GDestroyNotify elementFreeFunc)
395 	{
396 		g_ptr_array_set_free_func(gPtrArray, elementFreeFunc);
397 	}
398 
399 	/**
400 	 * Sets the size of the array. When making the array larger,
401 	 * newly-added elements will be set to %NULL. When making it smaller,
402 	 * if @array has a non-%NULL #GDestroyNotify function then it will be
403 	 * called for the removed elements.
404 	 *
405 	 * Params:
406 	 *     length = the new length of the pointer array
407 	 */
408 	public void setSize(int length)
409 	{
410 		g_ptr_array_set_size(gPtrArray, length);
411 	}
412 
413 	/**
414 	 * Creates a new #GPtrArray with @reserved_size pointers preallocated
415 	 * and a reference count of 1. This avoids frequent reallocation, if
416 	 * you are going to add many pointers to the array. Note however that
417 	 * the size of the array is still 0.
418 	 *
419 	 * Params:
420 	 *     reservedSize = number of pointers preallocated
421 	 *
422 	 * Returns: the new #GPtrArray
423 	 */
424 	public static PtrArray sizedNew(uint reservedSize)
425 	{
426 		auto p = g_ptr_array_sized_new(reservedSize);
427 
428 		if(p is null)
429 		{
430 			return null;
431 		}
432 
433 		return new PtrArray(cast(GPtrArray*) p);
434 	}
435 
436 	/**
437 	 * Sorts the array, using @compare_func which should be a qsort()-style
438 	 * comparison function (returns less than zero for first arg is less
439 	 * than second arg, zero for equal, greater than zero if irst arg is
440 	 * greater than second arg).
441 	 *
442 	 * Note that the comparison function for g_ptr_array_sort() doesn't
443 	 * take the pointers from the array as arguments, it takes pointers to
444 	 * the pointers in the array.
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_ptr_array_sort(gPtrArray, compareFunc);
454 	}
455 
456 	/**
457 	 * Like g_ptr_array_sort(), but the comparison function has an extra
458 	 * user data argument.
459 	 *
460 	 * Note that the comparison function for g_ptr_array_sort_with_data()
461 	 * doesn't take the pointers from the array as arguments, it takes
462 	 * pointers to the pointers in the array.
463 	 *
464 	 * This is guaranteed to be a stable sort since version 2.32.
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_ptr_array_sort_with_data(gPtrArray, compareFunc, userData);
473 	}
474 
475 	/**
476 	 * Atomically decrements the reference count of @array by one. If the
477 	 * reference count drops to 0, the effect is the same as calling
478 	 * g_ptr_array_free() with @free_segment set to %TRUE. This function
479 	 * is thread-safe and may be called from any thread.
480 	 *
481 	 * Since: 2.22
482 	 */
483 	public void unref()
484 	{
485 		g_ptr_array_unref(gPtrArray);
486 	}
487 }