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 gtkc.glib;
30 public  import gtkc.glibtypes;
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 	 * Params:
156 	 *     data = byte data for the array
157 	 *     len = length of @data
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 }