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.VariantBuilder; 26 27 private import glib.ConstructionException; 28 private import glib.Str; 29 private import glib.Variant; 30 private import glib.VariantType; 31 private import glib.c.functions; 32 public import glib.c.types; 33 private import gtkd.Loader; 34 35 36 /** 37 * A utility type for constructing container-type #GVariant instances. 38 * 39 * This is an opaque structure and may only be accessed using the 40 * following functions. 41 * 42 * #GVariantBuilder is not threadsafe in any way. Do not attempt to 43 * access it from more than one thread. 44 */ 45 public class VariantBuilder 46 { 47 /** the main Gtk struct */ 48 protected GVariantBuilder* gVariantBuilder; 49 protected bool ownedRef; 50 51 /** Get the main Gtk struct */ 52 public GVariantBuilder* getVariantBuilderStruct(bool transferOwnership = false) 53 { 54 if (transferOwnership) 55 ownedRef = false; 56 return gVariantBuilder; 57 } 58 59 /** the main Gtk struct as a void* */ 60 protected void* getStruct() 61 { 62 return cast(void*)gVariantBuilder; 63 } 64 65 /** 66 * Sets our main struct and passes it to the parent class. 67 */ 68 public this (GVariantBuilder* gVariantBuilder, bool ownedRef = false) 69 { 70 this.gVariantBuilder = gVariantBuilder; 71 this.ownedRef = ownedRef; 72 } 73 74 ~this () 75 { 76 if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef ) 77 g_variant_builder_unref(gVariantBuilder); 78 } 79 80 81 /** 82 * Allocates and initialises a new #GVariantBuilder. 83 * 84 * You should call g_variant_builder_unref() on the return value when it 85 * is no longer needed. The memory will not be automatically freed by 86 * any other call. 87 * 88 * In most cases it is easier to place a #GVariantBuilder directly on 89 * the stack of the calling function and initialise it with 90 * g_variant_builder_init(). 91 * 92 * Params: 93 * type = a container type 94 * 95 * Returns: a #GVariantBuilder 96 * 97 * Since: 2.24 98 * 99 * Throws: ConstructionException GTK+ fails to create the object. 100 */ 101 public this(VariantType type) 102 { 103 auto __p = g_variant_builder_new((type is null) ? null : type.getVariantTypeStruct()); 104 105 if(__p is null) 106 { 107 throw new ConstructionException("null returned by new"); 108 } 109 110 this(cast(GVariantBuilder*) __p); 111 } 112 113 /** 114 * Adds @value to @builder. 115 * 116 * It is an error to call this function in any way that would create an 117 * inconsistent value to be constructed. Some examples of this are 118 * putting different types of items into an array, putting the wrong 119 * types or number of items in a tuple, putting more than one value into 120 * a variant, etc. 121 * 122 * If @value is a floating reference (see g_variant_ref_sink()), 123 * the @builder instance takes ownership of @value. 124 * 125 * Params: 126 * value = a #GVariant 127 * 128 * Since: 2.24 129 */ 130 public void addValue(Variant value) 131 { 132 g_variant_builder_add_value(gVariantBuilder, (value is null) ? null : value.getVariantStruct()); 133 } 134 135 /** 136 * Releases all memory associated with a #GVariantBuilder without 137 * freeing the #GVariantBuilder structure itself. 138 * 139 * It typically only makes sense to do this on a stack-allocated 140 * #GVariantBuilder if you want to abort building the value part-way 141 * through. This function need not be called if you call 142 * g_variant_builder_end() and it also doesn't need to be called on 143 * builders allocated with g_variant_builder_new() (see 144 * g_variant_builder_unref() for that). 145 * 146 * This function leaves the #GVariantBuilder structure set to all-zeros. 147 * It is valid to call this function on either an initialised 148 * #GVariantBuilder or one that is set to all-zeros but it is not valid 149 * to call this function on uninitialised memory. 150 * 151 * Since: 2.24 152 */ 153 public void clear() 154 { 155 g_variant_builder_clear(gVariantBuilder); 156 } 157 158 /** 159 * Closes the subcontainer inside the given @builder that was opened by 160 * the most recent call to g_variant_builder_open(). 161 * 162 * It is an error to call this function in any way that would create an 163 * inconsistent value to be constructed (ie: too few values added to the 164 * subcontainer). 165 * 166 * Since: 2.24 167 */ 168 public void close() 169 { 170 g_variant_builder_close(gVariantBuilder); 171 } 172 173 /** 174 * Ends the builder process and returns the constructed value. 175 * 176 * It is not permissible to use @builder in any way after this call 177 * except for reference counting operations (in the case of a 178 * heap-allocated #GVariantBuilder) or by reinitialising it with 179 * g_variant_builder_init() (in the case of stack-allocated). This 180 * means that for the stack-allocated builders there is no need to 181 * call g_variant_builder_clear() after the call to 182 * g_variant_builder_end(). 183 * 184 * It is an error to call this function in any way that would create an 185 * inconsistent value to be constructed (ie: insufficient number of 186 * items added to a container with a specific number of children 187 * required). It is also an error to call this function if the builder 188 * was created with an indefinite array or maybe type and no children 189 * have been added; in this case it is impossible to infer the type of 190 * the empty array. 191 * 192 * Returns: a new, floating, #GVariant 193 * 194 * Since: 2.24 195 */ 196 public Variant end() 197 { 198 auto __p = g_variant_builder_end(gVariantBuilder); 199 200 if(__p is null) 201 { 202 return null; 203 } 204 205 return new Variant(cast(GVariant*) __p); 206 } 207 208 /** 209 * Initialises a #GVariantBuilder structure. 210 * 211 * @type must be non-%NULL. It specifies the type of container to 212 * construct. It can be an indefinite type such as 213 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)". 214 * Maybe, array, tuple, dictionary entry and variant-typed values may be 215 * constructed. 216 * 217 * After the builder is initialised, values are added using 218 * g_variant_builder_add_value() or g_variant_builder_add(). 219 * 220 * After all the child values are added, g_variant_builder_end() frees 221 * the memory associated with the builder and returns the #GVariant that 222 * was created. 223 * 224 * This function completely ignores the previous contents of @builder. 225 * On one hand this means that it is valid to pass in completely 226 * uninitialised memory. On the other hand, this means that if you are 227 * initialising over top of an existing #GVariantBuilder you need to 228 * first call g_variant_builder_clear() in order to avoid leaking 229 * memory. 230 * 231 * You must not call g_variant_builder_ref() or 232 * g_variant_builder_unref() on a #GVariantBuilder that was initialised 233 * with this function. If you ever pass a reference to a 234 * #GVariantBuilder outside of the control of your own code then you 235 * should assume that the person receiving that reference may try to use 236 * reference counting; you should use g_variant_builder_new() instead of 237 * this function. 238 * 239 * Params: 240 * type = a container type 241 * 242 * Since: 2.24 243 */ 244 public void init(VariantType type) 245 { 246 g_variant_builder_init(gVariantBuilder, (type is null) ? null : type.getVariantTypeStruct()); 247 } 248 249 /** 250 * Opens a subcontainer inside the given @builder. When done adding 251 * items to the subcontainer, g_variant_builder_close() must be called. @type 252 * is the type of the container: so to build a tuple of several values, @type 253 * must include the tuple itself. 254 * 255 * It is an error to call this function in any way that would cause an 256 * inconsistent value to be constructed (ie: adding too many values or 257 * a value of an incorrect type). 258 * 259 * Example of building a nested variant: 260 * |[<!-- language="C" --> 261 * GVariantBuilder builder; 262 * guint32 some_number = get_number (); 263 * g_autoptr (GHashTable) some_dict = get_dict (); 264 * GHashTableIter iter; 265 * const gchar *key; 266 * const GVariant *value; 267 * g_autoptr (GVariant) output = NULL; 268 * 269 * g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ua{sv})")); 270 * g_variant_builder_add (&builder, "u", some_number); 271 * g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}")); 272 * 273 * g_hash_table_iter_init (&iter, some_dict); 274 * while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) 275 * { 276 * g_variant_builder_open (&builder, G_VARIANT_TYPE ("{sv}")); 277 * g_variant_builder_add (&builder, "s", key); 278 * g_variant_builder_add (&builder, "v", value); 279 * g_variant_builder_close (&builder); 280 * } 281 * 282 * g_variant_builder_close (&builder); 283 * 284 * output = g_variant_builder_end (&builder); 285 * ]| 286 * 287 * Params: 288 * type = the #GVariantType of the container 289 * 290 * Since: 2.24 291 */ 292 public void open(VariantType type) 293 { 294 g_variant_builder_open(gVariantBuilder, (type is null) ? null : type.getVariantTypeStruct()); 295 } 296 297 alias doref = ref_; 298 /** 299 * Increases the reference count on @builder. 300 * 301 * Don't call this on stack-allocated #GVariantBuilder instances or bad 302 * things will happen. 303 * 304 * Returns: a new reference to @builder 305 * 306 * Since: 2.24 307 */ 308 public VariantBuilder ref_() 309 { 310 auto __p = g_variant_builder_ref(gVariantBuilder); 311 312 if(__p is null) 313 { 314 return null; 315 } 316 317 return new VariantBuilder(cast(GVariantBuilder*) __p, true); 318 } 319 320 /** 321 * Decreases the reference count on @builder. 322 * 323 * In the event that there are no more references, releases all memory 324 * associated with the #GVariantBuilder. 325 * 326 * Don't call this on stack-allocated #GVariantBuilder instances or bad 327 * things will happen. 328 * 329 * Since: 2.24 330 */ 331 public void unref() 332 { 333 g_variant_builder_unref(gVariantBuilder); 334 } 335 }