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