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