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