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 = VariantIter
29  * strct   = GVariantIter
30  * realStrct=
31  * ctorStrct=
32  * clss    = VariantIter
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_variant_iter_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- glib.Variant
48  * 	- gtkc.paths
49  * 	- gtkc.Loader
50  * structWrap:
51  * 	- GVariant* -> Variant
52  * 	- GVariantIter* -> VariantIter
53  * module aliases:
54  * local aliases:
55  * overrides:
56  */
57 
58 module glib.VariantIter;
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 gtkc.paths;
69 private import gtkc.Loader;
70 
71 
72 
73 
74 /**
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  *
80  * GVariant instances always have a type and a value (which are given
81  * at construction time). The type and value of a GVariant instance
82  * can never change other than by the GVariant itself being
83  * destroyed. A GVariant cannot contain a pointer.
84  *
85  * GVariant is reference counted using g_variant_ref() and
86  * g_variant_unref(). GVariant also has floating reference counts --
87  * see g_variant_ref_sink().
88  *
89  * GVariant is completely threadsafe. A GVariant instance can be
90  * concurrently accessed in any way from any number of threads without
91  * problems.
92  *
93  * GVariant is heavily optimised for dealing with data in serialised
94  * form. It works particularly well with data located in memory-mapped
95  * files. It can perform nearly all deserialisation operations in a
96  * small constant time, usually touching only a single memory page.
97  * Serialised GVariant data can also be sent over the network.
98  *
99  * GVariant is largely compatible with D-Bus. Almost all types of
100  * GVariant instances can be sent over D-Bus. See GVariantType for
101  * exceptions. (However, GVariant's serialisation format is not the same
102  * as the serialisation format of a D-Bus message body: use GDBusMessage,
103  * in the gio library, for those.)
104  *
105  * For space-efficiency, the GVariant serialisation format does not
106  * automatically include the variant's type or endianness, which must
107  * either be implied from context (such as knowledge that a particular
108  * file format always contains a little-endian G_VARIANT_TYPE_VARIANT)
109  * or supplied out-of-band (for instance, a type and/or endianness
110  * indicator could be placed at the beginning of a file, network message
111  * or network stream).
112  *
113  * A GVariant's size is limited mainly by any lower level operating
114  * system constraints, such as the number of bits in gsize. For
115  * example, it is reasonable to have a 2GB file mapped into memory
116  * with GMappedFile, and call g_variant_new_from_data() on it.
117  *
118  * For convenience to C programmers, GVariant features powerful
119  * varargs-based value construction and destruction. This feature is
120  * designed to be embedded in other libraries.
121  *
122  * There is a Python-inspired text language for describing GVariant
123  * values. GVariant includes a printer for this language and a parser
124  * with type inferencing.
125  *
126  * Memory Use
127  *
128  *  GVariant tries to be quite efficient with respect to memory use.
129  *  This section gives a rough idea of how much memory is used by the
130  *  current implementation. The information here is subject to change
131  *  in the future.
132  *
133  *  The memory allocated by GVariant can be grouped into 4 broad
134  *  purposes: memory for serialised data, memory for the type
135  *  information cache, buffer management memory and memory for the
136  *  GVariant structure itself.
137  *
138  * Serialised Data Memory
139  *
140  *  This is the memory that is used for storing GVariant data in
141  *  serialised form. This is what would be sent over the network or
142  *  what would end up on disk.
143  *
144  *  The amount of memory required to store a boolean is 1 byte. 16,
145  *  32 and 64 bit integers and double precision floating point numbers
146  *  use their "natural" size. Strings (including object path and
147  *  signature strings) are stored with a nul terminator, and as such
148  *  use the length of the string plus 1 byte.
149  *
150  *  Maybe types use no space at all to represent the null value and
151  *  use the same amount of space (sometimes plus one byte) as the
152  *  equivalent non-maybe-typed value to represent the non-null case.
153  *
154  *  Arrays use the amount of space required to store each of their
155  *  members, concatenated. Additionally, if the items stored in an
156  *  array are not of a fixed-size (ie: strings, other arrays, etc)
157  *  then an additional framing offset is stored for each item. The
158  *  size of this offset is either 1, 2 or 4 bytes depending on the
159  *  overall size of the container. Additionally, extra padding bytes
160  *  are added as required for alignment of child values.
161  *
162  *  Tuples (including dictionary entries) use the amount of space
163  *  required to store each of their members, concatenated, plus one
164  *  framing offset (as per arrays) for each non-fixed-sized item in
165  *  the tuple, except for the last one. Additionally, extra padding
166  *  bytes are added as required for alignment of child values.
167  *
168  *  Variants use the same amount of space as the item inside of the
169  *  variant, plus 1 byte, plus the length of the type string for the
170  *  item inside the variant.
171  *
172  *  As an example, consider a dictionary mapping strings to variants.
173  *  In the case that the dictionary is empty, 0 bytes are required for
174  *  the serialisation.
175  *
176  *  If we add an item "width" that maps to the int32 value of 500 then
177  *  we will use 4 byte to store the int32 (so 6 for the variant
178  *  containing it) and 6 bytes for the string. The variant must be
179  *  aligned to 8 after the 6 bytes of the string, so that's 2 extra
180  *  bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
181  *  for the dictionary entry. An additional 1 byte is added to the
182  *  array as a framing offset making a total of 15 bytes.
183  *
184  *  If we add another entry, "title" that maps to a nullable string
185  *  that happens to have a value of null, then we use 0 bytes for the
186  *  null value (and 3 bytes for the variant to contain it along with
187  *  its type string) plus 6 bytes for the string. Again, we need 2
188  *  padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
189  *
190  *  We now require extra padding between the two items in the array.
191  *  After the 14 bytes of the first item, that's 2 bytes required. We
192  *  now require 2 framing offsets for an extra two bytes. 14 + 2 + 11
193  *  + 2 = 29 bytes to encode the entire two-item dictionary.
194  *
195  * Type Information Cache
196  *
197  *  For each GVariant type that currently exists in the program a type
198  *  information structure is kept in the type information cache. The
199  *  type information structure is required for rapid deserialisation.
200  *
201  *  Continuing with the above example, if a GVariant exists with the
202  *  type "a{sv}" then a type information struct will exist for
203  *  "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
204  *  will share the same type information. Additionally, all
205  *  single-digit types are stored in read-only static memory and do
206  *  not contribute to the writable memory footprint of a program using
207  *  GVariant.
208  *
209  *  Aside from the type information structures stored in read-only
210  *  memory, there are two forms of type information. One is used for
211  *  container types where there is a single element type: arrays and
212  *  maybe types. The other is used for container types where there
213  *  are multiple element types: tuples and dictionary entries.
214  *
215  *  Array type info structures are 6 * sizeof (void *), plus the
216  *  memory required to store the type string itself. This means that
217  *  on 32bit systems, the cache entry for "a{sv}" would require 30
218  *  bytes of memory (plus malloc overhead).
219  *
220  *  Tuple type info structures are 6 * sizeof (void *), plus 4 *
221  *  sizeof (void *) for each item in the tuple, plus the memory
222  *  required to store the type string itself. A 2-item tuple, for
223  *  example, would have a type information structure that consumed
224  *  writable memory in the size of 14 * sizeof (void *) (plus type
225  *  string) This means that on 32bit systems, the cache entry for
226  *  "{sv}" would require 61 bytes of memory (plus malloc overhead).
227  *
228  *  This means that in total, for our "a{sv}" example, 91 bytes of
229  *  type information would be allocated.
230  *
231  *  The type information cache, additionally, uses a GHashTable to
232  *  store and lookup the cached items and stores a pointer to this
233  *  hash table in static storage. The hash table is freed when there
234  *  are zero items in the type cache.
235  *
236  *  Although these sizes may seem large it is important to remember
237  *  that a program will probably only have a very small number of
238  *  different types of values in it and that only one type information
239  *  structure is required for many different values of the same type.
240  *
241  * Buffer Management Memory
242  *
243  *  GVariant uses an internal buffer management structure to deal
244  *  with the various different possible sources of serialised data
245  *  that it uses. The buffer is responsible for ensuring that the
246  *  correct call is made when the data is no longer in use by
247  *  GVariant. This may involve a g_free() or a g_slice_free() or
248  *  even g_mapped_file_unref().
249  *
250  *  One buffer management structure is used for each chunk of
251  *  serialised data. The size of the buffer management structure is 4
252  *  * (void *). On 32bit systems, that's 16 bytes.
253  *
254  * GVariant structure
255  *
256  *  The size of a GVariant structure is 6 * (void *). On 32 bit
257  *  systems, that's 24 bytes.
258  *
259  *  GVariant structures only exist if they are explicitly created
260  *  with API calls. For example, if a GVariant is constructed out of
261  *  serialised data for the example given above (with the dictionary)
262  *  then although there are 9 individual values that comprise the
263  *  entire dictionary (two keys, two values, two variants containing
264  *  the values, two dictionary entries, plus the dictionary itself),
265  *  only 1 GVariant instance exists -- the one referring to the
266  *  dictionary.
267  *
268  *  If calls are made to start accessing the other values then
269  *  GVariant instances will exist for those values only for as long
270  *  as they are in use (ie: until you call g_variant_unref()). The
271  *  type information is shared. The serialised data and the buffer
272  *  management structure for that serialised data is shared by the
273  *  child.
274  *
275  * Summary
276  *
277  *  To put the entire example together, for our dictionary mapping
278  *  strings to variants (with two entries, as given above), we are
279  *  using 91 bytes of memory for type information, 29 byes of memory
280  *  for the serialised data, 16 bytes for buffer management and 24
281  *  bytes for the GVariant instance, or a total of 160 bytes, plus
282  *  malloc overhead. If we were to use g_variant_get_child_value() to
283  *  access the two dictionary entries, we would use an additional 48
284  *  bytes. If we were to have other dictionaries of the same type, we
285  *  would use more memory for the serialised data and buffer
286  *  management for those dictionaries, but the type information would
287  *  be shared.
288  */
289 public class VariantIter
290 {
291 	
292 	/** the main Gtk struct */
293 	protected GVariantIter* gVariantIter;
294 	
295 	
296 	public GVariantIter* getVariantIterStruct()
297 	{
298 		return gVariantIter;
299 	}
300 	
301 	
302 	/** the main Gtk struct as a void* */
303 	protected void* getStruct()
304 	{
305 		return cast(void*)gVariantIter;
306 	}
307 	
308 	/**
309 	 * Sets our main struct and passes it to the parent class
310 	 */
311 	public this (GVariantIter* gVariantIter)
312 	{
313 		this.gVariantIter = gVariantIter;
314 	}
315 	
316 	~this ()
317 	{
318 		if (  Linker.isLoaded(LIBRARY.GLIB) && gVariantIter !is null )
319 		{
320 			g_variant_iter_free(gVariantIter);
321 		}
322 	}
323 	
324 	/**
325 	 */
326 	
327 	/**
328 	 * Creates a new heap-allocated GVariantIter to iterate over the
329 	 * container that was being iterated over by iter. Iteration begins on
330 	 * the new iterator from the current position of the old iterator but
331 	 * the two copies are independent past that point.
332 	 * Use g_variant_iter_free() to free the return value when you no longer
333 	 * need it.
334 	 * A reference is taken to the container that iter is iterating over
335 	 * and will be releated only when g_variant_iter_free() is called.
336 	 * Since 2.24
337 	 * Returns: a new heap-allocated GVariantIter. [transfer full]
338 	 */
339 	public VariantIter copy()
340 	{
341 		// GVariantIter * g_variant_iter_copy (GVariantIter *iter);
342 		auto p = g_variant_iter_copy(gVariantIter);
343 		
344 		if(p is null)
345 		{
346 			return null;
347 		}
348 		
349 		return new VariantIter(cast(GVariantIter*) p);
350 	}
351 	
352 	/**
353 	 * Frees a heap-allocated GVariantIter. Only call this function on
354 	 * iterators that were returned by g_variant_iter_new() or
355 	 * g_variant_iter_copy().
356 	 * Since 2.24
357 	 */
358 	public void free()
359 	{
360 		// void g_variant_iter_free (GVariantIter *iter);
361 		g_variant_iter_free(gVariantIter);
362 	}
363 	
364 	/**
365 	 * Initialises (without allocating) a GVariantIter. iter may be
366 	 * completely uninitialised prior to this call; its old value is
367 	 * ignored.
368 	 * The iterator remains valid for as long as value exists, and need not
369 	 * be freed in any way.
370 	 * Since 2.24
371 	 * Params:
372 	 * value = a container GVariant
373 	 * Returns: the number of items in value
374 	 */
375 	public gsize init(Variant value)
376 	{
377 		// gsize g_variant_iter_init (GVariantIter *iter,  GVariant *value);
378 		return g_variant_iter_init(gVariantIter, (value is null) ? null : value.getVariantStruct());
379 	}
380 	
381 	/**
382 	 * Queries the number of child items in the container that we are
383 	 * iterating over. This is the total number of items -- not the number
384 	 * of items remaining.
385 	 * This function might be useful for preallocation of arrays.
386 	 * Since 2.24
387 	 * Returns: the number of children in the container
388 	 */
389 	public gsize nChildren()
390 	{
391 		// gsize g_variant_iter_n_children (GVariantIter *iter);
392 		return g_variant_iter_n_children(gVariantIter);
393 	}
394 	
395 	/**
396 	 * Creates a heap-allocated GVariantIter for iterating over the items
397 	 * in value.
398 	 * Use g_variant_iter_free() to free the return value when you no longer
399 	 * need it.
400 	 * A reference is taken to value and will be released only when
401 	 * g_variant_iter_free() is called.
402 	 * Since 2.24
403 	 * Params:
404 	 * value = a container GVariant
405 	 * Throws: ConstructionException GTK+ fails to create the object.
406 	 */
407 	public this (Variant value)
408 	{
409 		// GVariantIter * g_variant_iter_new (GVariant *value);
410 		auto p = g_variant_iter_new((value is null) ? null : value.getVariantStruct());
411 		if(p is null)
412 		{
413 			throw new ConstructionException("null returned by g_variant_iter_new((value is null) ? null : value.getVariantStruct())");
414 		}
415 		this(cast(GVariantIter*) p);
416 	}
417 	
418 	/**
419 	 * Gets the next item in the container. If no more items remain then
420 	 * NULL is returned.
421 	 * Use g_variant_unref() to drop your reference on the return value when
422 	 * you no longer need it.
423 	 * $(DDOC_COMMENT example)
424 	 * Since 2.24
425 	 * Returns: a GVariant, or NULL. [allow-none][transfer full]
426 	 */
427 	public Variant nextValue()
428 	{
429 		// GVariant * g_variant_iter_next_value (GVariantIter *iter);
430 		auto p = g_variant_iter_next_value(gVariantIter);
431 		
432 		if(p is null)
433 		{
434 			return null;
435 		}
436 		
437 		return new Variant(cast(GVariant*) p);
438 	}
439 }