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