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