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.ByteArray;
26 
27 private import glib.Bytes;
28 private import glib.ConstructionException;
29 private import glib.c.functions;
30 public  import glib.c.types;
31 
32 
33 /**
34  * Contains the public fields of a GByteArray.
35  */
36 public class ByteArray
37 {
38 	/** the main Gtk struct */
39 	protected GByteArray* gByteArray;
40 	protected bool ownedRef;
41 
42 	/** Get the main Gtk struct */
43 	public GByteArray* getByteArrayStruct(bool transferOwnership = false)
44 	{
45 		if (transferOwnership)
46 			ownedRef = false;
47 		return gByteArray;
48 	}
49 
50 	/** the main Gtk struct as a void* */
51 	protected void* getStruct()
52 	{
53 		return cast(void*)gByteArray;
54 	}
55 
56 	/**
57 	 * Sets our main struct and passes it to the parent class.
58 	 */
59 	public this (GByteArray* gByteArray, bool ownedRef = false)
60 	{
61 		this.gByteArray = gByteArray;
62 		this.ownedRef = ownedRef;
63 	}
64 
65 
66 	/**
67 	 * Adds the given bytes to the end of the #GByteArray.
68 	 * The array will grow in size automatically if necessary.
69 	 *
70 	 * Params:
71 	 *     data = the byte data to be added
72 	 *     len = the number of bytes to add
73 	 *
74 	 * Returns: the #GByteArray
75 	 */
76 	public ByteArray append(ubyte* data, uint len)
77 	{
78 		auto __p = g_byte_array_append(gByteArray, data, len);
79 
80 		if(__p is null)
81 		{
82 			return null;
83 		}
84 
85 		return new ByteArray(cast(GByteArray*) __p);
86 	}
87 
88 	/**
89 	 * Frees the memory allocated by the #GByteArray. If @free_segment is
90 	 * %TRUE it frees the actual byte data. If the reference count of
91 	 * @array is greater than one, the #GByteArray wrapper is preserved but
92 	 * the size of @array will be set to zero.
93 	 *
94 	 * Params:
95 	 *     freeSegment = if %TRUE the actual byte data is freed as well
96 	 *
97 	 * Returns: the element data if @free_segment is %FALSE, otherwise
98 	 *     %NULL.  The element data should be freed using g_free().
99 	 */
100 	public ubyte* free(bool freeSegment)
101 	{
102 		return g_byte_array_free(gByteArray, freeSegment);
103 	}
104 
105 	/**
106 	 * Transfers the data from the #GByteArray into a new immutable #GBytes.
107 	 *
108 	 * The #GByteArray is freed unless the reference count of @array is greater
109 	 * than one, the #GByteArray wrapper is preserved but the size of @array
110 	 * will be set to zero.
111 	 *
112 	 * This is identical to using g_bytes_new_take() and g_byte_array_free()
113 	 * together.
114 	 *
115 	 * Returns: a new immutable #GBytes representing same
116 	 *     byte data that was in the array
117 	 *
118 	 * Since: 2.32
119 	 */
120 	public Bytes freeToBytes()
121 	{
122 		auto __p = g_byte_array_free_to_bytes(gByteArray);
123 
124 		if(__p is null)
125 		{
126 			return null;
127 		}
128 
129 		return new Bytes(cast(GBytes*) __p, true);
130 	}
131 
132 	/**
133 	 * Creates a new #GByteArray with a reference count of 1.
134 	 *
135 	 * Returns: the new #GByteArray
136 	 *
137 	 * Throws: ConstructionException GTK+ fails to create the object.
138 	 */
139 	public this()
140 	{
141 		auto __p = g_byte_array_new();
142 
143 		if(__p is null)
144 		{
145 			throw new ConstructionException("null returned by new");
146 		}
147 
148 		this(cast(GByteArray*) __p);
149 	}
150 
151 	/**
152 	 * Create byte array containing the data. The data will be owned by the array
153 	 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
154 	 *
155 	 * Do not use it if @len is greater than %G_MAXUINT. #GByteArray
156 	 * stores the length of its data in #guint, which may be shorter than
157 	 * #gsize.
158 	 *
159 	 * Params:
160 	 *     data = byte data for the array
161 	 *
162 	 * Returns: a new #GByteArray
163 	 *
164 	 * Since: 2.32
165 	 *
166 	 * Throws: ConstructionException GTK+ fails to create the object.
167 	 */
168 	public this(ubyte[] data)
169 	{
170 		auto __p = g_byte_array_new_take(data.ptr, cast(size_t)data.length);
171 
172 		if(__p is null)
173 		{
174 			throw new ConstructionException("null returned by new_take");
175 		}
176 
177 		this(cast(GByteArray*) __p);
178 	}
179 
180 	/**
181 	 * Adds the given data to the start of the #GByteArray.
182 	 * The array will grow in size automatically if necessary.
183 	 *
184 	 * Params:
185 	 *     data = the byte data to be added
186 	 *     len = the number of bytes to add
187 	 *
188 	 * Returns: the #GByteArray
189 	 */
190 	public ByteArray prepend(ubyte* data, uint len)
191 	{
192 		auto __p = g_byte_array_prepend(gByteArray, data, len);
193 
194 		if(__p is null)
195 		{
196 			return null;
197 		}
198 
199 		return new ByteArray(cast(GByteArray*) __p);
200 	}
201 
202 	alias doref = ref_;
203 	/**
204 	 * Atomically increments the reference count of @array by one.
205 	 * This function is thread-safe and may be called from any thread.
206 	 *
207 	 * Returns: The passed in #GByteArray
208 	 *
209 	 * Since: 2.22
210 	 */
211 	public ByteArray ref_()
212 	{
213 		auto __p = g_byte_array_ref(gByteArray);
214 
215 		if(__p is null)
216 		{
217 			return null;
218 		}
219 
220 		return new ByteArray(cast(GByteArray*) __p);
221 	}
222 
223 	/**
224 	 * Removes the byte at the given index from a #GByteArray.
225 	 * The following bytes are moved down one place.
226 	 *
227 	 * Params:
228 	 *     index = the index of the byte to remove
229 	 *
230 	 * Returns: the #GByteArray
231 	 */
232 	public ByteArray removeIndex(uint index)
233 	{
234 		auto __p = g_byte_array_remove_index(gByteArray, index);
235 
236 		if(__p is null)
237 		{
238 			return null;
239 		}
240 
241 		return new ByteArray(cast(GByteArray*) __p);
242 	}
243 
244 	/**
245 	 * Removes the byte at the given index from a #GByteArray. The last
246 	 * element in the array is used to fill in the space, so this function
247 	 * does not preserve the order of the #GByteArray. But it is faster
248 	 * than g_byte_array_remove_index().
249 	 *
250 	 * Params:
251 	 *     index = the index of the byte to remove
252 	 *
253 	 * Returns: the #GByteArray
254 	 */
255 	public ByteArray removeIndexFast(uint index)
256 	{
257 		auto __p = g_byte_array_remove_index_fast(gByteArray, index);
258 
259 		if(__p is null)
260 		{
261 			return null;
262 		}
263 
264 		return new ByteArray(cast(GByteArray*) __p);
265 	}
266 
267 	/**
268 	 * Removes the given number of bytes starting at the given index from a
269 	 * #GByteArray.  The following elements are moved to close the gap.
270 	 *
271 	 * Params:
272 	 *     index = the index of the first byte to remove
273 	 *     length = the number of bytes to remove
274 	 *
275 	 * Returns: the #GByteArray
276 	 *
277 	 * Since: 2.4
278 	 */
279 	public ByteArray removeRange(uint index, uint length)
280 	{
281 		auto __p = g_byte_array_remove_range(gByteArray, index, length);
282 
283 		if(__p is null)
284 		{
285 			return null;
286 		}
287 
288 		return new ByteArray(cast(GByteArray*) __p);
289 	}
290 
291 	/**
292 	 * Sets the size of the #GByteArray, expanding it if necessary.
293 	 *
294 	 * Params:
295 	 *     length = the new size of the #GByteArray
296 	 *
297 	 * Returns: the #GByteArray
298 	 */
299 	public ByteArray setSize(uint length)
300 	{
301 		auto __p = g_byte_array_set_size(gByteArray, length);
302 
303 		if(__p is null)
304 		{
305 			return null;
306 		}
307 
308 		return new ByteArray(cast(GByteArray*) __p);
309 	}
310 
311 	/**
312 	 * Creates a new #GByteArray with @reserved_size bytes preallocated.
313 	 * This avoids frequent reallocation, if you are going to add many
314 	 * bytes to the array. Note however that the size of the array is still
315 	 * 0.
316 	 *
317 	 * Params:
318 	 *     reservedSize = number of bytes preallocated
319 	 *
320 	 * Returns: the new #GByteArray
321 	 */
322 	public static ByteArray sizedNew(uint reservedSize)
323 	{
324 		auto __p = g_byte_array_sized_new(reservedSize);
325 
326 		if(__p is null)
327 		{
328 			return null;
329 		}
330 
331 		return new ByteArray(cast(GByteArray*) __p);
332 	}
333 
334 	/**
335 	 * Sorts a byte array, using @compare_func which should be a
336 	 * qsort()-style comparison function (returns less than zero for first
337 	 * arg is less than second arg, zero for equal, greater than zero if
338 	 * first arg is greater than second arg).
339 	 *
340 	 * If two array elements compare equal, their order in the sorted array
341 	 * is undefined. If you want equal elements to keep their order (i.e.
342 	 * you want a stable sort) you can write a comparison function that,
343 	 * if two elements would otherwise compare equal, compares them by
344 	 * their addresses.
345 	 *
346 	 * Params:
347 	 *     compareFunc = comparison function
348 	 */
349 	public void sort(GCompareFunc compareFunc)
350 	{
351 		g_byte_array_sort(gByteArray, compareFunc);
352 	}
353 
354 	/**
355 	 * Like g_byte_array_sort(), but the comparison function takes an extra
356 	 * user data argument.
357 	 *
358 	 * Params:
359 	 *     compareFunc = comparison function
360 	 *     userData = data to pass to @compare_func
361 	 */
362 	public void sortWithData(GCompareDataFunc compareFunc, void* userData)
363 	{
364 		g_byte_array_sort_with_data(gByteArray, compareFunc, userData);
365 	}
366 
367 	/**
368 	 * Frees the data in the array and resets the size to zero, while
369 	 * the underlying array is preserved for use elsewhere and returned
370 	 * to the caller.
371 	 *
372 	 * Params:
373 	 *     len = pointer to retrieve the number of
374 	 *         elements of the original array
375 	 *
376 	 * Returns: the element data, which should be
377 	 *     freed using g_free().
378 	 *
379 	 * Since: 2.64
380 	 */
381 	public ubyte* steal(out size_t len)
382 	{
383 		return g_byte_array_steal(gByteArray, &len);
384 	}
385 
386 	/**
387 	 * Atomically decrements the reference count of @array by one. If the
388 	 * reference count drops to 0, all memory allocated by the array is
389 	 * released. This function is thread-safe and may be called from any
390 	 * thread.
391 	 *
392 	 * Since: 2.22
393 	 */
394 	public void unref()
395 	{
396 		g_byte_array_unref(gByteArray);
397 	}
398 }