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