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