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  * Conversion parameters:
26  * inFile  = glib-Pointer-Arrays.html
27  * outPack = glib
28  * outFile = PtrArray
29  * strct   = GPtrArray
30  * realStrct=
31  * ctorStrct=
32  * clss    = PtrArray
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_ptr_array_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * structWrap:
47  * 	- GPtrArray* -> PtrArray
48  * module aliases:
49  * local aliases:
50  * overrides:
51  */
52 
53 module glib.PtrArray;
54 
55 public  import gtkc.glibtypes;
56 
57 private import gtkc.glib;
58 private import glib.ConstructionException;
59 
60 
61 
62 
63 /**
64  * Pointer Arrays are similar to Arrays but are used only for storing
65  * pointers.
66  *
67  * Note
68  *
69  * If you remove elements from the array, elements at the
70  * end of the array are moved into the space previously occupied by the
71  * removed element. This means that you should not rely on the index of
72  * particular elements remaining the same. You should also be careful
73  * when deleting elements while iterating over the array.
74  *
75  * To create a pointer array, use g_ptr_array_new().
76  *
77  * To add elements to a pointer array, use g_ptr_array_add().
78  *
79  * To remove elements from a pointer array, use g_ptr_array_remove(),
80  * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
81  *
82  * To access an element of a pointer array, use g_ptr_array_index().
83  *
84  * To set the size of a pointer array, use g_ptr_array_set_size().
85  *
86  * To free a pointer array, use g_ptr_array_free().
87  *
88  * $(DDOC_COMMENT example)
89  */
90 public class PtrArray
91 {
92 	
93 	/** the main Gtk struct */
94 	protected GPtrArray* gPtrArray;
95 	
96 	
97 	/** Get the main Gtk struct */
98 	public GPtrArray* getPtrArrayStruct()
99 	{
100 		return gPtrArray;
101 	}
102 	
103 	
104 	/** the main Gtk struct as a void* */
105 	protected void* getStruct()
106 	{
107 		return cast(void*)gPtrArray;
108 	}
109 	
110 	/**
111 	 * Sets our main struct and passes it to the parent class
112 	 */
113 	public this (GPtrArray* gPtrArray)
114 	{
115 		this.gPtrArray = gPtrArray;
116 	}
117 	
118 	/**
119 	 */
120 	
121 	/**
122 	 * Creates a new GPtrArray with a reference count of 1.
123 	 * Throws: ConstructionException GTK+ fails to create the object.
124 	 */
125 	public this ()
126 	{
127 		// GPtrArray * g_ptr_array_new (void);
128 		auto p = g_ptr_array_new();
129 		if(p is null)
130 		{
131 			throw new ConstructionException("null returned by g_ptr_array_new()");
132 		}
133 		this(cast(GPtrArray*) p);
134 	}
135 	
136 	/**
137 	 * Creates a new GPtrArray with reserved_size pointers preallocated
138 	 * and a reference count of 1. This avoids frequent reallocation, if
139 	 * you are going to add many pointers to the array. Note however that
140 	 * the size of the array is still 0.
141 	 * Params:
142 	 * reservedSize = number of pointers preallocated.
143 	 * Returns: the new GPtrArray.
144 	 */
145 	public static PtrArray sizedNew(uint reservedSize)
146 	{
147 		// GPtrArray * g_ptr_array_sized_new (guint reserved_size);
148 		auto p = g_ptr_array_sized_new(reservedSize);
149 		
150 		if(p is null)
151 		{
152 			return null;
153 		}
154 		
155 		return new PtrArray(cast(GPtrArray*) p);
156 	}
157 	
158 	/**
159 	 * Creates a new GPtrArray with a reference count of 1 and use element_free_func
160 	 * for freeing each element when the array is destroyed either via
161 	 * g_ptr_array_unref(), when g_ptr_array_free() is called with free_segment
162 	 * set to TRUE or when removing elements.
163 	 * Since 2.22
164 	 * Params:
165 	 * elementFreeFunc = A function to free elements with destroy array or NULL. [allow-none]
166 	 * Throws: ConstructionException GTK+ fails to create the object.
167 	 */
168 	public this (GDestroyNotify elementFreeFunc)
169 	{
170 		// GPtrArray * g_ptr_array_new_with_free_func (GDestroyNotify element_free_func);
171 		auto p = g_ptr_array_new_with_free_func(elementFreeFunc);
172 		if(p is null)
173 		{
174 			throw new ConstructionException("null returned by g_ptr_array_new_with_free_func(elementFreeFunc)");
175 		}
176 		this(cast(GPtrArray*) p);
177 	}
178 	
179 	/**
180 	 * Creates a new GPtrArray with reserved_size pointers preallocated
181 	 * and a reference count of 1. This avoids frequent reallocation, if
182 	 * you are going to add many pointers to the array. Note however that
183 	 * the size of the array is still 0. It also set element_free_func
184 	 * for freeing each element when the array is destroyed either via
185 	 * g_ptr_array_unref(), when g_ptr_array_free() is called with free_segment
186 	 * set to TRUE or when removing elements.
187 	 * Since 2.30
188 	 * Params:
189 	 * reservedSize = number of pointers preallocated.
190 	 * elementFreeFunc = A function to free elements with destroy array or NULL. [allow-none]
191 	 * Throws: ConstructionException GTK+ fails to create the object.
192 	 */
193 	public this (uint reservedSize, GDestroyNotify elementFreeFunc)
194 	{
195 		// GPtrArray * g_ptr_array_new_full (guint reserved_size,  GDestroyNotify element_free_func);
196 		auto p = g_ptr_array_new_full(reservedSize, elementFreeFunc);
197 		if(p is null)
198 		{
199 			throw new ConstructionException("null returned by g_ptr_array_new_full(reservedSize, elementFreeFunc)");
200 		}
201 		this(cast(GPtrArray*) p);
202 	}
203 	
204 	/**
205 	 * Sets a function for freeing each element when array is destroyed
206 	 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
207 	 * with free_segment set to TRUE or when removing elements.
208 	 * Since 2.22
209 	 * Params:
210 	 * elementFreeFunc = A function to free elements with destroy array or NULL. [allow-none]
211 	 */
212 	public void setFreeFunc(GDestroyNotify elementFreeFunc)
213 	{
214 		// void g_ptr_array_set_free_func (GPtrArray *array,  GDestroyNotify element_free_func);
215 		g_ptr_array_set_free_func(gPtrArray, elementFreeFunc);
216 	}
217 	
218 	/**
219 	 * Atomically increments the reference count of array by one.
220 	 * This function is thread-safe and may be called from any thread.
221 	 * Since 2.22
222 	 * Returns: The passed in GPtrArray
223 	 */
224 	public PtrArray doref()
225 	{
226 		// GPtrArray * g_ptr_array_ref (GPtrArray *array);
227 		auto p = g_ptr_array_ref(gPtrArray);
228 		
229 		if(p is null)
230 		{
231 			return null;
232 		}
233 		
234 		return new PtrArray(cast(GPtrArray*) p);
235 	}
236 	
237 	/**
238 	 * Atomically decrements the reference count of array by one. If the
239 	 * reference count drops to 0, the effect is the same as calling
240 	 * g_ptr_array_free() with free_segment set to TRUE. This function
241 	 * is MT-safe and may be called from any thread.
242 	 * Since 2.22
243 	 */
244 	public void unref()
245 	{
246 		// void g_ptr_array_unref (GPtrArray *array);
247 		g_ptr_array_unref(gPtrArray);
248 	}
249 	
250 	/**
251 	 * Adds a pointer to the end of the pointer array. The array will grow
252 	 * in size automatically if necessary.
253 	 * Params:
254 	 * data = the pointer to add.
255 	 */
256 	public void add(void* data)
257 	{
258 		// void g_ptr_array_add (GPtrArray *array,  gpointer data);
259 		g_ptr_array_add(gPtrArray, data);
260 	}
261 	
262 	/**
263 	 * Removes the first occurrence of the given pointer from the pointer
264 	 * array. The following elements are moved down one place. If array
265 	 * has a non-NULL GDestroyNotify function it is called for the
266 	 * removed element.
267 	 * It returns TRUE if the pointer was removed, or FALSE if the
268 	 * pointer was not found.
269 	 * Params:
270 	 * data = the pointer to remove.
271 	 * Returns: TRUE if the pointer is removed. FALSE if the pointer is not found in the array.
272 	 */
273 	public int remove(void* data)
274 	{
275 		// gboolean g_ptr_array_remove (GPtrArray *array,  gpointer data);
276 		return g_ptr_array_remove(gPtrArray, data);
277 	}
278 	
279 	/**
280 	 * Removes the pointer at the given index from the pointer array. The
281 	 * following elements are moved down one place. If array has a
282 	 * non-NULL GDestroyNotify function it is called for the removed
283 	 * element.
284 	 * Params:
285 	 * index = the index of the pointer to remove.
286 	 * Returns: the pointer which was removed.
287 	 */
288 	public void* removeIndex(uint index)
289 	{
290 		// gpointer g_ptr_array_remove_index (GPtrArray *array,  guint index_);
291 		return g_ptr_array_remove_index(gPtrArray, index);
292 	}
293 	
294 	/**
295 	 * Removes the first occurrence of the given pointer from the pointer
296 	 * array. The last element in the array is used to fill in the space,
297 	 * so this function does not preserve the order of the array. But it is
298 	 * faster than g_ptr_array_remove(). If array has a non-NULL
299 	 * GDestroyNotify function it is called for the removed element.
300 	 * It returns TRUE if the pointer was removed, or FALSE if the
301 	 * pointer was not found.
302 	 * Params:
303 	 * data = the pointer to remove.
304 	 * Returns: TRUE if the pointer was found in the array.
305 	 */
306 	public int removeFast(void* data)
307 	{
308 		// gboolean g_ptr_array_remove_fast (GPtrArray *array,  gpointer data);
309 		return g_ptr_array_remove_fast(gPtrArray, data);
310 	}
311 	
312 	/**
313 	 * Removes the pointer at the given index from the pointer array. The
314 	 * last element in the array is used to fill in the space, so this
315 	 * function does not preserve the order of the array. But it is faster
316 	 * than g_ptr_array_remove_index(). If array has a non-NULL
317 	 * GDestroyNotify function it is called for the removed element.
318 	 * Params:
319 	 * index = the index of the pointer to remove.
320 	 * Returns: the pointer which was removed.
321 	 */
322 	public void* removeIndexFast(uint index)
323 	{
324 		// gpointer g_ptr_array_remove_index_fast (GPtrArray *array,  guint index_);
325 		return g_ptr_array_remove_index_fast(gPtrArray, index);
326 	}
327 	
328 	/**
329 	 * Removes the given number of pointers starting at the given index
330 	 * from a GPtrArray. The following elements are moved to close the
331 	 * gap. If array has a non-NULL GDestroyNotify function it is called
332 	 * for the removed elements.
333 	 * Since 2.4
334 	 * Params:
335 	 * index = the index of the first pointer to remove.
336 	 * length = the number of pointers to remove.
337 	 */
338 	public void removeRange(uint index, uint length)
339 	{
340 		// void g_ptr_array_remove_range (GPtrArray *array,  guint index_,  guint length);
341 		g_ptr_array_remove_range(gPtrArray, index, length);
342 	}
343 	
344 	/**
345 	 * Sorts the array, using compare_func which should be a qsort()-style
346 	 * comparison function (returns less than zero for first arg is less
347 	 * than second arg, zero for equal, greater than zero if irst arg is
348 	 * greater than second arg).
349 	 * Note
350 	 * The comparison function for g_ptr_array_sort() doesn't
351 	 * take the pointers from the array as arguments, it takes pointers to
352 	 * the pointers in the array.
353 	 * This is guaranteed to be a stable sort since version 2.32.
354 	 * Params:
355 	 * compareFunc = comparison function.
356 	 */
357 	public void sort(GCompareFunc compareFunc)
358 	{
359 		// void g_ptr_array_sort (GPtrArray *array,  GCompareFunc compare_func);
360 		g_ptr_array_sort(gPtrArray, compareFunc);
361 	}
362 	
363 	/**
364 	 * Like g_ptr_array_sort(), but the comparison function has an extra
365 	 * user data argument.
366 	 * Note
367 	 * The comparison function for g_ptr_array_sort_with_data()
368 	 * doesn't take the pointers from the array as arguments, it takes
369 	 * pointers to the pointers in the array.
370 	 * This is guaranteed to be a stable sort since version 2.32.
371 	 * Params:
372 	 * compareFunc = comparison function.
373 	 * userData = data to pass to compare_func.
374 	 */
375 	public void sortWithData(GCompareDataFunc compareFunc, void* userData)
376 	{
377 		// void g_ptr_array_sort_with_data (GPtrArray *array,  GCompareDataFunc compare_func,  gpointer user_data);
378 		g_ptr_array_sort_with_data(gPtrArray, compareFunc, userData);
379 	}
380 	
381 	/**
382 	 * Sets the size of the array. When making the array larger,
383 	 * newly-added elements will be set to NULL. When making it smaller,
384 	 * if array has a non-NULL GDestroyNotify function then it will be
385 	 * called for the removed elements.
386 	 * Params:
387 	 * length = the new length of the pointer array.
388 	 */
389 	public void setSize(int length)
390 	{
391 		// void g_ptr_array_set_size (GPtrArray *array,  gint length);
392 		g_ptr_array_set_size(gPtrArray, length);
393 	}
394 	
395 	/**
396 	 * Frees the memory allocated for the GPtrArray. If free_seg is TRUE
397 	 * it frees the memory block holding the elements as well. Pass FALSE
398 	 * if you want to free the GPtrArray wrapper but preserve the
399 	 * underlying array for use elsewhere. If the reference count of array
400 	 * is greater than one, the GPtrArray wrapper is preserved but the
401 	 * size of array will be set to zero.
402 	 * Note
403 	 * If array contents point to dynamically-allocated
404 	 * memory, they should be freed separately if free_seg is TRUE and no
405 	 * GDestroyNotify function has been set for array.
406 	 * Params:
407 	 * freeSeg = if TRUE the actual pointer array is freed as well.
408 	 * Returns: the pointer array if free_seg is FALSE, otherwise NULL. The pointer array should be freed using g_free().
409 	 */
410 	public void** free(int freeSeg)
411 	{
412 		// gpointer * g_ptr_array_free (GPtrArray *array,  gboolean free_seg);
413 		return g_ptr_array_free(gPtrArray, freeSeg);
414 	}
415 	
416 	/**
417 	 * Calls a function for each element of a GPtrArray.
418 	 * Since 2.4
419 	 * Params:
420 	 * func = the function to call for each array element
421 	 * userData = user data to pass to the function
422 	 */
423 	public void foreac(GFunc func, void* userData)
424 	{
425 		// void g_ptr_array_foreach (GPtrArray *array,  GFunc func,  gpointer user_data);
426 		g_ptr_array_foreach(gPtrArray, func, userData);
427 	}
428 }