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 * Conversion parameters: 26 * inFile = 27 * outPack = glib 28 * outFile = VariantBuilder 29 * strct = GVariantBuilder 30 * realStrct= 31 * ctorStrct= 32 * clss = VariantBuilder 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_variant_builder_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.Str 47 * - glib.Variant 48 * - glib.VariantType 49 * structWrap: 50 * - GVariant* -> Variant 51 * - GVariantBuilder* -> VariantBuilder 52 * - GVariantType* -> VariantType 53 * module aliases: 54 * local aliases: 55 * overrides: 56 */ 57 58 module glib.VariantBuilder; 59 60 public import gtkc.glibtypes; 61 62 private import gtkc.glib; 63 private import glib.ConstructionException; 64 65 66 private import glib.Str; 67 private import glib.Variant; 68 private import glib.VariantType; 69 70 71 72 73 /** 74 * Description 75 * GVariant is a variant datatype; it stores a value along with 76 * information about the type of that value. The range of possible 77 * values is determined by the type. The type system used by GVariant 78 * is GVariantType. 79 * GVariant instances always have a type and a value (which are given 80 * at construction time). The type and value of a GVariant instance 81 * can never change other than by the GVariant itself being 82 * destroyed. A GVariant can not contain a pointer. 83 * GVariant is reference counted using g_variant_ref() and 84 * g_variant_unref(). GVariant also has floating reference counts -- 85 * see g_variant_ref_sink(). 86 * GVariant is completely threadsafe. A GVariant instance can be 87 * concurrently accessed in any way from any number of threads without 88 * problems. 89 * GVariant is heavily optimised for dealing with data in serialised 90 * form. It works particularly well with data located in memory-mapped 91 * files. It can perform nearly all deserialisation operations in a 92 * small constant time, usually touching only a single memory page. 93 * Serialised GVariant data can also be sent over the network. 94 * GVariant is largely compatible with D-Bus. Almost all types of 95 * GVariant instances can be sent over D-Bus. See GVariantType for 96 * exceptions. 97 * For convenience to C programmers, GVariant features powerful 98 * varargs-based value construction and destruction. This feature is 99 * designed to be embedded in other libraries. 100 * There is a Python-inspired text language for describing GVariant 101 * values. GVariant includes a printer for this language and a parser 102 * with type inferencing. 103 * Memory Use 104 * GVariant tries to be quite efficient with respect to memory use. 105 * This section gives a rough idea of how much memory is used by the 106 * current implementation. The information here is subject to change 107 * in the future. 108 * The memory allocated by GVariant can be grouped into 4 broad 109 * purposes: memory for serialised data, memory for the type 110 * information cache, buffer management memory and memory for the 111 * GVariant structure itself. 112 * Serialised Data Memory 113 * This is the memory that is used for storing GVariant data in 114 * serialised form. This is what would be sent over the network or 115 * what would end up on disk. 116 * The amount of memory required to store a boolean is 1 byte. 16, 117 * 32 and 64 bit integers and double precision floating point numbers 118 * use their "natural" size. Strings (including object path and 119 * signature strings) are stored with a nul terminator, and as such 120 * use the length of the string plus 1 byte. 121 * Maybe types use no space at all to represent the null value and 122 * use the same amount of space (sometimes plus one byte) as the 123 * equivalent non-maybe-typed value to represent the non-null case. 124 * Arrays use the amount of space required to store each of their 125 * members, concatenated. Additionally, if the items stored in an 126 * array are not of a fixed-size (ie: strings, other arrays, etc) 127 * then an additional framing offset is stored for each item. The 128 * size of this offset is either 1, 2 or 4 bytes depending on the 129 * overall size of the container. Additionally, extra padding bytes 130 * are added as required for alignment of child values. 131 * Tuples (including dictionary entries) use the amount of space 132 * required to store each of their members, concatenated, plus one 133 * framing offset (as per arrays) for each non-fixed-sized item in 134 * the tuple, except for the last one. Additionally, extra padding 135 * bytes are added as required for alignment of child values. 136 * Variants use the same amount of space as the item inside of the 137 * variant, plus 1 byte, plus the length of the type string for the 138 * item inside the variant. 139 * As an example, consider a dictionary mapping strings to variants. 140 * In the case that the dictionary is empty, 0 bytes are required for 141 * the serialisation. 142 * If we add an item "width" that maps to the int32 value of 500 then 143 * we will use 4 byte to store the int32 (so 6 for the variant 144 * containing it) and 6 bytes for the string. The variant must be 145 * aligned to 8 after the 6 bytes of the string, so that's 2 extra 146 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used 147 * for the dictionary entry. An additional 1 byte is added to the 148 * array as a framing offset making a total of 15 bytes. 149 * If we add another entry, "title" that maps to a nullable string 150 * that happens to have a value of null, then we use 0 bytes for the 151 * null value (and 3 bytes for the variant to contain it along with 152 * its type string) plus 6 bytes for the string. Again, we need 2 153 * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes. 154 * We now require extra padding between the two items in the array. 155 * After the 14 bytes of the first item, that's 2 bytes required. We 156 * now require 2 framing offsets for an extra two bytes. 14 + 2 + 11 157 * + 2 = 29 bytes to encode the entire two-item dictionary. 158 * Type Information Cache 159 * For each GVariant type that currently exists in the program a type 160 * information structure is kept in the type information cache. The 161 * type information structure is required for rapid deserialisation. 162 * Continuing with the above example, if a GVariant exists with the 163 * type "a{sv}" then a type information struct will exist for 164 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type 165 * will share the same type information. Additionally, all 166 * single-digit types are stored in read-only static memory and do 167 * not contribute to the writable memory footprint of a program using 168 * GVariant. 169 * Aside from the type information structures stored in read-only 170 * memory, there are two forms of type information. One is used for 171 * container types where there is a single element type: arrays and 172 * maybe types. The other is used for container types where there 173 * are multiple element types: tuples and dictionary entries. 174 * Array type info structures are 6 * sizeof (void *), plus the 175 * memory required to store the type string itself. This means that 176 * on 32bit systems, the cache entry for "a{sv}" would require 30 177 * bytes of memory (plus malloc overhead). 178 * Tuple type info structures are 6 * sizeof (void *), plus 4 * 179 * sizeof (void *) for each item in the tuple, plus the memory 180 * required to store the type string itself. A 2-item tuple, for 181 * example, would have a type information structure that consumed 182 * writable memory in the size of 14 * sizeof (void *) (plus type 183 * string) This means that on 32bit systems, the cache entry for 184 * "{sv}" would require 61 bytes of memory (plus malloc overhead). 185 * This means that in total, for our "a{sv}" example, 91 bytes of 186 * type information would be allocated. 187 * The type information cache, additionally, uses a GHashTable to 188 * store and lookup the cached items and stores a pointer to this 189 * hash table in static storage. The hash table is freed when there 190 * are zero items in the type cache. 191 * Although these sizes may seem large it is important to remember 192 * that a program will probably only have a very small number of 193 * different types of values in it and that only one type information 194 * structure is required for many different values of the same type. 195 * Buffer Management Memory 196 * GVariant uses an internal buffer management structure to deal 197 * with the various different possible sources of serialised data 198 * that it uses. The buffer is responsible for ensuring that the 199 * correct call is made when the data is no longer in use by 200 * GVariant. This may involve a g_free() or a g_slice_free() or 201 * even g_mapped_file_unref(). 202 * One buffer management structure is used for each chunk of 203 * serialised data. The size of the buffer management structure is 4 204 * * (void *). On 32bit systems, that's 16 bytes. 205 * GVariant structure 206 * The size of a GVariant structure is 6 * (void *). On 32 bit 207 * systems, that's 24 bytes. 208 * GVariant structures only exist if they are explicitly created 209 * with API calls. For example, if a GVariant is constructed out of 210 * serialised data for the example given above (with the dictionary) 211 * then although there are 9 individual values that comprise the 212 * entire dictionary (two keys, two values, two variants containing 213 * the values, two dictionary entries, plus the dictionary itself), 214 * only 1 GVariant instance exists -- the one refering to the 215 * dictionary. 216 * If calls are made to start accessing the other values then 217 * GVariant instances will exist for those values only for as long 218 * as they are in use (ie: until you call g_variant_unref()). The 219 * type information is shared. The serialised data and the buffer 220 * management structure for that serialised data is shared by the 221 * child. 222 * Summary 223 * To put the entire example together, for our dictionary mapping 224 * strings to variants (with two entries, as given above), we are 225 * using 91 bytes of memory for type information, 29 byes of memory 226 * for the serialised data, 16 bytes for buffer management and 24 227 * bytes for the GVariant instance, or a total of 160 bytes, plus 228 * malloc overhead. If we were to use g_variant_get_child_value() to 229 * access the two dictionary entries, we would use an additional 48 230 * bytes. If we were to have other dictionaries of the same type, we 231 * would use more memory for the serialised data and buffer 232 * management for those dictionaries, but the type information would 233 * be shared. 234 */ 235 public class VariantBuilder 236 { 237 238 /** the main Gtk struct */ 239 protected GVariantBuilder* gVariantBuilder; 240 241 242 public GVariantBuilder* getVariantBuilderStruct() 243 { 244 return gVariantBuilder; 245 } 246 247 248 /** the main Gtk struct as a void* */ 249 protected void* getStruct() 250 { 251 return cast(void*)gVariantBuilder; 252 } 253 254 /** 255 * Sets our main struct and passes it to the parent class 256 */ 257 public this (GVariantBuilder* gVariantBuilder) 258 { 259 this.gVariantBuilder = gVariantBuilder; 260 } 261 262 /** 263 */ 264 265 /** 266 * Decreases the reference count on builder. 267 * In the event that there are no more references, releases all memory 268 * associated with the GVariantBuilder. 269 * Don't call this on stack-allocated GVariantBuilder instances or bad 270 * things will happen. 271 * Since 2.24 272 */ 273 public void unref() 274 { 275 // void g_variant_builder_unref (GVariantBuilder *builder); 276 g_variant_builder_unref(gVariantBuilder); 277 } 278 279 /** 280 * Increases the reference count on builder. 281 * Don't call this on stack-allocated GVariantBuilder instances or bad 282 * things will happen. 283 * Since 2.24 284 * Returns: a new reference to builder 285 */ 286 public VariantBuilder doref() 287 { 288 // GVariantBuilder * g_variant_builder_ref (GVariantBuilder *builder); 289 auto p = g_variant_builder_ref(gVariantBuilder); 290 291 if(p is null) 292 { 293 return null; 294 } 295 296 return new VariantBuilder(cast(GVariantBuilder*) p); 297 } 298 299 /** 300 * Allocates and initialises a new GVariantBuilder. 301 * You should call g_variant_builder_unref() on the return value when it 302 * is no longer needed. The memory will not be automatically freed by 303 * any other call. 304 * In most cases it is easier to place a GVariantBuilder directly on 305 * the stack of the calling function and initialise it with 306 * g_variant_builder_init(). 307 * Since 2.24 308 * Params: 309 * type = a container type 310 * Throws: ConstructionException GTK+ fails to create the object. 311 */ 312 public this (VariantType type) 313 { 314 // GVariantBuilder * g_variant_builder_new (const GVariantType *type); 315 auto p = g_variant_builder_new((type is null) ? null : type.getVariantTypeStruct()); 316 if(p is null) 317 { 318 throw new ConstructionException("null returned by g_variant_builder_new((type is null) ? null : type.getVariantTypeStruct())"); 319 } 320 this(cast(GVariantBuilder*) p); 321 } 322 323 /** 324 * Initialises a GVariantBuilder structure. 325 * type must be non-NULL. It specifies the type of container to 326 * construct. It can be an indefinite type such as 327 * G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)". 328 * Maybe, array, tuple, dictionary entry and variant-typed values may be 329 * constructed. 330 * After the builder is initialised, values are added using 331 * g_variant_builder_add_value() or g_variant_builder_add(). 332 * After all the child values are added, g_variant_builder_end() frees 333 * the memory associated with the builder and returns the GVariant that 334 * was created. 335 * This function completely ignores the previous contents of builder. 336 * On one hand this means that it is valid to pass in completely 337 * uninitialised memory. On the other hand, this means that if you are 338 * initialising over top of an existing GVariantBuilder you need to 339 * first call g_variant_builder_clear() in order to avoid leaking 340 * memory. 341 * You must not call g_variant_builder_ref() or 342 * g_variant_builder_unref() on a GVariantBuilder that was initialised 343 * with this function. If you ever pass a reference to a 344 * GVariantBuilder outside of the control of your own code then you 345 * should assume that the person receiving that reference may try to use 346 * reference counting; you should use g_variant_builder_new() instead of 347 * this function. 348 * Since 2.24 349 * Params: 350 * type = a container type 351 */ 352 public void init(VariantType type) 353 { 354 // void g_variant_builder_init (GVariantBuilder *builder, const GVariantType *type); 355 g_variant_builder_init(gVariantBuilder, (type is null) ? null : type.getVariantTypeStruct()); 356 } 357 358 /** 359 * Releases all memory associated with a GVariantBuilder without 360 * freeing the GVariantBuilder structure itself. 361 * It typically only makes sense to do this on a stack-allocated 362 * GVariantBuilder if you want to abort building the value part-way 363 * through. This function need not be called if you call 364 * g_variant_builder_end() and it also doesn't need to be called on 365 * builders allocated with g_variant_builder_new (see 366 * g_variant_builder_unref() for that). 367 * This function leaves the GVariantBuilder structure set to all-zeros. 368 * It is valid to call this function on either an initialised 369 * GVariantBuilder or one that is set to all-zeros but it is not valid 370 * to call this function on uninitialised memory. 371 * Since 2.24 372 */ 373 public void clear() 374 { 375 // void g_variant_builder_clear (GVariantBuilder *builder); 376 g_variant_builder_clear(gVariantBuilder); 377 } 378 379 /** 380 * Adds value to builder. 381 * It is an error to call this function in any way that would create an 382 * inconsistent value to be constructed. Some examples of this are 383 * putting different types of items into an array, putting the wrong 384 * types or number of items in a tuple, putting more than one value into 385 * a variant, etc. 386 * If value is a floating reference (see g_variant_ref_sink()), 387 * the builder instance takes ownership of value. 388 * Since 2.24 389 * Params: 390 * value = a GVariant 391 */ 392 public void addValue(Variant value) 393 { 394 // void g_variant_builder_add_value (GVariantBuilder *builder, GVariant *value); 395 g_variant_builder_add_value(gVariantBuilder, (value is null) ? null : value.getVariantStruct()); 396 } 397 398 /** 399 * Ends the builder process and returns the constructed value. 400 * It is not permissible to use builder in any way after this call 401 * except for reference counting operations (in the case of a 402 * heap-allocated GVariantBuilder) or by reinitialising it with 403 * g_variant_builder_init() (in the case of stack-allocated). 404 * It is an error to call this function in any way that would create an 405 * inconsistent value to be constructed (ie: insufficient number of 406 * items added to a container with a specific number of children 407 * required). It is also an error to call this function if the builder 408 * was created with an indefinite array or maybe type and no children 409 * have been added; in this case it is impossible to infer the type of 410 * the empty array. 411 * Since 2.24 412 * Returns: a new, floating, GVariant. [transfer none] 413 */ 414 public Variant end() 415 { 416 // GVariant * g_variant_builder_end (GVariantBuilder *builder); 417 auto p = g_variant_builder_end(gVariantBuilder); 418 419 if(p is null) 420 { 421 return null; 422 } 423 424 return new Variant(cast(GVariant*) p); 425 } 426 427 /** 428 * Opens a subcontainer inside the given builder. When done adding 429 * items to the subcontainer, g_variant_builder_close() must be called. 430 * It is an error to call this function in any way that would cause an 431 * inconsistent value to be constructed (ie: adding too many values or 432 * a value of an incorrect type). 433 * Since 2.24 434 * Params: 435 * type = a GVariantType 436 */ 437 public void open(VariantType type) 438 { 439 // void g_variant_builder_open (GVariantBuilder *builder, const GVariantType *type); 440 g_variant_builder_open(gVariantBuilder, (type is null) ? null : type.getVariantTypeStruct()); 441 } 442 443 /** 444 * Closes the subcontainer inside the given builder that was opened by 445 * the most recent call to g_variant_builder_open(). 446 * It is an error to call this function in any way that would create an 447 * inconsistent value to be constructed (ie: too few values added to the 448 * subcontainer). 449 * Since 2.24 450 */ 451 public void close() 452 { 453 // void g_variant_builder_close (GVariantBuilder *builder); 454 g_variant_builder_close(gVariantBuilder); 455 } 456 }