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 = gobject-Closures.html 27 * outPack = gobject 28 * outFile = Closure 29 * strct = GClosure 30 * realStrct= 31 * ctorStrct= 32 * clss = Closure 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_closure_ 41 * omit structs: 42 * omit prefixes: 43 * - g_cclosure_ 44 * omit code: 45 * omit signals: 46 * imports: 47 * - gobject.ObjectG 48 * - gobject.Value 49 * - glib.Source 50 * structWrap: 51 * - GClosure* -> Closure 52 * - GObject* -> ObjectG 53 * - GSource* -> Source 54 * - GValue* -> Value 55 * module aliases: 56 * local aliases: 57 * overrides: 58 */ 59 60 module gobject.Closure; 61 62 public import gtkc.gobjecttypes; 63 64 private import gtkc.gobject; 65 private import glib.ConstructionException; 66 private import gobject.ObjectG; 67 68 69 private import gobject.ObjectG; 70 private import gobject.Value; 71 private import glib.Source; 72 73 74 75 76 /** 77 * Description 78 * A GClosure represents a callback supplied by the programmer. It 79 * will generally comprise a function of some kind and a marshaller 80 * used to call it. It is the reponsibility of the marshaller to 81 * convert the arguments for the invocation from GValues into 82 * a suitable form, perform the callback on the converted arguments, 83 * and transform the return value back into a GValue. 84 * In the case of C programs, a closure usually just holds a pointer 85 * to a function and maybe a data argument, and the marshaller 86 * converts between GValue and native C types. The GObject 87 * library provides the GCClosure type for this purpose. Bindings for 88 * other languages need marshallers which convert between GValues and suitable representations in the runtime of the language in 89 * order to use functions written in that languages as callbacks. 90 * Within GObject, closures play an important role in the 91 * implementation of signals. When a signal is registered, the 92 * c_marshaller argument to g_signal_new() specifies the default C 93 * marshaller for any closure which is connected to this 94 * signal. GObject provides a number of C marshallers for this 95 * purpose, see the g_cclosure_marshal_*() functions. Additional C 96 * marshallers can be generated with the glib-genmarshal utility. Closures 97 * can be explicitly connected to signals with 98 * g_signal_connect_closure(), but it usually more convenient to let 99 * GObject create a closure automatically by using one of the 100 * g_signal_connect_*() functions which take a callback function/user 101 * data pair. 102 * Using closures has a number of important advantages over a simple 103 * callback function/data pointer combination: 104 * Closures allow the callee to get the types of the callback parameters, 105 * which means that language bindings don't have to write individual glue 106 * for each callback type. 107 * The reference counting of GClosure makes it easy to handle reentrancy 108 * right; if a callback is removed while it is being invoked, the closure 109 * and its parameters won't be freed until the invocation finishes. 110 * g_closure_invalidate() and invalidation notifiers allow callbacks to be 111 * automatically removed when the objects they point to go away. 112 */ 113 public class Closure 114 { 115 116 /** the main Gtk struct */ 117 protected GClosure* gClosure; 118 119 120 public GClosure* getClosureStruct() 121 { 122 return gClosure; 123 } 124 125 126 /** the main Gtk struct as a void* */ 127 protected void* getStruct() 128 { 129 return cast(void*)gClosure; 130 } 131 132 /** 133 * Sets our main struct and passes it to the parent class 134 */ 135 public this (GClosure* gClosure) 136 { 137 this.gClosure = gClosure; 138 } 139 140 /** 141 */ 142 143 /** 144 * A variant of g_closure_new_simple() which stores object in the 145 * data field of the closure and calls g_object_watch_closure() on 146 * object and the created closure. This function is mainly useful 147 * when implementing new types of closures. 148 * Params: 149 * sizeofClosure = the size of the structure to allocate, must be at least 150 * sizeof (GClosure) 151 * object = a GObject pointer to store in the data field of the newly 152 * allocated GClosure 153 * Throws: ConstructionException GTK+ fails to create the object. 154 */ 155 public this (uint sizeofClosure, ObjectG object) 156 { 157 // GClosure * g_closure_new_object (guint sizeof_closure, GObject *object); 158 auto p = g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct()); 159 if(p is null) 160 { 161 throw new ConstructionException("null returned by g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct())"); 162 } 163 this(cast(GClosure*) p); 164 } 165 166 /** 167 * Increments the reference count on a closure to force it staying 168 * alive while the caller holds a pointer to it. 169 * Returns: The closure passed in, for convenience. [transfer none] 170 */ 171 public Closure doref() 172 { 173 // GClosure * g_closure_ref (GClosure *closure); 174 auto p = g_closure_ref(gClosure); 175 176 if(p is null) 177 { 178 return null; 179 } 180 181 return ObjectG.getDObject!(Closure)(cast(GClosure*) p); 182 } 183 184 /** 185 * Takes over the initial ownership of a closure. Each closure is 186 * initially created in a floating state, which 187 * means that the initial reference count is not owned by any caller. 188 * g_closure_sink() checks to see if the object is still floating, and 189 * if so, unsets the floating state and decreases the reference 190 * count. If the closure is not floating, g_closure_sink() does 191 * nothing. The reason for the existance of the floating state is to 192 */ 193 public void sink() 194 { 195 // void g_closure_sink (GClosure *closure); 196 g_closure_sink(gClosure); 197 } 198 199 /** 200 * Decrements the reference count of a closure after it was previously 201 * incremented by the same caller. If no other callers are using the 202 * closure, then the closure will be destroyed and freed. 203 */ 204 public void unref() 205 { 206 // void g_closure_unref (GClosure *closure); 207 g_closure_unref(gClosure); 208 } 209 210 /** 211 * Invokes the closure, i.e. executes the callback represented by the closure. 212 * Params: 213 * returnValue = a GValue to store the return value. May be NULL if the 214 * callback of closure doesn't return a value. 215 * nParamValues = the length of the param_values array 216 * paramValues = an array of 217 * GValues holding the arguments on which to 218 * invoke the callback of closure. [array length=n_param_values] 219 * invocationHint = a context-dependent invocation hint 220 */ 221 public void invoke(Value returnValue, uint nParamValues, Value paramValues, void* invocationHint) 222 { 223 // void g_closure_invoke (GClosure *closure, GValue *return_value, guint n_param_values, const GValue *param_values, gpointer invocation_hint); 224 g_closure_invoke(gClosure, (returnValue is null) ? null : returnValue.getValueStruct(), nParamValues, (paramValues is null) ? null : paramValues.getValueStruct(), invocationHint); 225 } 226 227 /** 228 * Sets a flag on the closure to indicate that its calling 229 * environment has become invalid, and thus causes any future 230 * invocations of g_closure_invoke() on this closure to be 231 * ignored. Also, invalidation notifiers installed on the closure will 232 * be called at this point. Note that unless you are holding a 233 * reference to the closure yourself, the invalidation notifiers may 234 * unref the closure and cause it to be destroyed, so if you need to 235 * access the closure after calling g_closure_invalidate(), make sure 236 * that you've previously called g_closure_ref(). 237 * Note that g_closure_invalidate() will also be called when the 238 * reference count of a closure drops to zero (unless it has already 239 * been invalidated before). 240 */ 241 public void invalidate() 242 { 243 // void g_closure_invalidate (GClosure *closure); 244 g_closure_invalidate(gClosure); 245 } 246 247 /** 248 * Registers a finalization notifier which will be called when the 249 * reference count of closure goes down to 0. Multiple finalization 250 * notifiers on a single closure are invoked in unspecified order. If 251 * a single call to g_closure_unref() results in the closure being 252 * both invalidated and finalized, then the invalidate notifiers will 253 * be run before the finalize notifiers. 254 * Params: 255 * notifyData = data to pass to notify_func 256 * notifyFunc = the callback function to register 257 */ 258 public void addFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc) 259 { 260 // void g_closure_add_finalize_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func); 261 g_closure_add_finalize_notifier(gClosure, notifyData, notifyFunc); 262 } 263 264 /** 265 * Registers an invalidation notifier which will be called when the 266 * closure is invalidated with g_closure_invalidate(). Invalidation 267 * notifiers are invoked before finalization notifiers, in an 268 * unspecified order. 269 * Params: 270 * notifyData = data to pass to notify_func 271 * notifyFunc = the callback function to register 272 */ 273 public void addInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc) 274 { 275 // void g_closure_add_invalidate_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func); 276 g_closure_add_invalidate_notifier(gClosure, notifyData, notifyFunc); 277 } 278 279 /** 280 * Removes a finalization notifier. 281 * Notice that notifiers are automatically removed after they are run. 282 * Params: 283 * notifyData = data which was passed to g_closure_add_finalize_notifier() 284 * when registering notify_func 285 * notifyFunc = the callback function to remove 286 */ 287 public void removeFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc) 288 { 289 // void g_closure_remove_finalize_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func); 290 g_closure_remove_finalize_notifier(gClosure, notifyData, notifyFunc); 291 } 292 293 /** 294 * Removes an invalidation notifier. 295 * Notice that notifiers are automatically removed after they are run. 296 * Params: 297 * notifyData = data which was passed to g_closure_add_invalidate_notifier() 298 * when registering notify_func 299 * notifyFunc = the callback function to remove 300 */ 301 public void removeInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc) 302 { 303 // void g_closure_remove_invalidate_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func); 304 g_closure_remove_invalidate_notifier(gClosure, notifyData, notifyFunc); 305 } 306 307 /** 308 * Allocates a struct of the given size and initializes the initial 309 * part as a GClosure. This function is mainly useful when 310 * implementing new types of closures. 311 * $(DDOC_COMMENT example) 312 * Params: 313 * sizeofClosure = the size of the structure to allocate, must be at least 314 * sizeof (GClosure) 315 * data = data to store in the data field of the newly allocated GClosure 316 * Throws: ConstructionException GTK+ fails to create the object. 317 */ 318 public this (uint sizeofClosure, void* data) 319 { 320 // GClosure * g_closure_new_simple (guint sizeof_closure, gpointer data); 321 auto p = g_closure_new_simple(sizeofClosure, data); 322 if(p is null) 323 { 324 throw new ConstructionException("null returned by g_closure_new_simple(sizeofClosure, data)"); 325 } 326 this(cast(GClosure*) p); 327 } 328 329 /** 330 * Sets the marshaller of closure. The marshal_data 331 * of marshal provides a way for a meta marshaller to provide additional 332 * information to the marshaller. (See g_closure_set_meta_marshal().) For 333 * GObject's C predefined marshallers (the g_cclosure_marshal_*() 334 * functions), what it provides is a callback function to use instead of 335 * closure->callback. 336 * Params: 337 * marshal = a GClosureMarshal function 338 */ 339 public void setMarshal(GClosureMarshal marshal) 340 { 341 // void g_closure_set_marshal (GClosure *closure, GClosureMarshal marshal); 342 g_closure_set_marshal(gClosure, marshal); 343 } 344 345 /** 346 * Adds a pair of notifiers which get invoked before and after the 347 * closure callback, respectively. This is typically used to protect 348 * the extra arguments for the duration of the callback. See 349 * g_object_watch_closure() for an example of marshal guards. 350 * Params: 351 * preMarshalData = data to pass to pre_marshal_notify 352 * preMarshalNotify = a function to call before the closure callback 353 * postMarshalData = data to pass to post_marshal_notify 354 * postMarshalNotify = a function to call after the closure callback 355 */ 356 public void addMarshalGuards(void* preMarshalData, GClosureNotify preMarshalNotify, void* postMarshalData, GClosureNotify postMarshalNotify) 357 { 358 // void g_closure_add_marshal_guards (GClosure *closure, gpointer pre_marshal_data, GClosureNotify pre_marshal_notify, gpointer post_marshal_data, GClosureNotify post_marshal_notify); 359 g_closure_add_marshal_guards(gClosure, preMarshalData, preMarshalNotify, postMarshalData, postMarshalNotify); 360 } 361 362 /** 363 * Sets the meta marshaller of closure. A meta marshaller wraps 364 * closure->marshal and modifies the way it is called in some 365 * fashion. The most common use of this facility is for C callbacks. 366 * The same marshallers (generated by glib-genmarshal) are used 367 * everywhere, but the way that we get the callback function 368 * differs. In most cases we want to use closure->callback, but in 369 * other cases we want to use some different technique to retrieve the 370 * callback function. 371 * For example, class closures for signals (see 372 * g_signal_type_cclosure_new()) retrieve the callback function from a 373 * fixed offset in the class structure. The meta marshaller retrieves 374 * the right callback and passes it to the marshaller as the 375 * marshal_data argument. 376 * Params: 377 * marshalData = context-dependent data to pass to meta_marshal 378 * metaMarshal = a GClosureMarshal function 379 */ 380 public void setMetaMarshal(void* marshalData, GClosureMarshal metaMarshal) 381 { 382 // void g_closure_set_meta_marshal (GClosure *closure, gpointer marshal_data, GClosureMarshal meta_marshal); 383 g_closure_set_meta_marshal(gClosure, marshalData, metaMarshal); 384 } 385 386 /** 387 * Set the callback for a source as a GClosure. 388 * If the source is not one of the standard GLib types, the closure_callback 389 * and closure_marshal fields of the GSourceFuncs structure must have been 390 * filled in with pointers to appropriate functions. 391 * Params: 392 * source = the source 393 * closure = a GClosure 394 */ 395 public static void gSourceSetClosure(Source source, Closure closure) 396 { 397 // void g_source_set_closure (GSource *source, GClosure *closure); 398 g_source_set_closure((source is null) ? null : source.getSourceStruct(), (closure is null) ? null : closure.getClosureStruct()); 399 } 400 401 /** 402 * Sets a dummy callback for source. The callback will do nothing, and 403 * if the source expects a gboolean return value, it will return TRUE. 404 * (If the source expects any other type of return value, it will return 405 * a 0/NULL value; whatever g_value_init() initializes a GValue to for 406 * that type.) 407 * If the source is not one of the standard GLib types, the 408 * closure_callback and closure_marshal fields of the GSourceFuncs 409 * structure must have been filled in with pointers to appropriate 410 * functions. 411 * Params: 412 * source = the source 413 */ 414 public static void gSourceSetDummyCallback(Source source) 415 { 416 // void g_source_set_dummy_callback (GSource *source); 417 g_source_set_dummy_callback((source is null) ? null : source.getSourceStruct()); 418 } 419 }