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