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