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