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 alias doref = ref_; 200 /** 201 * Atomically increments the reference count of @array by one. 202 * This function is thread-safe and may be called from any thread. 203 * 204 * Returns: The passed in #GByteArray 205 * 206 * Since: 2.22 207 */ 208 public ByteArray ref_() 209 { 210 auto p = g_byte_array_ref(gByteArray); 211 212 if(p is null) 213 { 214 return null; 215 } 216 217 return new ByteArray(cast(GByteArray*) p); 218 } 219 220 /** 221 * Removes the byte at the given index from a #GByteArray. 222 * The following bytes are moved down one place. 223 * 224 * Params: 225 * index = the index of the byte to remove 226 * 227 * Returns: the #GByteArray 228 */ 229 public ByteArray removeIndex(uint index) 230 { 231 auto p = g_byte_array_remove_index(gByteArray, index); 232 233 if(p is null) 234 { 235 return null; 236 } 237 238 return new ByteArray(cast(GByteArray*) p); 239 } 240 241 /** 242 * Removes the byte at the given index from a #GByteArray. The last 243 * element in the array is used to fill in the space, so this function 244 * does not preserve the order of the #GByteArray. But it is faster 245 * than g_byte_array_remove_index(). 246 * 247 * Params: 248 * index = the index of the byte to remove 249 * 250 * Returns: the #GByteArray 251 */ 252 public ByteArray removeIndexFast(uint index) 253 { 254 auto p = g_byte_array_remove_index_fast(gByteArray, index); 255 256 if(p is null) 257 { 258 return null; 259 } 260 261 return new ByteArray(cast(GByteArray*) p); 262 } 263 264 /** 265 * Removes the given number of bytes starting at the given index from a 266 * #GByteArray. The following elements are moved to close the gap. 267 * 268 * Params: 269 * index = the index of the first byte to remove 270 * length = the number of bytes to remove 271 * 272 * Returns: the #GByteArray 273 * 274 * Since: 2.4 275 */ 276 public ByteArray removeRange(uint index, uint length) 277 { 278 auto p = g_byte_array_remove_range(gByteArray, index, length); 279 280 if(p is null) 281 { 282 return null; 283 } 284 285 return new ByteArray(cast(GByteArray*) p); 286 } 287 288 /** 289 * Sets the size of the #GByteArray, expanding it if necessary. 290 * 291 * Params: 292 * length = the new size of the #GByteArray 293 * 294 * Returns: the #GByteArray 295 */ 296 public ByteArray setSize(uint length) 297 { 298 auto p = g_byte_array_set_size(gByteArray, length); 299 300 if(p is null) 301 { 302 return null; 303 } 304 305 return new ByteArray(cast(GByteArray*) p); 306 } 307 308 /** 309 * Creates a new #GByteArray with @reserved_size bytes preallocated. 310 * This avoids frequent reallocation, if you are going to add many 311 * bytes to the array. Note however that the size of the array is still 312 * 0. 313 * 314 * Params: 315 * reservedSize = number of bytes preallocated 316 * 317 * Returns: the new #GByteArray 318 */ 319 public static ByteArray sizedNew(uint reservedSize) 320 { 321 auto p = g_byte_array_sized_new(reservedSize); 322 323 if(p is null) 324 { 325 return null; 326 } 327 328 return new ByteArray(cast(GByteArray*) p); 329 } 330 331 /** 332 * Sorts a byte array, using @compare_func which should be a 333 * qsort()-style comparison function (returns less than zero for first 334 * arg is less than second arg, zero for equal, greater than zero if 335 * first arg is greater than second arg). 336 * 337 * If two array elements compare equal, their order in the sorted array 338 * is undefined. If you want equal elements to keep their order (i.e. 339 * you want a stable sort) you can write a comparison function that, 340 * if two elements would otherwise compare equal, compares them by 341 * their addresses. 342 * 343 * Params: 344 * compareFunc = comparison function 345 */ 346 public void sort(GCompareFunc compareFunc) 347 { 348 g_byte_array_sort(gByteArray, compareFunc); 349 } 350 351 /** 352 * Like g_byte_array_sort(), but the comparison function takes an extra 353 * user data argument. 354 * 355 * Params: 356 * compareFunc = comparison function 357 * userData = data to pass to @compare_func 358 */ 359 public void sortWithData(GCompareDataFunc compareFunc, void* userData) 360 { 361 g_byte_array_sort_with_data(gByteArray, compareFunc, userData); 362 } 363 364 /** 365 * Atomically decrements the reference count of @array by one. If the 366 * reference count drops to 0, all memory allocated by the array is 367 * released. This function is thread-safe and may be called from any 368 * thread. 369 * 370 * Since: 2.22 371 */ 372 public void unref() 373 { 374 g_byte_array_unref(gByteArray); 375 } 376 }