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